| 07-25-2009, 02:09 PM | #1 |
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. GetRandomPointInCircle:globals private location temploc endglobals function GetRandomPointInCircle takes location l, real radius returns location local real x2 = GetLocationX(l) local real y2 = GetLocationY(l) 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 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 call MoveLocation(temploc,dx,dy) return temploc endfunction 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 |
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 |
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 |
Updated the code. Leave cleaning the returned location in the hands of the user. |
| 07-25-2009, 02:26 PM | #5 | |
Quote:
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 |
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 |
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 | |
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? |
| 07-25-2009, 04:25 PM | #9 | |
Quote:
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 | |
Quote:
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 |
Except now your function is not random at all? |
| 07-25-2009, 04:45 PM | #12 |
Darn... lol. I think it doesn't even need a function now... |
| 07-25-2009, 05:07 PM | #13 | |
Quote:
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 |
Yes, with locations, a struct or a parallel array, what do you want from those three? |
| 07-25-2009, 05:30 PM | #15 | |
Quote:
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... |
