/// <summary> /// Retrieves the angle, expressed in radians, between the two specified vectors. /// </summary> /// <param name="vector1">The first vector to evaluate.</param> /// <param name="vector2">The second vector to evaluate.</param> /// <returns>The angle, in radians, between vector1 and vector2.</returns> public static float AngleBetween(VectorF vector1, VectorF vector2) { vector1.Normalize(); vector2.Normalize(); double angle = Math.Atan2(vector2.y, vector2.x) - Math.Atan2(vector1.y, vector1.x); if (angle > Math.PI) { angle -= Math.PI * 2.0; } else if (angle < -Math.PI) { angle += Math.PI * 2.0; } return((float)angle); }
/// <summary> /// Calculates the cross product of two vectors. /// </summary> /// <param name="vector1">The first vector to evaluate.</param> /// <param name="vector2">The second vector to evaluate.</param> /// <returns>The cross product of vector1 and vector2. The following formula is used to /// calculate the cross product: (Vector1.X * Vector2.Y) - (Vector1.Y * Vector2.X)</returns> public static float CrossProduct(VectorF vector1, VectorF vector2) { return((vector1.x * vector2.y) - (vector1.y * vector2.x)); }
/// <summary> /// Calculates the determinant of two vectors. /// </summary> /// <param name="vector1">The first vector to evaluate.</param> /// <param name="vector2">The second vector to evaluate.</param> /// <returns>The determinant of vector1 and vector2.</returns> public static float Determinant(VectorF vector1, VectorF vector2) { // In the case of two 2D vectors, the determinant is the cross-product. return(CrossProduct(vector1, vector2)); }
/// <summary> /// Divides the specified vector by the specified scalar and returns the result. /// </summary> /// <param name="vector">The vector structure to divide.</param> /// <param name="scalar">The amount by which vector is divided.</param> /// <returns>The result of dividing vector by scalar.</returns> public static VectorF Divide(VectorF vector, float scalar) { return(vector / scalar); }
/// <summary> /// Calculates the dot product of the two specified vectors and returns the result. /// </summary> /// <param name="vector1">The first vector to multiply.</param> /// <param name="vector2">The second vector structure to multiply.</param> /// <returns>The scalar dot product of vector1 and vector2, which is calculated /// using the following formula: (vector1.X * vector2.X) + (vector1.Y * vector2.Y)</returns> public static float Multiply(VectorF vector1, VectorF vector2) { return(vector1 * vector2); }
/// <summary> /// Multiplies the specified scalar by the specified vector and returns the resulting vector. /// </summary> /// <param name="scalar">The scalar to multiply.</param> /// <param name="vector">The vector to multiply.</param> /// <returns>The result of multiplying scalar and vector.</returns> public static VectorF Multiply(float scalar, VectorF vector) { return(scalar * vector); }
/// <summary> /// Multiplies the specified scalar by the specified vector and returns the resulting vector. /// </summary> /// <param name="vector">The vector to multiply.</param> /// <param name="scalar">The scalar to multiply.</param> /// <returns>The result of multiplying scalar and vector.</returns> public static VectorF Multiply(VectorF vector, float scalar) { return(vector * scalar); }
/// <summary> /// Subtracts the specified vector from another specified vector. /// </summary> /// <param name="vector1">The vector from which vector2 is subtracted.</param> /// <param name="vector2">The vector to subtract from vector1.</param> /// <returns>The difference between vector1 and vector2.</returns> public static VectorF Subtract(VectorF vector1, VectorF vector2) { return(vector1 - vector2); }
/// <summary> /// Translates the specified point by the specified vector and returns the resulting point. /// </summary> /// <param name="vector">The amount to translate the specified point.</param> /// <param name="point">The point to translate.</param> /// <returns>The result of translating point by vector.</returns> public static PointF Add(VectorF vector, PointF point) { return(vector + point); }
/// <summary> /// Adds two vectors and returns the result. /// </summary> /// <param name="vector1">The first vector to add.</param> /// <param name="vector2">The second vector to add.</param> /// <returns> The sum of vector1 and vector2.</returns> public static VectorF Add(VectorF vector1, VectorF vector2) { return(vector1 + vector2); }
/// <summary> /// Compares two vectors for equality. /// </summary> /// <param name="value">The vector to compare with this vector.</param> /// <returns>true if value has the same X and Y values as this vector; /// otherwise, false.</returns> public bool Equals(VectorF value) { return(value == this); }
/// <summary> /// Compares the two specified vectors for equality. /// </summary> /// <param name="vector1">The first vector to compare.</param> /// <param name="vector2">The second vector to compare.</param> /// <returns>true if the X and Y components of vector1 and vector2 are equal; /// otherwise, false.</returns> public static bool Equals(VectorF vector1, VectorF vector2) { return(vector1 == vector2); }