// MIT License - Copyright (C) The Mono.Xna Team // This file is subject to the terms and conditions defined in // file 'LICENSE.txt', which is part of this source code package. using System; using System.Diagnostics; namespace CommonLang.Geometry { public struct Vector2 : IEquatable { #region Private Fields private static readonly Vector2 zeroVector = new Vector2(0f, 0f); private static readonly Vector2 unitVector = new Vector2(1f, 1f); private static readonly Vector2 unitXVector = new Vector2(1f, 0f); private static readonly Vector2 unitYVector = new Vector2(0f, 1f); #endregion #region Public Fields /// /// The x coordinate of this . /// public float X; /// /// The y coordinate of this . /// public float Y; #endregion #region 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( this.X.ToString(), " ", this.Y.ToString() ); } } #endregion #region 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(float x, float 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(float value) { this.X = value; this.Y = value; } #endregion #region 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; } /// /// 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, float 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 *(float 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, float divider) { float factor = 1 / divider; value1.X *= factor; value1.Y *= factor; return value1; } /// /// 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 not equal. /// /// instance on the left of the not equal sign. /// instance on the right of the not equal sign. /// true if the instances are not equal; false otherwise. public static bool operator !=(Vector2 value1, Vector2 value2) { return value1.X != value2.X || value1.Y != value2.Y; } #endregion #region Public 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; } /// /// Performs vector addition on and /// , storing the result of the /// addition in . /// /// The first vector to add. /// The second vector to add. /// The result of the vector addition. public static void Add(ref Vector2 value1, ref Vector2 value2, out Vector2 result) { result.X = value1.X + value2.X; result.Y = value1.Y + value2.Y; } /// /// Creates a new that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 2d-triangle. /// /// The first vector of 2d-triangle. /// The second vector of 2d-triangle. /// The third vector of 2d-triangle. /// Barycentric scalar b2 which represents a weighting factor towards second vector of 2d-triangle. /// Barycentric scalar b3 which represents a weighting factor towards third vector of 2d-triangle. /// The cartesian translation of barycentric coordinates. public static Vector2 Barycentric(Vector2 value1, Vector2 value2, Vector2 value3, float amount1, float amount2) { return new Vector2( MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2), MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2)); } /// /// Creates a new that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 2d-triangle. /// /// The first vector of 2d-triangle. /// The second vector of 2d-triangle. /// The third vector of 2d-triangle. /// Barycentric scalar b2 which represents a weighting factor towards second vector of 2d-triangle. /// Barycentric scalar b3 which represents a weighting factor towards third vector of 2d-triangle. /// The cartesian translation of barycentric coordinates as an output parameter. public static void Barycentric(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, float amount1, float amount2, out Vector2 result) { result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2); result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2); } /// /// Creates a new that contains CatmullRom interpolation of the specified vectors. /// /// The first vector in interpolation. /// The second vector in interpolation. /// The third vector in interpolation. /// The fourth vector in interpolation. /// Weighting factor. /// The result of CatmullRom interpolation. public static Vector2 CatmullRom(Vector2 value1, Vector2 value2, Vector2 value3, Vector2 value4, float amount) { return new Vector2( MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount), MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount)); } /// /// Creates a new that contains CatmullRom interpolation of the specified vectors. /// /// The first vector in interpolation. /// The second vector in interpolation. /// The third vector in interpolation. /// The fourth vector in interpolation. /// Weighting factor. /// The result of CatmullRom interpolation as an output parameter. public static void CatmullRom(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, ref Vector2 value4, float amount, out Vector2 result) { result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount); result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount); } /// /// 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( MathHelper.Clamp(value1.X, min.X, max.X), MathHelper.Clamp(value1.Y, min.Y, max.Y)); } /// /// Clamps the specified value within a range. /// /// The value to clamp. /// The min value. /// The max value. /// The clamped value as an output parameter. public static void Clamp(ref Vector2 value1, ref Vector2 min, ref Vector2 max, out Vector2 result) { result.X = MathHelper.Clamp(value1.X, min.X, max.X); result.Y = MathHelper.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 float Distance(Vector2 value1, Vector2 value2) { float v1 = value1.X - value2.X, v2 = value1.Y - value2.Y; return (float)Math.Sqrt((v1 * v1) + (v2 * v2)); } /// /// Returns the distance between two vectors. /// /// The first vector. /// The second vector. /// The distance between two vectors as an output parameter. public static void Distance(ref Vector2 value1, ref Vector2 value2, out float result) { float v1 = value1.X - value2.X, v2 = value1.Y - value2.Y; result = (float)Math.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 float DistanceSquared(Vector2 value1, Vector2 value2) { float v1 = value1.X - value2.X, v2 = value1.Y - value2.Y; return (v1 * v1) + (v2 * v2); } /// /// Returns the squared distance between two vectors. /// /// The first vector. /// The second vector. /// The squared distance between two vectors as an output parameter. public static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result) { float v1 = value1.X - value2.X, v2 = value1.Y - value2.Y; result = (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 the components of another . /// /// Source . /// Divisor . /// The result of dividing the vectors as an output parameter. public static void Divide(ref Vector2 value1, ref Vector2 value2, out Vector2 result) { result.X = value1.X / value2.X; result.Y = value1.Y / value2.Y; } /// /// 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, float divider) { float factor = 1 / divider; value1.X *= factor; value1.Y *= factor; return value1; } /// /// Divides the components of a by a scalar. /// /// Source . /// Divisor scalar. /// The result of dividing a vector by a scalar as an output parameter. public static void Divide(ref Vector2 value1, float divider, out Vector2 result) { float factor = 1 / divider; result.X = value1.X * factor; result.Y = value1.Y * factor; } /// /// Returns a dot product of two vectors. /// /// The first vector. /// The second vector. /// The dot product of two vectors. public static float Dot(Vector2 value1, Vector2 value2) { return (value1.X * value2.X) + (value1.Y * value2.Y); } /// /// Returns a dot product of two vectors. /// /// The first vector. /// The second vector. /// The dot product of two vectors as an output parameter. public static void Dot(ref Vector2 value1, ref Vector2 value2, out float result) { result = (value1.X * value2.X) + (value1.Y * value2.Y); } /// /// 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) { if (obj is Vector2) { return Equals((Vector2)obj); } return false; } /// /// 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(); } /// /// Creates a new that contains hermite spline interpolation. /// /// The first position vector. /// The first tangent vector. /// The second position vector. /// The second tangent vector. /// Weighting factor. /// The hermite spline interpolation vector. public static Vector2 Hermite(Vector2 value1, Vector2 tangent1, Vector2 value2, Vector2 tangent2, float amount) { return new Vector2(MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount), MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount)); } /// /// Creates a new that contains hermite spline interpolation. /// /// The first position vector. /// The first tangent vector. /// The second position vector. /// The second tangent vector. /// Weighting factor. /// The hermite spline interpolation vector as an output parameter. public static void Hermite(ref Vector2 value1, ref Vector2 tangent1, ref Vector2 value2, ref Vector2 tangent2, float amount, out Vector2 result) { result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount); result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount); } /// /// Returns the length of this . /// /// The length of this . public float Length() { return (float)Math.Sqrt((X * X) + (Y * Y)); } /// /// Returns the squared length of this . /// /// The squared length of this . public float LengthSquared() { return (X * X) + (Y * 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, float amount) { return new Vector2( MathHelper.Lerp(value1.X, value2.X, amount), MathHelper.Lerp(value1.Y, value2.Y, amount)); } /// /// 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 as an output parameter. public static void Lerp(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result) { result.X = MathHelper.Lerp(value1.X, value2.X, amount); result.Y = MathHelper.Lerp(value1.Y, value2.Y, amount); } /// /// 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 maximal values from the two vectors. /// /// The first vector. /// The second vector. /// The with maximal values from the two vectors as an output parameter. public static void Max(ref Vector2 value1, ref Vector2 value2, out Vector2 result) { result.X = value1.X > value2.X ? value1.X : value2.X; result.Y = 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 minimal values from the two vectors. /// /// The first vector. /// The second vector. /// The with minimal values from the two vectors as an output parameter. public static void Min(ref Vector2 value1, ref Vector2 value2, out Vector2 result) { result.X = value1.X < value2.X ? value1.X : value2.X; result.Y = 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 two vectors. /// /// Source . /// Source . /// The result of the vector multiplication as an output parameter. public static void Multiply(ref Vector2 value1, ref Vector2 value2, out Vector2 result) { result.X = value1.X * value2.X; result.Y = value1.Y * value2.Y; } /// /// 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, float scaleFactor) { value1.X *= scaleFactor; value1.Y *= scaleFactor; return value1; } /// /// Creates a new that contains a multiplication of and a scalar. /// /// Source . /// Scalar value. /// The result of the multiplication with a scalar as an output parameter. public static void Multiply(ref Vector2 value1, float scaleFactor, out Vector2 result) { result.X = value1.X * scaleFactor; result.Y = value1.Y * scaleFactor; } /// /// Creates a new that contains the specified vector inversion. /// /// 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 the specified vector inversion. /// /// Source . /// The result of the vector inversion as an output parameter. public static void Negate(ref Vector2 value, out Vector2 result) { result.X = -value.X; result.Y = -value.Y; } /// /// Turns this to a unit vector with the same direction. /// public void Normalize() { float val = 1.0f / (float)Math.Sqrt((X * X) + (Y * Y)); X *= val; Y *= val; } /// /// Creates a new that contains a normalized values from another vector. /// /// Source . /// Unit vector. public static Vector2 Normalize(Vector2 value) { float val = 1.0f / (float)Math.Sqrt((value.X * value.X) + (value.Y * value.Y)); value.X *= val; value.Y *= val; return value; } /// /// Creates a new that contains a normalized values from another vector. /// /// Source . /// Unit vector as an output parameter. public static void Normalize(ref Vector2 value, out Vector2 result) { float val = 1.0f / (float)Math.Sqrt((value.X * value.X) + (value.Y * value.Y)); result.X = value.X * val; result.Y = value.Y * val; } /// /// 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; float val = 2.0f * ((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 reflect vector of the given vector and normal. /// /// Source . /// Reflection normal. /// Reflected vector as an output parameter. public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result) { float val = 2.0f * ((vector.X * normal.X) + (vector.Y * normal.Y)); result.X = vector.X - (normal.X * val); result.Y = vector.Y - (normal.Y * val); } /// /// Creates a new that contains cubic interpolation of the specified vectors. /// /// Source . /// Source . /// Weighting value. /// Cubic interpolation of the specified vectors. public static Vector2 SmoothStep(Vector2 value1, Vector2 value2, float amount) { return new Vector2( MathHelper.SmoothStep(value1.X, value2.X, amount), MathHelper.SmoothStep(value1.Y, value2.Y, amount)); } /// /// Creates a new that contains cubic interpolation of the specified vectors. /// /// Source . /// Source . /// Weighting value. /// Cubic interpolation of the specified vectors as an output parameter. public static void SmoothStep(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result) { result.X = MathHelper.SmoothStep(value1.X, value2.X, amount); result.Y = MathHelper.SmoothStep(value1.Y, value2.Y, amount); } /// /// 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 subtraction of on from a another. /// /// Source . /// Source . /// The result of the vector subtraction as an output parameter. public static void Subtract(ref Vector2 value1, ref Vector2 value2, out Vector2 result) { result.X = value1.X - value2.X; result.Y = value1.Y - value2.Y; } /// /// Returns a representation of this in the format: /// {X:[] Y:[]} /// /// A representation of this . public override string ToString() { return "{X:" + X + " Y:" + Y + "}"; } /// /// Gets a representation for this object. /// /// A representation for this object. public Point ToPoint() { return new Point((int) X,(int) Y); } /// /// Creates a new that contains a transformation of vector(position.X,position.Y,0,1) by the specified . /// /// Source . /// The transformation . /// Transformed . public static Vector2 Transform(Vector2 position, Matrix 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 vector(position.X,position.Y,0,1) by the specified . /// /// Source . /// The transformation . /// Transformed as an output parameter. public static void Transform(ref Vector2 position, ref Matrix matrix, out Vector2 result) { var x = (position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M41; var y = (position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M42; result.X = x; result.Y = y; } /// /// Creates a new that contains a transformation of vector(position.X,position.Y,0,0) 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 vector(position.X,position.Y,0,0) 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) { var rot1 = new Vector3(rotation.X + rotation.X, rotation.Y + rotation.Y, rotation.Z + rotation.Z); var rot2 = new Vector3(rotation.X, rotation.X, rotation.W); var rot3 = new Vector3(1, rotation.Y, rotation.Z); var rot4 = rot1*rot2; var rot5 = rot1*rot3; var v = new Vector2(); v.X = (float)((double)value.X * (1.0 - (double)rot5.Y - (double)rot5.Z) + (double)value.Y * ((double)rot4.Y - (double)rot4.Z)); v.Y = (float)((double)value.X * ((double)rot4.Y + (double)rot4.Z) + (double)value.Y * (1.0 - (double)rot4.X - (double)rot5.Z)); result.X = v.X; result.Y = v.Y; } /// /// 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 Matrix matrix, Vector2[] destinationArray, int destinationIndex, int length) { if (sourceArray == null) throw new ArgumentNullException("sourceArray"); if (destinationArray == null) throw new ArgumentNullException("destinationArray"); if (sourceArray.Length < sourceIndex + length) throw new ArgumentException("Source array length is lesser than sourceIndex + length"); if (destinationArray.Length < destinationIndex + length) throw new ArgumentException("Destination array length is lesser than destinationIndex + length"); for (int x = 0; x < length; x++) { var position = sourceArray[sourceIndex + x]; var 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 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 ) { if (sourceArray == null) throw new ArgumentNullException("sourceArray"); if (destinationArray == null) throw new ArgumentNullException("destinationArray"); if (sourceArray.Length < sourceIndex + length) throw new ArgumentException("Source array length is lesser than sourceIndex + length"); if (destinationArray.Length < destinationIndex + length) throw new ArgumentException("Destination array length is lesser than destinationIndex + length"); for (int x = 0; x < length; x++) { var position = sourceArray[sourceIndex + x]; var destination = destinationArray[destinationIndex + x]; Vector2 v; Transform(ref position,ref rotation,out v); destination.X = v.X; destination.Y = v.Y; 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 transformation . /// Destination array. public static void Transform( Vector2[] sourceArray, ref Matrix matrix, Vector2[] destinationArray) { Transform(sourceArray, 0, ref matrix, destinationArray, 0, sourceArray.Length); } /// /// 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); } /// /// 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, Matrix 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 as an output parameter. public static void TransformNormal(ref Vector2 normal, ref Matrix matrix, out Vector2 result) { var x = (normal.X * matrix.M11) + (normal.Y * matrix.M21); var y = (normal.X * matrix.M12) + (normal.Y * matrix.M22); result.X = x; result.Y = y; } /// /// 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 Matrix matrix, Vector2[] destinationArray, int destinationIndex, int length ) { if (sourceArray == null) throw new ArgumentNullException("sourceArray"); if (destinationArray == null) throw new ArgumentNullException("destinationArray"); if (sourceArray.Length < sourceIndex + length) throw new ArgumentException("Source array length is lesser than sourceIndex + length"); if (destinationArray.Length < destinationIndex + length) throw new ArgumentException("Destination array length is lesser than destinationIndex + length"); for (int i = 0; i < length; i++) { var normal = sourceArray[sourceIndex + i]; destinationArray[destinationIndex + i] = 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 Matrix matrix, Vector2[] destinationArray ) { if (sourceArray == null) throw new ArgumentNullException("sourceArray"); if (destinationArray == null) throw new ArgumentNullException("destinationArray"); if (destinationArray.Length < sourceArray.Length) throw new ArgumentException("Destination array length is lesser than source array length"); for (int i = 0; i < sourceArray.Length; i++) { var normal = sourceArray[i]; destinationArray[i] = new Vector2((normal.X * matrix.M11) + (normal.Y * matrix.M21), (normal.X * matrix.M12) + (normal.Y * matrix.M22)); } } #endregion } }