Jump to content
  • Advertisement
Sign in to follow this  
Thoughtless

[Solved] Random position within a circle area

This topic is 3613 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

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


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


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


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

Share this post


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


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!