Added InverseSqrtFast. Vector2.LengthFast uses this to approximate the magnitude.
This commit is contained in:
parent
b675c7e621
commit
1d8e5545da
2 changed files with 22 additions and 3 deletions
|
@ -15,9 +15,28 @@ namespace OpenTK.Math
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Functions
|
public static class Functions
|
||||||
{
|
{
|
||||||
public static float SqrtFast(float p)
|
/// <summary>
|
||||||
|
/// Returns an approximation of the inverse square root of a number.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">A number.</param>
|
||||||
|
/// <returns>An approximation of the inverse square root of the specified number, with an upper error bound of 0.001</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// This is an improved implementation of the the method known as Carmack's inverse square root
|
||||||
|
/// which is found in the Quake III source code. This implementation comes from
|
||||||
|
/// http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see
|
||||||
|
/// http://www.beyond3d.com/content/articles/8/
|
||||||
|
/// </remarks>
|
||||||
|
public static float InverseSqrtFast(float x)
|
||||||
{
|
{
|
||||||
throw new Exception("The method or operation is not implemented.");
|
unsafe
|
||||||
|
{
|
||||||
|
float xhalf = 0.5f * x;
|
||||||
|
int i = *(int*)&x; // Read bits as integer.
|
||||||
|
i = 0x5f375a86 - (i >> 1); // Make an initial guess for Newton-Raphson approximation
|
||||||
|
x = *(float*)&i; // Convert bits back to float
|
||||||
|
x = x * (1.5f - xhalf * x * x); // Perform a single Newton-Raphson step.
|
||||||
|
return x;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -160,7 +160,7 @@ namespace OpenTK.Math
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return OpenTK.Math.Functions.SqrtFast(X * X + Y * Y);
|
return OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue