# [Solved] Random position within a circle area

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

## Recommended Posts

I just created a 2d particle system and am now trying to add different ways to position the particles on creation. At the moment I only have 1 implemented, rectangle. And am now trying to add oval. Here is the rectangle one just for clarification:
int newX, newY;
newX = (int)(x - placementWidth / 2 + rand.Next(0, placementWidth));
newY = (int)(y - placementHeight / 2 + rand.Next(0, placementHeight));


Now I'm trying to implement the oval one, and I'm not too flash at trigonometry. So basically what I need to make is either: - A while loop that makes random x,y pairs and checks that they are within oval (horizontal radius = placementWidth/2, vertical radius = placementHeight/2). - Or if possible an algorithm that guarantees it will be a position within the circle using 2 random numbers, the problem I see with this is that if it is possible it will not give an equal chance to land in any spot. I wouldn't have any problems changing oval from circle is 2 radius' are too difficult. [Edited by - Thoughtless on August 3, 2008 5:25:50 PM]

##### Share on other sites
Generage a random angle, and a random length. That'll give you a random point in a circle.

If you want to make it into an oval, just stretch the X or Y axes.

eg. (You didn't mention what language you're using, so I'll assume C#, which is easily convertible into C++ or Java or whatever it is you're using)
double width = 1.5; // For exampledouble height = 1.0;double theta = rand.NextDouble() * Math.PI * 2;double length = rand.NextDouble();double x = Math.Cos(theta) * width * length;double y = Math.Sin(theta) * height * length;

The code is untested and uncompiled, but I think it should work.

##### Share on other sites
As above, but you'll want to take the square root (I think) of the random length to get a uniform distribution over the circle's area.

Edit: obviously assuming a unit radius.

##### Share on other sites
Quote:
 Original post by Sc4FreakGenerage a random angle, and a random length. That'll give you a random point in a circle.

That'll do it :)

Quote:
 Original post by Guntharpobut you'll want to take the square root (I think) of the random length to get a uniform distribution over the circle's area.

I don't understand what your saying here with square root but I can see that as an oval it wouldn't distribute fairly.

I'm actually not going to bother with oval (just circle) because I would probably never use it :P and yeah, there are extra complications using this:
double x = Math.Cos(theta) * width * length;double y = Math.Sin(theta) * height * length;

As the 0.01 percent would mean more vertically then it would horizontally if the oval was higher then it was wide, leading in uneven distribution-ness.

Thanks for all your help, PS it is C# I'm using.

##### Share on other sites
Hello,

Even with a circle, your distribution is still going to be wrong. The average distance between points in the inner part of the circle will be smaller than the average distance between points in the outer part of the circle.

This is why, as one of the poster suggested, that you need to take the square root of random(0,1) * radius - to get the appropriate distance.

##### Share on other sites
Quote:
 Original post by LawgiverHello,Even with a circle, your distribution is still going to be wrong. The average distance between points in the inner part of the circle will be smaller than the average distance between points in the outer part of the circle.This is why, as one of the poster suggested, that you need to take the square root of random(0,1) * radius - to get the appropriate distance.

Ahhh, I get it (sort of). I just added it and it looks fine:

newX = (int)(customX + Math.Cos(theta) * (Math.Sqrt(placementWidth * length)*10));newY = (int)(customY + Math.Sin(theta) * (Math.Sqrt(placementHeight * length)*10));

The multiply by 10 compensates it so the original 'size' stays in tact.

Here it is in action 'bursting' 750 particles:

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

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

• Total Topics
633743
• Total Posts
3013644
×