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;

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 *= radius * (float)Math.Sqrt(_random.NextDouble());
randomPoint += position;

return randomPoint;