I'm trying to generate a nebula cloud in a specified radius around a Vector2
(Point).
Currently I'm using this code
public static Vector2 GeneratePosition(Vector2 position, float radius)
{
float X = GenerateScaleFloat() * (GenerateInt(2) == 1 ? 1 : -1);
float Y = GenerateInt((int)(radius * -(1 - Math.Abs(X))),
(int)(radius * (1 - Math.Abs(X)))) / radius;
X *= radius;
Y *= radius;
return new Vector2(X, Y);
}
which gives me this result:
(Origin: 0;0
, Radius: 300
, Particles: 5000
)
It looks to me like my code is generating a rotated square. How can I make it generate the positions of the particles in (and within) a more circular pattern?
GenerateScaleFloat: Returns a value between 0.0 and 1.0
GenerateInt: The standard rand.Next
EDIT: This post has now been flagged as duplicate, which was not my intention. I will however leave my answer here for other googlers to find since I know how helpful it is:
New code:
public static Vector2 GeneratePosition(Vector2 position, float radius)
{
float X = GenerateScaleFloat() * (GenerateInt(2) == 1 ? 1 : -1) * radius;
float Y = float.PositiveInfinity;
while(Y > Math.Sqrt(Math.Pow(radius, 2) - Math.Pow(X, 2))
|| Y < -Math.Sqrt(Math.Pow(radius, 2) - Math.Pow(X, 2)))
{
Y = GenerateScaleFloat() * (GenerateInt(2) == 1 ? 1 : -1) * radius;
}
return new Vector2(X, Y) + position;
}
New output: (Origin: 0;0, Radius 100, Particles: 5000)
Hope it'll help somebody else