#### Archived

This topic is now archived and is closed to further replies.

# Random point in circle...

This topic is 6191 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Generate a random angle and magnitude, something like this:
  #include #include #include /* 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!]

##### Share on other sites
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!]

##### Share on other sites
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

##### Share on other sites
It will help you to know that N&V''s solution is usually referred to as "polar coordinates".

ld

##### Share on other sites
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.

##### Share on other sites
  #include #include #include #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

##### Share on other sites
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!]

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×