Advertisement

Random point in circle...

Started by December 26, 2001 05:20 PM
40 comments, last by VanKurt 23 years, 1 month ago
Here''s a really simple (2D) one: -> How do I get a random coordinate (X|Y) that lies in a circle ? (I know the middle-point of the circle and it''s radius) Any suggestions ? ;-) thx, VK
If you are using an API to draw the circle, you won''t be able to do this.

Write a Circle Fill Algorythm and record each point that is filled in an array. Then index into the array using a random number from 0 to the size of the array - 1. Fill algorythms (bresenham, etc.) are not easy to understand, and may take a few days of studying how they work. Once you understand them though you can do a whole lot of things you didn''t think were possible in real-time

Code comment of the week:
bool hack = false; // DO NOT CHANGE THIS EVER
- Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com
Advertisement
This is more difficult than it looks.

If you take a random y-coordinate, then take a random x-coordinate that lies within the circle, then you are more likely to get a spot on the top edge than in the center.

If you take a random angle and a random distance, then you are more likely to get a spot near the center than near any same-sized area at the edge.

The only truly random way I can think of is to take a random point within the enclosing square, check if it''s within the circle, and if it''s not, take another random point.

Depends on how random you want it to be.
Generate a random angle and magnitude, something like this:
  #include <stdlib.h>#include <time.h>#include <math.h>/* Call this somewhere */srand(time(NULL));/* Later on (Angle is in radians) */int X, Y;double Angle, Mag;Angle = (rand()/RAND_MAX)*6.283185307;Mag = (rand()/RAND_MAX)*RadiusOfCircle;X = (int)(cos(Angle) * Mag);Y = (int)(sin(Angle) * Mag);  

That way the X and Y should always be in the circle.

[Resist Windows XP''s Invasive Production Activation Technology!]
quote:
Original post by Beer Hunter
The only truly random way I can think of is to take a random point within the enclosing square, check if it''s within the circle, and if it''s not, take another random point.

The problem with that is if every point is outside of the circle, the algorithm will take an infinite time to complete. This is unlikely, yet possible, so you have to worry about it.

[Resist Windows XP''s Invasive Production Activation Technology!]
Null and Void - are you thinking of a circle of radius zero? Change my suggestion to "within or on the circle" and all will be well
Advertisement
It will help you to know that N&V''s solution is usually referred to as "polar coordinates".

ld
No Excuses
I''m on vacation, so I don''t have my books with me, but I believe this one may have been covered in one of the Graphics Gems books.
    #include <stdlib.h>#include <time.h>#include <math.h>#define PIOVER180 (3.14156/180)...time_t t;srand((unsigned int)time(&t));...float angle = float(rand()%360);float x = (float)sin(angle*PIOVER180) * radius;float y = (float)cos(angle*PIOVER180) * radius;...    


There. A little bit easier to read.

Later,
ZE.

Edited by - ZealousElixir on December 27, 2001 10:17:43 PM

[twitter]warrenm[/twitter]

ZealousElixir, your version only finds a point on the outside of the circle (not any point within the circle). Also, yours will be less random due to your use of the low order bits (you''re using modulo instead of division to cap the random value).

Beer Hunter, what I mean was: what if every x returned is 0 and every 0 returned is 0? The point 0,0 isn''t going to be in the circle. Well, that isn''t very likely, I know. But, there''s someodd other points that aren''t in the circle either. So, what if every other value is rejected because of that? The algorithm could take a very long time to finally pick a point inside the circle if you''re unlucky. If you''re really unlucky, the algorithm could never complete !

[Resist Windows XP''s Invasive Production Activation Technology!]

This topic is closed to new replies.

Advertisement