mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 22:55:42 +00:00
(#35) Add Random.NextRotationUniform
This commit is contained in:
parent
cb7673c9a4
commit
a32fe8cc4a
@ -33,6 +33,37 @@ namespace X10D.RandomExtensions
|
||||
return Quaternion.CreateFromYawPitchRoll(y, x, z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a randomly generated rotation with uniform distribution.
|
||||
/// </summary>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>A <see cref="Quaternion" /> constructed with uniform distribution.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
|
||||
public static Quaternion NextRotationUniform(this Random random)
|
||||
{
|
||||
if (random is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(random));
|
||||
}
|
||||
|
||||
var seed = random.Next();
|
||||
var seededRandom = new Random(seed);
|
||||
float normal, w, x, y, z;
|
||||
|
||||
do
|
||||
{
|
||||
w = seededRandom.NextSingle(-1f, 1f);
|
||||
x = seededRandom.NextSingle(-1f, 1f);
|
||||
y = seededRandom.NextSingle(-1f, 1f);
|
||||
z = seededRandom.NextSingle(-1f, 1f);
|
||||
normal = w * w + x * x + y * y + z * z;
|
||||
}
|
||||
while (normal > 1f || normal == 0f);
|
||||
|
||||
normal = MathF.Sqrt(normal);
|
||||
return new Quaternion(x / normal, y / normal, z / normal, w / normal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="Vector2" /> with magnitude 1 whose components indicate a random point on the unit circle.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user