[Solved] Random position within a circle area

Started by
4 comments, last by Thoughtless 15 years, 8 months ago
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]
Advertisement
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
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.
Quote:Original post by Sc4Freak
Generage 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 Guntharpo
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.

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.
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.
Quote:Original post by Lawgiver
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.


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:

This topic is closed to new replies.

Advertisement