Jump to content
  • Advertisement

Archived

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

VanKurt

Random point in circle...

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
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!]

Share this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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!]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!