#region License
/* MoonWorks - Game Development Framework
* Copyright 2022 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion
namespace MoonWorks.Math.Fixed
{
///
/// Describes a fixed point 2D-vector.
///
[Serializable]
[DebuggerDisplay("{DebugDisplayString,nq}")]
[StructLayout(LayoutKind.Explicit)]
public struct Vector2 : IEquatable
{
#region Public Static Properties
///
/// Returns a with components 0, 0.
///
public static Vector2 Zero
{
get
{
return zeroVector;
}
}
///
/// Returns a with components 1, 1.
///
public static Vector2 One
{
get
{
return unitVector;
}
}
///
/// Returns a with components 1, 0.
///
public static Vector2 UnitX
{
get
{
return unitXVector;
}
}
///
/// Returns a with components 0, 1.
///
public static Vector2 UnitY
{
get
{
return unitYVector;
}
}
#endregion
#region Internal Properties
internal string DebugDisplayString
{
get
{
return string.Concat(
X.ToString(), " ",
Y.ToString()
);
}
}
#endregion
#region Public Fields
///
/// The x coordinate of this .
///
[FieldOffset(0)]
public Fix64 X;
///
/// The y coordinate of this .
///
[FieldOffset(8)]
public Fix64 Y;
#endregion
#region Private Static Fields
private static readonly Vector2 zeroVector = new Vector2(0, 0);
private static readonly Vector2 unitVector = new Vector2(1, 1);
private static readonly Vector2 unitXVector = new Vector2(1, 0);
private static readonly Vector2 unitYVector = new Vector2(0, 1);
#endregion
#region Public Constructors
///
/// Constructs a 2d vector with X and Y from two values.
///
/// The x coordinate in 2d-space.
/// The y coordinate in 2d-space.
public Vector2(Fix64 x, Fix64 y)
{
this.X = x;
this.Y = y;
}
///
/// Constructs a 2d vector with X and Y set to the same value.
///
/// The x and y coordinates in 2d-space.
public Vector2(Fix64 value)
{
this.X = value;
this.Y = value;
}
public Vector2(int x, int y)
{
this.X = new Fix64(x);
this.Y = new Fix64(y);
}
#endregion
#region Public Methods
///
/// Compares whether current instance is equal to specified .
///
/// The to compare.
/// true if the instances are equal; false otherwise.
public override bool Equals(object obj)
{
return obj is Vector2 fixVector && Equals(fixVector);
}
///
/// Compares whether current instance is equal to specified .
///
/// The to compare.
/// true if the instances are equal; false otherwise.
public bool Equals(Vector2 other)
{
return (X == other.X &&
Y == other.Y);
}
///
/// Gets the hash code of this .
///
/// Hash code of this .
public override int GetHashCode()
{
return X.GetHashCode() + Y.GetHashCode();
}
///
/// Returns the length of this .
///
/// The length of this .
public Fix64 Length()
{
return Fix64.Sqrt((X * X) + (Y * Y));
}
///
/// Returns the squared length of this .
///
/// The squared length of this .
public Fix64 LengthSquared()
{
return (X * X) + (Y * Y);
}
///
/// Turns this to an angle in radians.
///
public Fix64 Angle()
{
return Fix64.Atan2(Y, X);
}
///
/// Returns this Vector2 with the fractional components cut off.
///
public Vector2 Truncated()
{
return new Vector2((int) X, (int) Y);
}
///
/// Creates a new that contains linear interpolation of the specified vectors.
///
/// The first vector.
/// The second vector.
/// Weighting value(between 0.0 and 1.0).
/// The result of linear interpolation of the specified vectors.
public static Vector2 Lerp(Vector2 value1, Vector2 value2, Fix64 amount)
{
return new Vector2(
Fix64.Lerp(value1.X, value2.X, amount),
Fix64.Lerp(value1.Y, value2.Y, amount)
);
}
///
/// Returns a representation of this in the format:
/// {X:[] Y:[]}
///
/// A representation of this .
public override string ToString()
{
return (
"{X:" + X.ToString() +
" Y:" + Y.ToString() +
"}"
);
}
#endregion
#region Public Static Methods
///
/// Performs vector addition on and .
///
/// The first vector to add.
/// The second vector to add.
/// The result of the vector addition.
public static Vector2 Add(Vector2 value1, Vector2 value2)
{
value1.X += value2.X;
value1.Y += value2.Y;
return value1;
}
///
/// Clamps the specified value within a range.
///
/// The value to clamp.
/// The min value.
/// The max value.
/// The clamped value.
public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max)
{
return new Vector2(
Fix64.Clamp(value1.X, min.X, max.X),
Fix64.Clamp(value1.Y, min.Y, max.Y)
);
}
///
/// Returns the distance between two vectors.
///
/// The first vector.
/// The second vector.
/// The distance between two vectors.
public static Fix64 Distance(Vector2 value1, Vector2 value2)
{
Fix64 v1 = value1.X - value2.X, v2 = value1.Y - value2.Y;
return Fix64.Sqrt((v1 * v1) + (v2 * v2));
}
///
/// Returns the squared distance between two vectors.
///
/// The first vector.
/// The second vector.
/// The squared distance between two vectors.
public static Fix64 DistanceSquared(Vector2 value1, Vector2 value2)
{
Fix64 v1 = value1.X - value2.X, v2 = value1.Y - value2.Y;
return (v1 * v1) + (v2 * v2);
}
///
/// Divides the components of a by the components of another .
///
/// Source .
/// Divisor .
/// The result of dividing the vectors.
public static Vector2 Divide(Vector2 value1, Vector2 value2)
{
value1.X /= value2.X;
value1.Y /= value2.Y;
return value1;
}
///
/// Divides the components of a by a scalar.
///
/// Source .
/// Divisor scalar.
/// The result of dividing a vector by a scalar.
public static Vector2 Divide(Vector2 value1, Fix64 divider)
{
Fix64 factor = Fix64.One / divider;
value1.X *= factor;
value1.Y *= factor;
return value1;
}
///
/// Returns a dot product of two vectors.
///
/// The first vector.
/// The second vector.
/// The dot product of two vectors.
public static Fix64 Dot(Vector2 value1, Vector2 value2)
{
return (value1.X * value2.X) + (value1.Y * value2.Y);
}
///
/// Creates a new that contains a maximal values from the two vectors.
///
/// The first vector.
/// The second vector.
/// The with maximal values from the two vectors.
public static Vector2 Max(Vector2 value1, Vector2 value2)
{
return new Vector2(
value1.X > value2.X ? value1.X : value2.X,
value1.Y > value2.Y ? value1.Y : value2.Y
);
}
///
/// Creates a new that contains a minimal values from the two vectors.
///
/// The first vector.
/// The second vector.
/// The with minimal values from the two vectors.
public static Vector2 Min(Vector2 value1, Vector2 value2)
{
return new Vector2(
value1.X < value2.X ? value1.X : value2.X,
value1.Y < value2.Y ? value1.Y : value2.Y
);
}
///
/// Creates a new that contains a multiplication of two vectors.
///
/// Source .
/// Source .
/// The result of the vector multiplication.
public static Vector2 Multiply(Vector2 value1, Vector2 value2)
{
value1.X *= value2.X;
value1.Y *= value2.Y;
return value1;
}
///
/// Creates a new that contains a multiplication of and a scalar.
///
/// Source .
/// Scalar value.
/// The result of the vector multiplication with a scalar.
public static Vector2 Multiply(Vector2 value1, Fix64 scaleFactor)
{
value1.X *= scaleFactor;
value1.Y *= scaleFactor;
return value1;
}
///
/// Creates a new that contains the specified vector inversion.
/// direction of .
///
/// Source .
/// The result of the vector inversion.
public static Vector2 Negate(Vector2 value)
{
value.X = -value.X;
value.Y = -value.Y;
return value;
}
///
/// Creates a new that contains a normalized values from another vector.
///
/// Source .
/// Unit vector.
public static Vector2 Normalize(Vector2 value)
{
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.Y *= val;
return value;
}
///
/// Creates a new that contains reflect vector of the given vector and normal.
///
/// Source .
/// Reflection normal.
/// Reflected vector.
public static Vector2 Reflect(Vector2 vector, Vector2 normal)
{
Vector2 result;
Fix64 val = new Fix64(2) * ((vector.X * normal.X) + (vector.Y * normal.Y));
result.X = vector.X - (normal.X * val);
result.Y = vector.Y - (normal.Y * val);
return result;
}
///
/// Creates a new that contains subtraction of on from a another.
///
/// Source .
/// Source .
/// The result of the vector subtraction.
public static Vector2 Subtract(Vector2 value1, Vector2 value2)
{
value1.X -= value2.X;
value1.Y -= value2.Y;
return value1;
}
///
/// Creates a new that contains a transformation of 2d-vector by the specified .
///
/// Source .
/// The transformation .
/// Transformed .
public static Vector2 Transform(Vector2 position, Matrix4x4 matrix)
{
return new Vector2(
(position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M41,
(position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M42
);
}
///
/// Creates a new that contains a transformation of 2d-vector by the specified , representing the rotation.
///
/// Source .
/// The which contains rotation transformation.
/// Transformed .
public static Vector2 Transform(Vector2 value, Quaternion rotation)
{
Transform(ref value, ref rotation, out value);
return value;
}
///
/// Creates a new that contains a transformation of 2d-vector by the specified , representing the rotation.
///
/// Source .
/// The which contains rotation transformation.
/// Transformed as an output parameter.
public static void Transform(
ref Vector2 value,
ref Quaternion rotation,
out Vector2 result
)
{
Fix64 two = new Fix64(2);
Fix64 x = two * -(rotation.Z * value.Y);
Fix64 y = two * (rotation.Z * value.X);
Fix64 z = two * (rotation.X * value.Y - rotation.Y * value.X);
result.X = value.X + x * rotation.W + (rotation.Y * z - rotation.Z * y);
result.Y = value.Y + y * rotation.W + (rotation.Z * x - rotation.X * z);
}
///
/// Creates a new that contains a transformation of 2d-vector by the specified .
///
/// Source .
/// The transformation .
/// Transformed .
public static Vector2 Transform(Vector2 position, Matrix3x2 matrix)
{
return new Vector2(
(position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M31,
(position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M32
);
}
///
/// Apply transformation on all vectors within array of by the specified and places the results in an another array.
///
/// Source array.
/// The transformation .
/// Destination array.
public static void Transform(
Vector2[] sourceArray,
ref Matrix4x4 matrix,
Vector2[] destinationArray
)
{
Transform(sourceArray, 0, ref matrix, destinationArray, 0, sourceArray.Length);
}
///
/// Apply transformation on vectors within array of by the specified and places the results in an another array.
///
/// Source array.
/// The starting index of transformation in the source array.
/// The transformation .
/// Destination array.
/// The starting index in the destination array, where the first should be written.
/// The number of vectors to be transformed.
public static void Transform(
Vector2[] sourceArray,
int sourceIndex,
ref Matrix4x4 matrix,
Vector2[] destinationArray,
int destinationIndex,
int length
)
{
for (int x = 0; x < length; x += 1)
{
Vector2 position = sourceArray[sourceIndex + x];
Vector2 destination = destinationArray[destinationIndex + x];
destination.X = (position.X * matrix.M11) + (position.Y * matrix.M21)
+ matrix.M41;
destination.Y = (position.X * matrix.M12) + (position.Y * matrix.M22)
+ matrix.M42;
destinationArray[destinationIndex + x] = destination;
}
}
///
/// Apply transformation on all vectors within array of by the specified and places the results in an another array.
///
/// Source array.
/// The which contains rotation transformation.
/// Destination array.
public static void Transform(
Vector2[] sourceArray,
ref Quaternion rotation,
Vector2[] destinationArray
)
{
Transform(
sourceArray,
0,
ref rotation,
destinationArray,
0,
sourceArray.Length
);
}
///
/// Apply transformation on vectors within array of by the specified and places the results in an another array.
///
/// Source array.
/// The starting index of transformation in the source array.
/// The which contains rotation transformation.
/// Destination array.
/// The starting index in the destination array, where the first should be written.
/// The number of vectors to be transformed.
public static void Transform(
Vector2[] sourceArray,
int sourceIndex,
ref Quaternion rotation,
Vector2[] destinationArray,
int destinationIndex,
int length
)
{
for (int i = 0; i < length; i += 1)
{
Vector2 position = sourceArray[sourceIndex + i];
Vector2 v;
Transform(ref position, ref rotation, out v);
destinationArray[destinationIndex + i] = v;
}
}
///
/// Creates a new that contains a transformation of the specified normal by the specified .
///
/// Source which represents a normal vector.
/// The transformation .
/// Transformed normal.
public static Vector2 TransformNormal(Vector2 normal, Matrix4x4 matrix)
{
return new Vector2(
(normal.X * matrix.M11) + (normal.Y * matrix.M21),
(normal.X * matrix.M12) + (normal.Y * matrix.M22)
);
}
///
/// Creates a new that contains a transformation of the specified normal by the specified .
///
/// Source which represents a normal vector.
/// The transformation .
/// Transformed normal.
public static Vector2 TransformNormal(Vector2 normal, Matrix3x2 matrix)
{
return new Vector2(
normal.X * matrix.M11 + normal.Y * matrix.M21,
normal.X * matrix.M12 + normal.Y * matrix.M22);
}
///
/// Apply transformation on all normals within array of by the specified and places the results in an another array.
///
/// Source array.
/// The transformation .
/// Destination array.
public static void TransformNormal(
Vector2[] sourceArray,
ref Matrix4x4 matrix,
Vector2[] destinationArray
)
{
TransformNormal(
sourceArray,
0,
ref matrix,
destinationArray,
0,
sourceArray.Length
);
}
///
/// Apply transformation on normals within array of by the specified and places the results in an another array.
///
/// Source array.
/// The starting index of transformation in the source array.
/// The transformation .
/// Destination array.
/// The starting index in the destination array, where the first should be written.
/// The number of normals to be transformed.
public static void TransformNormal(
Vector2[] sourceArray,
int sourceIndex,
ref Matrix4x4 matrix,
Vector2[] destinationArray,
int destinationIndex,
int length
)
{
for (int i = 0; i < length; i += 1)
{
Vector2 position = sourceArray[sourceIndex + i];
Vector2 result;
result.X = (position.X * matrix.M11) + (position.Y * matrix.M21);
result.Y = (position.X * matrix.M12) + (position.Y * matrix.M22);
destinationArray[destinationIndex + i] = result;
}
}
///
/// Rotates a Vector2 by an angle.
///
/// The vector to rotate.
/// The angle in radians.
public static Vector2 Rotate(Vector2 vector, Fix64 angle)
{
return new Vector2(
vector.X * Fix64.Cos(angle) - vector.Y * Fix64.Sin(angle),
vector.X * Fix64.Sin(angle) + vector.Y * Fix64.Cos(angle)
);
}
#endregion
#region Public Static Operators
///
/// Inverts values in the specified .
///
/// Source on the right of the sub sign.
/// Result of the inversion.
public static Vector2 operator -(Vector2 value)
{
value.X = -value.X;
value.Y = -value.Y;
return value;
}
///
/// Compares whether two instances are equal.
///
/// instance on the left of the equal sign.
/// instance on the right of the equal sign.
/// true if the instances are equal; false otherwise.
public static bool operator ==(Vector2 value1, Vector2 value2)
{
return (value1.X == value2.X &&
value1.Y == value2.Y);
}
///
/// Compares whether two instances are equal.
///
/// instance on the left of the equal sign.
/// instance on the right of the equal sign.
/// true if the instances are equal; false otherwise.
public static bool operator !=(Vector2 value1, Vector2 value2)
{
return !(value1 == value2);
}
///
/// Adds two vectors.
///
/// Source on the left of the add sign.
/// Source on the right of the add sign.
/// Sum of the vectors.
public static Vector2 operator +(Vector2 value1, Vector2 value2)
{
value1.X += value2.X;
value1.Y += value2.Y;
return value1;
}
///
/// Subtracts a from a .
///
/// Source on the left of the sub sign.
/// Source on the right of the sub sign.
/// Result of the vector subtraction.
public static Vector2 operator -(Vector2 value1, Vector2 value2)
{
value1.X -= value2.X;
value1.Y -= value2.Y;
return value1;
}
///
/// Multiplies the components of two vectors by each other.
///
/// Source on the left of the mul sign.
/// Source on the right of the mul sign.
/// Result of the vector multiplication.
public static Vector2 operator *(Vector2 value1, Vector2 value2)
{
value1.X *= value2.X;
value1.Y *= value2.Y;
return value1;
}
///
/// Multiplies the components of vector by a scalar.
///
/// Source on the left of the mul sign.
/// Scalar value on the right of the mul sign.
/// Result of the vector multiplication with a scalar.
public static Vector2 operator *(Vector2 value, Fix64 scaleFactor)
{
value.X *= scaleFactor;
value.Y *= scaleFactor;
return value;
}
///
/// Multiplies the components of vector by a scalar.
///
/// Scalar value on the left of the mul sign.
/// Source on the right of the mul sign.
/// Result of the vector multiplication with a scalar.
public static Vector2 operator *(Fix64 scaleFactor, Vector2 value)
{
value.X *= scaleFactor;
value.Y *= scaleFactor;
return value;
}
///
/// Divides the components of a by the components of another .
///
/// Source on the left of the div sign.
/// Divisor on the right of the div sign.
/// The result of dividing the vectors.
public static Vector2 operator /(Vector2 value1, Vector2 value2)
{
value1.X /= value2.X;
value1.Y /= value2.Y;
return value1;
}
///
/// Divides the components of a by a scalar.
///
/// Source on the left of the div sign.
/// Divisor scalar on the right of the div sign.
/// The result of dividing a vector by a scalar.
public static Vector2 operator /(Vector2 value1, Fix64 divider)
{
Fix64 factor = Fix64.One / divider;
value1.X *= factor;
value1.Y *= factor;
return value1;
}
#endregion
}
}