Advertisement

Random point in circle...

Started by December 26, 2001 05:20 PM
40 comments, last by VanKurt 23 years, 1 month ago
Drawing 5000 points in a circle of radius 80.

From left to right:

1) The angle/magnitude method
2) Null & Void''s method
3) The box method
4) float Mag = sqrt(((float)rand())/(float)RAND_MAX)*radius;



Take your pick, folks.
Beer Hunter''s fourth one was what I was aiming for and failed to acheive .

[Resist Windows XP''s Invasive Production Activation Technology!]
Advertisement
I''m not up on all the mathmetics, but i got and idea and maybe it was already there but here''s what i thought of. find a random angle between 0-360. Find a random radius based on the total radius of the circle. Use that information to find the x/y pos.
quote:
Original post by Beer Hunter
4) float Mag = sqrt(((float)rand())/(float)RAND_MAX)*radius;



I don''t get it...

Sqrt always takes a positive input and returns a positive result...

How does that form a sphere?
A: It''s a circle, not a sphere
B: It''s just a magnitude. The angle can make that vector point towards a negative axis. In other words: You multiply by sin() and cos(). Look at the graphs of those functions. Both go below the x-axis.
AP - it''s a modification to the angle/magnitude method; that''s the new formula for the magnitude.
Advertisement
Beer Hunter, your method is simply cool! But I am wondering why it works. Would you (or anyone else) mind to explain that to me?

I do understand why the simple magnitude/angle method doesn''t work. (With smaller magnitude, an equal difference in angle will result in a smaller difference in position.)
Dirk =[Scarab]= Gerrits
With the angle/magnitude method you get a random number between 0.0 and 1.0 and then you multiply this number by the radius, to get the magnitude. The propability of getting 0.1 as a random number is equal to the chance of getting 0.2, 0.3, etc.
However, by taking the square root of such a number, you often get higher numbers (for example, the square root of 0.5 is 0.707). So the chance of getting a bigger number is increased, which results in a nice division of points.
OK, pretend we have a unit circle. The area of the inner circle of radius .1 is 1/100th of the area of the total circle. But the probability of picking a point in that inner circle is 1/10th, by the polar coordinate (sign/magnitude) method. This causes the density to be higher in the center by the pure polar coordinate method. Obviously, the probability of getting within a certain area is inversily square proportional, when we want inverse proportional.


Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
Thanks nickm and Vetinari. I think I understand it now.

[OT] HAPPY NEW YEAR!!
Dirk =[Scarab]= Gerrits

This topic is closed to new replies.

Advertisement