HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

GetRandomPointInCircle

07-25-2009, 02:09 PM#1
ploks
I'm tryin to get a random point in a circle which is defined by the centre coordinates (or location) and the radius. First I thought of taking a random angle and then randomize a point on the line that is created between the centre and the edge of the circle. However, that way there are more results near the centre.


The second approach was to get a randomized point in a square and then check whether the distance between the point and the centre is lower than the radius.


Expand GetRandomPointInCircle:

However this function returns a location (and takes one) and I have learned that locs are evil. Is this a nice way? how can I optimize it? And, does it even work?
07-25-2009, 02:14 PM#2
Kwah
Collapse Something like this:
function GetRandomPointInCircle takes real X, real Y, real radius returns location
    local real x2 = X 
    local real y2 = Y
    local real x1 = GetRandomReal(x2-radius,x2+radius)
    local real y1 = GetRandomReal(y2-radius,y2+radius)
    local real dx = x2 - y2
    local real dy = y2 - y1
    local location loc
        loop 
        exitwhen (dx*dx + dy*dy) <= radius*radius 
            if (dx*dx + dy*dy) > radius*radius then
                set dx = x2 - y2
                set dy = y2 - y1
            endif
        endloop 
    set loc = Location(dx,dy) 
    return loc
endfunction
07-25-2009, 02:17 PM#3
ploks
How would I return 2 values? A struct, seems a bit too much, or 2 functions one for the X value and one for the Y?

And about the location cleaning, doesn't my way works with a global loc that is moved between different points?
07-25-2009, 02:21 PM#4
Kwah
Updated the code.

Leave cleaning the returned location in the hands of the user.
07-25-2009, 02:26 PM#5
ploks
Quote:
Originally Posted by Kwah
Updated the code.

Leave cleaning the returned location in the hands of the user.

Seems correct! Thanks. Maybe there should be a system for moving locations around. (if that's more efficient than creating new all the time) kind of GroupUtils for locations and rects.
07-25-2009, 02:45 PM#6
chobibo
Collapse JASS:
function GetCircleRandomX takes real center_x, real radius returns real
    return GetRandomReal(center_x, radius)*Cos(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

function GetCircleRandomY takes real center_y, real radius returns real
    return GetRandomReal(center_y, radius)*Sin(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

Can't you use those?
07-25-2009, 03:30 PM#7
ploks
Wont the result of those be more centred to the centre of the circle, or do I fail to understand the functions? (My knowledge in cos and sin is limited at the best)
07-25-2009, 03:31 PM#8
Wizardum
Quote:
Originally Posted by chobibo
Collapse JASS:
function GetCircleRandomX takes real center_x, real radius returns real
    return GetRandomReal(center_x, radius)*Cos(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

function GetCircleRandomY takes real center_y, real radius returns real
    return GetRandomReal(center_y, radius)*Sin(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

Can't you use those?

does this actually work? your using random angles for each of the coordinates, I guess x and y should have the same angle, am I wrong?
07-25-2009, 04:25 PM#9
Vexorian
Quote:
Originally Posted by chobibo
Collapse JASS:
function GetCircleRandomX takes real center_x, real radius returns real
    return GetRandomReal(center_x, radius)*Cos(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

function GetCircleRandomY takes real center_y, real radius returns real
    return GetRandomReal(center_y, radius)*Sin(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

Can't you use those?
Those got a wrong random distribution.

Try creating a lot of points using those functions, you'll see a bias towards the center.

http://mathworld.wolfram.com/DiskPointPicking.html
07-25-2009, 04:26 PM#10
chobibo
Quote:
does this actually work? your using random angles for each of the coordinates, I guess x and y should have the same angle, am I wrong?

Oh sorry about that, thanks for the correction.

EDIT: Deleted misleading part. Vexorian thanks for telling me lol.
07-25-2009, 04:36 PM#11
Vexorian
Except now your function is not random at all?
07-25-2009, 04:45 PM#12
chobibo
Darn... lol. I think it doesn't even need a function now...
07-25-2009, 05:07 PM#13
ploks
Quote:
Originally Posted by chobibo
Darn... lol. I think it doesn't even need a function now...

I would say that the question is that: Can it be made in a function?


I fail to see how, especially efficient. (The loop might repeat itself, forever and ever...)
07-25-2009, 05:25 PM#14
chobibo
Yes, with locations, a struct or a parallel array, what do you want from those three?
07-25-2009, 05:30 PM#15
ploks
Quote:
Originally Posted by chobibo
Yes, with locations, a struct or a parallel array, what do you want from those three?

I saw somewhere else that locations where still faster than a struct and I think a parallel array. So I guess I keep locations. Will figure something out.


Or can't you have a long incredibly complicated functions that put all (or some) of the coordinates that exist in a circle in a array. After that it will randomize an index and take those coordinates. Get my idea? Or maybe it's just stupid. I just feel unsafe with the possibility of so many runs in a loop for just a random point...