Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411593 Posts in 69386 Topics- by 58444 Members - Latest Member: FightingFoxGame

May 07, 2024, 10:58:31 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)how to find a random point within a circle
Pages: [1]
Print
Author Topic: how to find a random point within a circle  (Read 2703 times)
nikki
Level 10
*****


View Profile
« on: August 19, 2010, 03:27:42 AM »

probably the answer is easy for you guys, but due to a little car to my-bike-an-me accident my brain stopped functio, func.. f, ah well i drew the question:



edit: jeez, copy-pasting this topic title in google leads to a plefor,plephoriu, .. alot of answer in google , i'm sorry.

« Last Edit: August 19, 2010, 03:32:06 AM by nikki » Logged
mewse
Level 6
*



View Profile WWW
« Reply #1 on: August 19, 2010, 03:34:34 AM »

The best way is really to randomly generate a point within the square, and then check whether that point is inside the circle.  If not, generate another random number.

If you want, you could instead generate an angle (0..2pi radians) and a distance from the center, and then do some trigonometry to convert that into a position (sin(angle)*distance + centerX, cos(angle)*distance + centerY);  that'd ensure that your generated point would be inside the circle.  However, doing this will result in most of your points being near the center of the circle, with fewer points out toward the edges.  The "randomly generate a point in the square, and repeat if that point isn't also inside the circle" approach will give an even distribution across the whole circle.

Logged
agersant
Level 4
****



View Profile
« Reply #2 on: August 19, 2010, 05:11:23 AM »

Why would the "random angle + random radius" technique put most points near the center ? That's the way I'd go.
Logged
Alistair Aitcheson
Level 5
*****


"Ali" for short


View Profile WWW
« Reply #3 on: August 19, 2010, 05:19:42 AM »

Why would the "random angle + random radius" technique put most points near the center ? That's the way I'd go.
If you think about it, the distance between two points at the same radius is going to be smaller if the radius is small, and larger if the radius is large.
Logged

st33d
Guest
« Reply #4 on: August 19, 2010, 05:33:13 AM »

Whenever I do this, I have a handy Trig table to call upon. So I just pick an index out of 360 and multiply the cos and sin indices by a random length.

I use it for explosions.
Logged
nikki
Level 10
*****


View Profile
« Reply #5 on: August 19, 2010, 05:43:10 AM »

I've tried both methods this far:



"The best way is really to randomly generate a point within the square, and then check whether that point is inside the circle.  If not, generate another random number. If you want, you could instead generate an angle (0..2pi radians) and a distance from the center, and then do some trigonometry to convert that into a position (sin(angle)*distance + centerX, cos(angle)*distance + centerY);  that'd ensure that your generated point would be inside the circle.  However, doing this will result in most of your points being near the center of the circle, with fewer points out toward the edges. The "randomly generate a point in the square, and repeat if that point isn't also inside the circle" approach will give an even distribution across the whole circle."

edited some more / deleted the first image i showed because a value was simply wrong.. edited in nice color-code the second time Wink
« Last Edit: August 19, 2010, 05:55:58 AM by nikki » Logged
Pishtaco
Level 10
*****


View Profile WWW
« Reply #6 on: August 19, 2010, 05:52:17 AM »


Think of it this way: imagine two circles, A and B, both with the same centre, but A has radius 1 and B has radius 0.5. Since B has half the radius of A, it has only one quarter of the area of A.

If you use the blue method to generate random points inside A, on average one quarter of the points will be in B.

If you use the red method, on average one half of the points will be in B. This is because this method puts a point in B if, when you choose the random distance from the centre, you choose a number <0.5.

I've tried both methods this far:

Looks like something is bugged with your red points.
Logged

jmp
Level 0
**


View Profile
« Reply #7 on: August 19, 2010, 05:52:59 AM »

Quote
However, doing this will result in most of your points being near the center of the circle, with fewer points out toward the edges.

One solution to this is to use a random radius r in the range [0, 1] and the coordinates

x = sqrt(r) * cos(theta)
y = sqrt(r) * sin(theta)

Then you can multiply the coordinates with the radius you want. It gives a pretty good result:


Bad

Good

See: http://mathworld.wolfram.com/DiskPointPicking.html
« Last Edit: August 19, 2010, 08:01:38 AM by jmp » Logged
nikki
Level 10
*****


View Profile
« Reply #8 on: August 19, 2010, 05:59:46 AM »

@jmp and Pishtaco : aah, i see, well i guess my red method is still flawed then, thanks for pointing that out , i was under the impression that cross that appeared over the axes was a side effect of the algorithm, thanks for that link, i'll have a go later.
Logged
Pishtaco
Level 10
*****


View Profile WWW
« Reply #9 on: August 19, 2010, 06:01:10 AM »

@jmp and Pishtaco : aah, i see, well i guess my red method is still flawed then, thanks for pointing that out , i was under the impression that cross that appeared over the axes was a side effect of the algorithm, thanks for that link, i'll have a go later.

Maybe you're choosing a different random distance for the x and the y coordinates, rather than using the same one for both?
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic