# Random point in a circle?

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

## Recommended Posts

I know that this should be simple, but I'm having a brain fart... How do you find a random point inside of a circle? I basically want something like: POINT RandomCircle( POINT cSize ); Any ideas, faster would be better in this case as I'm using it for a spray can effect much like whats in MS-Paint. Thanks, Jeremy

##### Share on other sites
x = r * cos (angle)
y = r * sin (angle)

All you have to do is specify the angle randomly!!

So Let's say the the circle itself is at coordinates (x1, y1) and you want to have a random point inside it, then:

x = x1 + r * cos (rand())
y = y1 + r * sin (rand())

where r is (of course) the radius of the circle.

regards,

##### Share on other sites
Are you talking about INSIDE a circle?

You would need to know the center of the circle and the radius. Then just make sure the point is no farther away from the center than the radius.

If you are talknig about a point ON a circle then just do something like

R*cos(angle)+x
R*sin(angle)+y

Where R is the radius of the circle and x and y is the center point of the circle.

##### Share on other sites
I'm sorry, that was for drawing points on the circles borders..

If you want them inside, then you have 2 situations

We said

x = x1 + r * cos (rand())
y = y1 + r * sin (rand())

After that, do the following:

if (x > x1)
x = x1 - (rand() % r)
else
x = x1 + (rand() % r)

if (y > y1)
y = y1 - (rand() % r)
else
y = y1 + (rand() % r)

That's a method.. I feel there is a much easier method, but my head is blocked to. Actually, my head was fed up with work, and so I joined GameDev to have some rest..

##### Share on other sites
Quote:
 Original post by Ali_Bx = r * cos (angle)y = r * sin (angle)All you have to do is specify the angle randomly!!So Let's say the the circle itself is at coordinates (x1, y1) and you want to have a random point inside it, then:x = x1 + r * cos (rand())y = y1 + r * sin (rand())where r is (of course) the radius of the circle.regards,

This will give a random point in the circle but the distribution will be more dense nearer the center of the circle.

One way to get a very uniform distribution is to sample randomly from a square centered on the circle's center and then test that the point is within the circle, if not try again.

As a speedup for your case you can precompute a collection of points that is the ratio of the square's area to the circle's area as the number of points you desire to have and then pick that many points and just ignore the ones that aren't chosen (this prevents the case that a very unlucky random number stream can cause this method to always pick a point outside the circle and never complete).

##### Share on other sites
this should do it,

angle = random(); // between 0 and 360 (degrees)

##### Share on other sites
Quote:
 Original post by murali_13_99radius = random(); //between 0 and the radius of the circleangle = random(); // between 0 and 360 (degrees)x = center + radius*cos(angle);y = center + radius*sin(angle);

Once again : uneven distribution.

##### Share on other sites
Quote:
 Original post by jdarlingAny ideas, faster would be better in this case as I'm using it for a spray can effect much like whats in MS-Paint.
Quote:
 Original post by ebnfThis will give a random point in the circle but the distribution will be more dense nearer the center of the circle.

If you're using it for the airbrush, don't you want the distribution to be more dense near the center?

##### Share on other sites
r = random value between 0 and 1
a = random value between 0 and 2*pi

float x = cosf(a) * R;
float y = sinf(a) * R;

##### Share on other sites
Thanks everyone for your comments. After a bit of playing with the numbers I found the following to generate quite a nice look:

procedure TSprayCan.MouseHandler(X, Y: Integer; LeftButtonDown,
RightButtonDown: Boolean);
var
SpraySize : Integer;
AColor : TColor;
A, Xo, R,
Yo, Xp, Yp: Integer;
begin
if (LeftButtonDown or RightButtonDown) and
((LastX <> X) or (LastY <> Y)) then
begin
AColor := clBlack;
if LeftButtonDown then
AColor := LeftColor;
if RightButtonDown then
AColor := RightColor;

SpraySize := seSpraySize.Value;

Xo:=SpraySize Div 2;
Yo:=Xo;
R:=Yo;

For A:=0 to R Do
Begin
Xp :=Round(Random(R)*Sin(Random(360)/45));
Yp :=Round(Random(R)*Cos(Random(360)/45));

ImageSetPixel(XP + X, YP + Y, AColor);
End;

LastX := X;
LastY := Y;

ImageChanged;
end;
end;

Sorry but the source is in Delphi.

Jeremy

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11