Showing posts with label Random Point. Show all posts
Showing posts with label Random Point. Show all posts

Tuesday, May 11, 2010

XNA Random Point On Plane

This method generates random points in a circle with a specified radius. The circle lies on a plane defined with a position and a normal. The generated points have an uniform distribution.

Random _random = new Random();

private Vector3 RandomPointOnPlane(Vector3 position, Vector3 normal, float radius)
{
Vector3 random = new Vector3();
Vector3 randomPoint;

do
{
random.X = 2.0f * (float)_random.NextDouble() - 1.0f;
random.Y = 2.0f * (float)_random.NextDouble() - 1.0f;
random.Z = 2.0f * (float)_random.NextDouble() - 1.0f;
Vector3.Cross(ref random, ref normal, out randomPoint);
} while (randomPoint == Vector3.Zero);

randomPoint.Normalize();
randomPoint *= radius * (float)Math.Sqrt(_random.NextDouble());
randomPoint += position;

return randomPoint;
}

Monday, May 10, 2010

XNA Random Point In Circle

The function creates a random point in a circle on the XZ plane.

Random _random = new Random();

private Vector3 RandomPointInCircle(Vector3 position, float radius)
{
float randomRadius = radius * (float)Math.Sqrt(_random.NextDouble());

double randomAngle = _random.NextDouble() * MathHelper.TwoPi;

float x = randomRadius * (float)Math.Cos(randomAngle);
float z = randomRadius * (float)Math.Sin(randomAngle);

return new Vector3(position.X + x, position.Y, position.Z + z);
}