HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Circular Rects

12-15-2006, 03:01 AM#1
xombie
Apparently doing:
Collapse JASS:
Rect( x - radius, y - radius, x + radius, y + radius )
will return a circular rect, as implied by the function:
Collapse JASS:
function GetRectFromCircleBJ takes location center,real radius returns rect
    local real centerX = GetLocationX(center)
    local real centerY = GetLocationY(center)
    return Rect(centerX - radius, centerY - radius, centerX + radius, centerY + radius)
endfunction
However when I used this same knowledge to make a function that gets a random location from a circular rect, the "rect" used was square, and therefore returned locations within the square, but outside the circle. Other than checking the distance of each generated spot to the origin of the "circle", are there any other ways to get a circle?

Edit: Unless ofcourse this function is used to determine a square, given a circle?

Sorry for this, but in addition, if a function returns a location does it leak?
Collapse JASS:
function just_for_kicks takes nothing returns location
local location m = Location( GetRandomReal( -120, 120 ), GetRandomReal( -120, 120 ) )
return m
endfunction
I'm almost positive it does, but in that case how can you get a function to return a location properly? I've tried using the return bug to fix the leak but the location returned always seems to be (0, 0).

Edit: Wow, go figure, the second I resort to posting this on these queries on the forum the answers come to me instantly. If anybody wants to answer these for somebody else's use, go ahead, I already figured it out (I'm so dumb).
12-15-2006, 06:23 AM#2
Captain Griffen
First point: You can't make rects that are circles. Just do checks. dx^2 + dy^2 <= r^2, then it is in the circle.

Second point: It doesn't leak an object, just has the potential to leak an object. You must set it to a variable in the function where you are using it, and then clean it up there. If you need to return an object, then of course you need to return an object.

There is a handle index leak though. Just use:

Collapse JASS:
return Location(GetRandomReal(-120.,120.), GetRandomReal(-120.,120.))
12-15-2006, 10:01 AM#3
xombie
Hopefully everybody who didn't know this can benefit from it. I got confused because I thought saying:

return Location(...) leaked a location. However I soon after corrected myself and got it working. The other confusion with RectFromCircle was that I thought it took a rect (square) and made a circle, when infact it made a circle and created a square (rectangle, sorry). Nonetheless, thanks.