Wc3C.net GetRandomPointInCircle
 Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar

07-25-2009, 06:37 PM   #16
chobibo
100% Genuine Retard!

Join Date: Jan 2007
Posts: 797

Quote:
 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...

That would be inefficient, it would take a lot of memory and a lot of processing power.
__________________
Where's mah sig?

Last edited by chobibo : 07-25-2009 at 07:47 PM.

 07-25-2009, 06:51 PM #17 ploks User   Join Date: Jun 2007 Posts: 99 Doesn't that function still give an uneven distribution. The distance between the imagined lines (one per degree) is longer the farther out you come from the middle. (like a sun) I believe that a loop is the only way. (create a square with the side 2r, randomize a point in square, check if distance between point and centre is <=r, if not repeat with new point)
 07-25-2009, 07:02 PM #18 Cheezeman User     Join Date: Apr 2009 Posts: 179 Flame_Phoenix once showed me this function made by Vexorian: JASS:``` //Function made by Vexorian, it selects a random region in a disk. //All regions have the same chance of beeing choosen function GetRandomPointInDisk takes real centerx, real centery, real minradius, real maxradius returns location local real d = SquareRoot(GetRandomReal(minradius * minradius, maxradius * maxradius)) local real a = GetRandomReal(0, 2 * bj_PI) return Location(centerx + d * Cos(a), centery + d * Sin(a)) endfunction ``` Maybe it helps? __________________ I have no signature yet
 07-25-2009, 07:06 PM #19 chobibo 100% Genuine Retard!     Join Date: Jan 2007 Posts: 797 I think you're right. __________________ Where's mah sig?
 07-25-2009, 07:47 PM #20 ploks User   Join Date: Jun 2007 Posts: 99 Thanks for the answers. Guess I'll go with Vexorians function (even though I don't understand it). The reward Rep rep!
 07-25-2009, 08:30 PM #21 chobibo 100% Genuine Retard!     Join Date: Jan 2007 Posts: 797 I also don't know,maybe the formula for the ara of the circle has something to do with this, since it used this formula: valid_r = √random(min_r^2, max_r^2) __________________ Where's mah sig?
07-25-2009, 10:13 PM   #22
Vexorian
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Quote:
 I believe that a loop is the only way. (create a square with the side 2r, randomize a point in square
square root + sin/cos does work.
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

07-25-2009, 10:31 PM   #23
uberfoop
User

Join Date: Jul 2005
Posts: 207

Submissions (1)

Quote:
 Originally Posted by Vexorian square root + sin/cos does work.
Indeed. The math works, and a quick distribution chart agrees as well:
__________________
Quote:
 Originally Posted by darkwulfv .03 is one 300th of a second.

Last edited by uberfoop : 07-25-2009 at 10:33 PM.

07-25-2009, 10:40 PM   #24
Anitarf
Procrastination Incarnate

Development Director

Join Date: Feb 2004
Posts: 8,190

Submissions (19)

That function is the only sensible thing to use, also it is quite short so you can just inline the code so you don't have to deal with locations.
__________________
 Cinema Workshop released! Let's make some cinematics! Editor's version of the winner of the Blizzard's Cinematic Contest: The Spirit of Vengeance

07-25-2009, 10:58 PM   #25
Vexorian
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Anyway, the probability a random point in the square will be inside the circle is 0.785398164 (3 / pi).

Therefore, the Expected number of tries is:

E = 1 + E*(1 - 0.785398164 )
0.785398164 E = 1
E = 1.273239544

So on average the function needs slightly more than one try to get a point in the circle. So it is not as slow as it might look like, this is interesting, because for more complicated cases, like getting a random point inside two circles which have an intersection, it is probably the only way to do it without going crazy in the process...
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

07-25-2009, 11:10 PM   #26
ploks
User

Join Date: Jun 2007
Posts: 99

Quote:
 Originally Posted by Vexorian square root + sin/cos does work.

I don't doubt that. It just that the math is beyond me since I haven't started with trigonometric functions yet in school. Will do that after the summer break. Thanks for the function anyways :)

07-25-2009, 11:12 PM   #27
Anitarf
Procrastination Incarnate

Development Director

Join Date: Feb 2004
Posts: 8,190

Submissions (19)

That's true, although with complicated cases, the smallest rectangle that encompasses your shape might still have a large percentage of it's area outside the shape, which makes it less effecient; that might be a moot point though if there's no other sane way to do it with that kind of shape.

Quote:
 Originally Posted by ploks I don't doubt that. It just that the math is beyond me since I haven't started with trigonometric functions yet in school. Will do that after the summer break. Thanks for the function anyways :)
__________________
 Cinema Workshop released! Let's make some cinematics! Editor's version of the winner of the Blizzard's Cinematic Contest: The Spirit of Vengeance

Last edited by Anitarf : 07-25-2009 at 11:14 PM.

07-25-2009, 11:23 PM   #28
ploks
User

Join Date: Jun 2007
Posts: 99

Quote:
 Originally Posted by Anitarf You can always read up on them ahead of time.

Doin it already. However I guess a real math teacher combined with the fear of a bad grade will make me learn faster ;)