forked from MoonsideGames/MoonWorks
remove non-static Normalize + return identity on zero vector
parent
80f3711f4c
commit
8209051a3c
|
@ -996,8 +996,8 @@ namespace MoonWorks.Math.Fixed
|
||||||
z = Vector3.Normalize(forward);
|
z = Vector3.Normalize(forward);
|
||||||
Vector3.Cross(ref forward, ref up, out x);
|
Vector3.Cross(ref forward, ref up, out x);
|
||||||
Vector3.Cross(ref x, ref forward, out y);
|
Vector3.Cross(ref x, ref forward, out y);
|
||||||
x.Normalize();
|
x = Vector3.Normalize(x);
|
||||||
y.Normalize();
|
y = Vector3.Normalize(y);
|
||||||
|
|
||||||
result = new Matrix4x4();
|
result = new Matrix4x4();
|
||||||
result.Right = x;
|
result.Right = x;
|
||||||
|
|
|
@ -214,23 +214,6 @@ namespace MoonWorks.Math.Fixed
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Scales the quaternion magnitude to unit length.
|
|
||||||
/// </summary>
|
|
||||||
public void Normalize()
|
|
||||||
{
|
|
||||||
Fix64 num = Fix64.One / (Fix64.Sqrt(
|
|
||||||
(X * X) +
|
|
||||||
(Y * Y) +
|
|
||||||
(Z * Z) +
|
|
||||||
(W * W)
|
|
||||||
));
|
|
||||||
this.X *= num;
|
|
||||||
this.Y *= num;
|
|
||||||
this.Z *= num;
|
|
||||||
this.W *= num;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a <see cref="String"/> representation of this <see cref="Quaternion"/> in the format:
|
/// Returns a <see cref="String"/> representation of this <see cref="Quaternion"/> in the format:
|
||||||
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>] W:[<see cref="W"/>]}
|
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>] W:[<see cref="W"/>]}
|
||||||
|
@ -759,12 +742,16 @@ namespace MoonWorks.Math.Fixed
|
||||||
/// <param name="result">The unit length quaternion an output parameter.</param>
|
/// <param name="result">The unit length quaternion an output parameter.</param>
|
||||||
public static void Normalize(ref Quaternion quaternion, out Quaternion result)
|
public static void Normalize(ref Quaternion quaternion, out Quaternion result)
|
||||||
{
|
{
|
||||||
Fix64 num = Fix64.One / (Fix64.Sqrt(
|
Fix64 lengthSquared = (quaternion.X * quaternion.X) + (quaternion.Y * quaternion.Y) +
|
||||||
(quaternion.X * quaternion.X) +
|
(quaternion.Z * quaternion.Z) + (quaternion.W * quaternion.W);
|
||||||
(quaternion.Y * quaternion.Y) +
|
|
||||||
(quaternion.Z * quaternion.Z) +
|
if (lengthSquared == Fix64.Zero)
|
||||||
(quaternion.W * quaternion.W)
|
{
|
||||||
));
|
result = Identity;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fix64 num = Fix64.One / Fix64.Sqrt(lengthSquared);
|
||||||
result.X = quaternion.X * num;
|
result.X = quaternion.X * num;
|
||||||
result.Y = quaternion.Y * num;
|
result.Y = quaternion.Y * num;
|
||||||
result.Z = quaternion.Z * num;
|
result.Z = quaternion.Z * num;
|
||||||
|
|
|
@ -1,105 +0,0 @@
|
||||||
namespace MoonWorks.Math.Fixed
|
|
||||||
{
|
|
||||||
public struct Transform2D : System.IEquatable<Transform2D>
|
|
||||||
{
|
|
||||||
public Vector2 Position { get; }
|
|
||||||
public Fix64 Rotation { get; }
|
|
||||||
public Vector2 Scale { get; }
|
|
||||||
|
|
||||||
private bool transformMatrixCalculated;
|
|
||||||
private Matrix3x2 transformMatrix;
|
|
||||||
|
|
||||||
public Matrix3x2 TransformMatrix
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (!transformMatrixCalculated)
|
|
||||||
{
|
|
||||||
transformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
|
|
||||||
transformMatrixCalculated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return transformMatrix;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsAxisAligned => Rotation % Fix64.PiOver2 == Fix64.Zero;
|
|
||||||
public bool IsUniformScale => Scale.X == Scale.Y || Scale.X == -Scale.Y;
|
|
||||||
|
|
||||||
public static readonly Transform2D Identity = new Transform2D(Vector2.Zero, Fix64.Zero, Vector2.One);
|
|
||||||
|
|
||||||
public Transform2D(Vector2 position)
|
|
||||||
{
|
|
||||||
Position = position;
|
|
||||||
Rotation = Fix64.Zero;
|
|
||||||
Scale = Vector2.One;
|
|
||||||
transformMatrixCalculated = false;
|
|
||||||
transformMatrix = Matrix3x2.Identity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transform2D(Vector2 position, Fix64 rotation)
|
|
||||||
{
|
|
||||||
Position = position;
|
|
||||||
Rotation = rotation;
|
|
||||||
Scale = Vector2.One;
|
|
||||||
transformMatrixCalculated = false;
|
|
||||||
transformMatrix = Matrix3x2.Identity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transform2D(Vector2 position, Fix64 rotation, Vector2 scale)
|
|
||||||
{
|
|
||||||
Position = position;
|
|
||||||
Rotation = rotation;
|
|
||||||
Scale = scale;
|
|
||||||
transformMatrixCalculated = false;
|
|
||||||
transformMatrix = Matrix3x2.Identity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transform2D Compose(Transform2D other)
|
|
||||||
{
|
|
||||||
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Matrix3x2 CreateTransformMatrix(Vector2 position, Fix64 rotation, Vector2 scale)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
Matrix3x2.CreateScale(scale) *
|
|
||||||
Matrix3x2.CreateRotation(rotation) *
|
|
||||||
Matrix3x2.CreateTranslation(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Equals(Transform2D other)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
Position == other.Position &&
|
|
||||||
Rotation == other.Rotation &&
|
|
||||||
Scale == other.Scale;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public override bool Equals(System.Object other)
|
|
||||||
{
|
|
||||||
if (other is Transform2D otherTransform)
|
|
||||||
{
|
|
||||||
return Equals(otherTransform);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return System.HashCode.Combine(Position, Rotation, Scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator ==(Transform2D a, Transform2D b)
|
|
||||||
{
|
|
||||||
return a.Equals(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator !=(Transform2D a, Transform2D b)
|
|
||||||
{
|
|
||||||
return !a.Equals(b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -200,16 +200,6 @@ namespace MoonWorks.Math.Fixed
|
||||||
return (X * X) + (Y * Y);
|
return (X * X) + (Y * Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Turns this <see cref="Vector2"/> to a unit vector with the same direction.
|
|
||||||
/// </summary>
|
|
||||||
public void Normalize()
|
|
||||||
{
|
|
||||||
Fix64 val = Fix64.One / Fix64.Sqrt((X * X) + (Y * Y));
|
|
||||||
X *= val;
|
|
||||||
Y *= val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Turns this <see cref="Vector2"/> to an angle in radians.
|
/// Turns this <see cref="Vector2"/> to an angle in radians.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -423,7 +413,14 @@ namespace MoonWorks.Math.Fixed
|
||||||
/// <returns>Unit vector.</returns>
|
/// <returns>Unit vector.</returns>
|
||||||
public static Vector2 Normalize(Vector2 value)
|
public static Vector2 Normalize(Vector2 value)
|
||||||
{
|
{
|
||||||
Fix64 val = Fix64.One / Fix64.Sqrt((value.X * value.X) + (value.Y * value.Y));
|
Fix64 lengthSquared = (value.X * value.X) + (value.Y * value.Y);
|
||||||
|
|
||||||
|
if (lengthSquared == Fix64.Zero)
|
||||||
|
{
|
||||||
|
return Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fix64 val = Fix64.One / Fix64.Sqrt(lengthSquared);
|
||||||
value.X *= val;
|
value.X *= val;
|
||||||
value.Y *= val;
|
value.Y *= val;
|
||||||
return value;
|
return value;
|
||||||
|
|
|
@ -309,21 +309,6 @@ namespace MoonWorks.Math.Fixed
|
||||||
return (X * X) + (Y * Y) + (Z * Z);
|
return (X * X) + (Y * Y) + (Z * Z);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Turns this <see cref="Vector3"/> to a unit vector with the same direction.
|
|
||||||
/// </summary>
|
|
||||||
public void Normalize()
|
|
||||||
{
|
|
||||||
Fix64 factor = Fix64.One / Fix64.Sqrt(
|
|
||||||
(X * X) +
|
|
||||||
(Y * Y) +
|
|
||||||
(Z * Z)
|
|
||||||
);
|
|
||||||
X *= factor;
|
|
||||||
Y *= factor;
|
|
||||||
Z *= factor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a <see cref="String"/> representation of this <see cref="Vector3"/> in the format:
|
/// Returns a <see cref="String"/> representation of this <see cref="Vector3"/> in the format:
|
||||||
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>]}
|
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>]}
|
||||||
|
@ -733,11 +718,14 @@ namespace MoonWorks.Math.Fixed
|
||||||
/// <returns>Unit vector.</returns>
|
/// <returns>Unit vector.</returns>
|
||||||
public static Vector3 Normalize(Vector3 value)
|
public static Vector3 Normalize(Vector3 value)
|
||||||
{
|
{
|
||||||
Fix64 factor = Fix64.One / Fix64.Sqrt(
|
Fix64 lengthSquared = (value.X * value.X) + (value.Y * value.Y) + (value.Z * value.Z);
|
||||||
(value.X * value.X) +
|
|
||||||
(value.Y * value.Y) +
|
if (lengthSquared == Fix64.Zero)
|
||||||
(value.Z * value.Z)
|
{
|
||||||
);
|
return Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fix64 factor = Fix64.One / Fix64.Sqrt(lengthSquared);
|
||||||
return new Vector3(
|
return new Vector3(
|
||||||
value.X * factor,
|
value.X * factor,
|
||||||
value.Y * factor,
|
value.Y * factor,
|
||||||
|
|
|
@ -611,7 +611,7 @@ namespace MoonWorks.Math.Float
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Vector3.Cross(ref cameraUpVector, ref vector, out vector3);
|
Vector3.Cross(ref cameraUpVector, ref vector, out vector3);
|
||||||
vector3.Normalize();
|
vector3 = Vector3.Normalize(vector3);
|
||||||
Vector3.Cross(ref vector, ref vector3, out vector2);
|
Vector3.Cross(ref vector, ref vector3, out vector2);
|
||||||
result.M11 = vector3.X;
|
result.M11 = vector3.X;
|
||||||
result.M12 = vector3.Y;
|
result.M12 = vector3.Y;
|
||||||
|
@ -730,16 +730,16 @@ namespace MoonWorks.Math.Float
|
||||||
Vector3.Forward;
|
Vector3.Forward;
|
||||||
}
|
}
|
||||||
Vector3.Cross(ref rotateAxis, ref vector, out vector3);
|
Vector3.Cross(ref rotateAxis, ref vector, out vector3);
|
||||||
vector3.Normalize();
|
vector3 = Vector3.Normalize(vector3);
|
||||||
Vector3.Cross(ref vector3, ref rotateAxis, out vector);
|
Vector3.Cross(ref vector3, ref rotateAxis, out vector);
|
||||||
vector.Normalize();
|
vector = Vector3.Normalize(vector);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Vector3.Cross(ref rotateAxis, ref vector2, out vector3);
|
Vector3.Cross(ref rotateAxis, ref vector2, out vector3);
|
||||||
vector3.Normalize();
|
vector3 = Vector3.Normalize(vector3);
|
||||||
Vector3.Cross(ref vector3, ref vector4, out vector);
|
Vector3.Cross(ref vector3, ref vector4, out vector);
|
||||||
vector.Normalize();
|
vector = Vector3.Normalize(vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
result.M11 = vector3.X;
|
result.M11 = vector3.X;
|
||||||
|
@ -1701,8 +1701,8 @@ namespace MoonWorks.Math.Float
|
||||||
Vector3.Normalize(ref forward, out z);
|
Vector3.Normalize(ref forward, out z);
|
||||||
Vector3.Cross(ref forward, ref up, out x);
|
Vector3.Cross(ref forward, ref up, out x);
|
||||||
Vector3.Cross(ref x, ref forward, out y);
|
Vector3.Cross(ref x, ref forward, out y);
|
||||||
x.Normalize();
|
x = Vector3.Normalize(x);
|
||||||
y.Normalize();
|
y = Vector3.Normalize(y);
|
||||||
|
|
||||||
result = new Matrix4x4();
|
result = new Matrix4x4();
|
||||||
result.Right = x;
|
result.Right = x;
|
||||||
|
|
|
@ -1,105 +0,0 @@
|
||||||
namespace MoonWorks.Math.Float
|
|
||||||
{
|
|
||||||
public struct Transform2D : System.IEquatable<Transform2D>
|
|
||||||
{
|
|
||||||
public Vector2 Position { get; }
|
|
||||||
public float Rotation { get; }
|
|
||||||
public Vector2 Scale { get; }
|
|
||||||
|
|
||||||
private bool transformMatrixCalculated;
|
|
||||||
private Matrix3x2 transformMatrix;
|
|
||||||
|
|
||||||
public Matrix3x2 TransformMatrix
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (!transformMatrixCalculated)
|
|
||||||
{
|
|
||||||
transformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
|
|
||||||
transformMatrixCalculated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return transformMatrix;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsAxisAligned => Rotation % MathHelper.PiOver2 == 0;
|
|
||||||
public bool IsUniformScale => Scale.X == Scale.Y;
|
|
||||||
|
|
||||||
public static readonly Transform2D Identity = new Transform2D(Vector2.Zero, 0, Vector2.One);
|
|
||||||
|
|
||||||
public Transform2D(Vector2 position)
|
|
||||||
{
|
|
||||||
Position = position;
|
|
||||||
Rotation = 0;
|
|
||||||
Scale = Vector2.One;
|
|
||||||
transformMatrixCalculated = false;
|
|
||||||
transformMatrix = Matrix3x2.Identity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transform2D(Vector2 position, float rotation)
|
|
||||||
{
|
|
||||||
Position = position;
|
|
||||||
Rotation = rotation;
|
|
||||||
Scale = Vector2.One;
|
|
||||||
transformMatrixCalculated = false;
|
|
||||||
transformMatrix = Matrix3x2.Identity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transform2D(Vector2 position, float rotation, Vector2 scale)
|
|
||||||
{
|
|
||||||
Position = position;
|
|
||||||
Rotation = rotation;
|
|
||||||
Scale = scale;
|
|
||||||
transformMatrixCalculated = false;
|
|
||||||
transformMatrix = Matrix3x2.Identity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transform2D Compose(Transform2D other)
|
|
||||||
{
|
|
||||||
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Matrix3x2 CreateTransformMatrix(Vector2 position, float rotation, Vector2 scale)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
Matrix3x2.CreateScale(scale) *
|
|
||||||
Matrix3x2.CreateRotation(rotation) *
|
|
||||||
Matrix3x2.CreateTranslation(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Equals(Transform2D other)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
Position == other.Position &&
|
|
||||||
Rotation == other.Rotation &&
|
|
||||||
Scale == other.Scale;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public override bool Equals(System.Object other)
|
|
||||||
{
|
|
||||||
if (other is Transform2D otherTransform)
|
|
||||||
{
|
|
||||||
return Equals(otherTransform);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return System.HashCode.Combine(Position, Rotation, Scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator ==(Transform2D a, Transform2D b)
|
|
||||||
{
|
|
||||||
return a.Equals(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator !=(Transform2D a, Transform2D b)
|
|
||||||
{
|
|
||||||
return !a.Equals(b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -194,16 +194,6 @@ namespace MoonWorks.Math.Float
|
||||||
return (X * X) + (Y * Y);
|
return (X * X) + (Y * Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Turns this <see cref="Vector2"/> to a unit vector with the same direction.
|
|
||||||
/// </summary>
|
|
||||||
public void Normalize()
|
|
||||||
{
|
|
||||||
float val = 1.0f / (float) System.Math.Sqrt((X * X) + (Y * Y));
|
|
||||||
X *= val;
|
|
||||||
Y *= val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Turns this <see cref="Vector2"/> to an angle in radians.
|
/// Turns this <see cref="Vector2"/> to an angle in radians.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -717,7 +707,14 @@ namespace MoonWorks.Math.Float
|
||||||
/// <returns>Unit vector.</returns>
|
/// <returns>Unit vector.</returns>
|
||||||
public static Vector2 Normalize(Vector2 value)
|
public static Vector2 Normalize(Vector2 value)
|
||||||
{
|
{
|
||||||
float val = 1.0f / (float) System.Math.Sqrt((value.X * value.X) + (value.Y * value.Y));
|
float lengthSquared = (value.X * value.X) + (value.Y * value.Y);
|
||||||
|
|
||||||
|
if (lengthSquared == 0)
|
||||||
|
{
|
||||||
|
return Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
float val = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||||
value.X *= val;
|
value.X *= val;
|
||||||
value.Y *= val;
|
value.Y *= val;
|
||||||
return value;
|
return value;
|
||||||
|
|
|
@ -302,21 +302,6 @@ namespace MoonWorks.Math.Float
|
||||||
return (X * X) + (Y * Y) + (Z * Z);
|
return (X * X) + (Y * Y) + (Z * Z);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Turns this <see cref="Vector3"/> to a unit vector with the same direction.
|
|
||||||
/// </summary>
|
|
||||||
public void Normalize()
|
|
||||||
{
|
|
||||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
|
||||||
(X * X) +
|
|
||||||
(Y * Y) +
|
|
||||||
(Z * Z)
|
|
||||||
);
|
|
||||||
X *= factor;
|
|
||||||
Y *= factor;
|
|
||||||
Z *= factor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a <see cref="String"/> representation of this <see cref="Vector3"/> in the format:
|
/// Returns a <see cref="String"/> representation of this <see cref="Vector3"/> in the format:
|
||||||
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>]}
|
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>]}
|
||||||
|
@ -900,11 +885,14 @@ namespace MoonWorks.Math.Float
|
||||||
/// <returns>Unit vector.</returns>
|
/// <returns>Unit vector.</returns>
|
||||||
public static Vector3 Normalize(Vector3 value)
|
public static Vector3 Normalize(Vector3 value)
|
||||||
{
|
{
|
||||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
float lengthSquared = (value.X * value.X) + (value.Y * value.Y) + (value.Z * value.Z);
|
||||||
(value.X * value.X) +
|
|
||||||
(value.Y * value.Y) +
|
if (lengthSquared == 0f)
|
||||||
(value.Z * value.Z)
|
{
|
||||||
);
|
return Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
float factor = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||||
return new Vector3(
|
return new Vector3(
|
||||||
value.X * factor,
|
value.X * factor,
|
||||||
value.Y * factor,
|
value.Y * factor,
|
||||||
|
|
|
@ -267,23 +267,6 @@ namespace MoonWorks.Math.Float
|
||||||
return (X * X) + (Y * Y) + (Z * Z) + (W * W);
|
return (X * X) + (Y * Y) + (Z * Z) + (W * W);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Turns this <see cref="Vector4"/> to a unit vector with the same direction.
|
|
||||||
/// </summary>
|
|
||||||
public void Normalize()
|
|
||||||
{
|
|
||||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
|
||||||
(X * X) +
|
|
||||||
(Y * Y) +
|
|
||||||
(Z * Z) +
|
|
||||||
(W * W)
|
|
||||||
);
|
|
||||||
X *= factor;
|
|
||||||
Y *= factor;
|
|
||||||
Z *= factor;
|
|
||||||
W *= factor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
|
@ -853,12 +836,15 @@ namespace MoonWorks.Math.Float
|
||||||
/// <returns>Unit vector.</returns>
|
/// <returns>Unit vector.</returns>
|
||||||
public static Vector4 Normalize(Vector4 vector)
|
public static Vector4 Normalize(Vector4 vector)
|
||||||
{
|
{
|
||||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
var lengthSquared = (vector.X * vector.X) + (vector.Y * vector.Y) +
|
||||||
(vector.X * vector.X) +
|
(vector.Z * vector.Z) + (vector.W * vector.W);
|
||||||
(vector.Y * vector.Y) +
|
|
||||||
(vector.Z * vector.Z) +
|
if (lengthSquared == 0)
|
||||||
(vector.W * vector.W)
|
{
|
||||||
);
|
return Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
float factor = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||||
return new Vector4(
|
return new Vector4(
|
||||||
vector.X * factor,
|
vector.X * factor,
|
||||||
vector.Y * factor,
|
vector.Y * factor,
|
||||||
|
@ -870,16 +856,20 @@ namespace MoonWorks.Math.Float
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new <see cref="Vector4"/> that contains a normalized values from another vector.
|
/// Creates a new <see cref="Vector4"/> that contains a normalized values from another vector.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">Source <see cref="Vector4"/>.</param>
|
/// <param name="vector">Source <see cref="Vector4"/>.</param>
|
||||||
/// <param name="result">Unit vector as an output parameter.</param>
|
/// <param name="result">Unit vector as an output parameter.</param>
|
||||||
public static void Normalize(ref Vector4 vector, out Vector4 result)
|
public static void Normalize(ref Vector4 vector, out Vector4 result)
|
||||||
{
|
{
|
||||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
float lengthSquared = (vector.X * vector.X) + (vector.Y * vector.Y) +
|
||||||
(vector.X * vector.X) +
|
(vector.Z * vector.Z) + (vector.W * vector.W);
|
||||||
(vector.Y * vector.Y) +
|
|
||||||
(vector.Z * vector.Z) +
|
if (lengthSquared == 0)
|
||||||
(vector.W * vector.W)
|
{
|
||||||
);
|
result = Zero;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float factor = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||||
result.X = vector.X * factor;
|
result.X = vector.X * factor;
|
||||||
result.Y = vector.Y * factor;
|
result.Y = vector.Y * factor;
|
||||||
result.Z = vector.Z * factor;
|
result.Z = vector.Z * factor;
|
||||||
|
|
Loading…
Reference in New Issue