/// <summary> /// AngleBetween - the angle between 2 vectors /// </summary> /// <returns> /// Returns the the angle in degrees between vector1 and vector2 /// </returns> /// <param name="vector1"> The first Vector </param> /// <param name="vector2"> The second Vector </param> public static double AngleBetween(Vector vector1, Vector vector2) { double sin = vector1._x * vector2._y - vector2._x * vector1._y; double cos = vector1._x * vector2._x + vector1._y * vector2._y; return Math.Atan2(sin, cos) * (180 / Math.PI); }
public static bool IsCCW(Point pt1, Point pt2, Point pt3) { Vector V21 = new Vector(pt2, pt1); Vector v23 = new Vector(pt2, pt3); return CrossProduct(V21, v23) > 0; // sin(angle pt2 pt1 pt3) < 0, 180<angle pt2 pt1 pt3 <360 }
public static bool IsClockwise(Point pt1, Point pt2, Point pt3) { Vector V21 = new Vector(pt2, pt1); Vector v23 = new Vector(pt2, pt3); return CrossProduct(V21, v23) < 0; // sin(angle pt1 pt2 pt3) > 0, 0<angle pt1 pt2 pt3 <180 }
/// <summary> /// Equals - compares this Vector with the passed in object. In this equality /// Double.NaN is equal to itself, unlike in numeric equality. /// Note that double values can acquire error when operated upon, such that /// an exact comparison between two values which /// are logically equal may fail. /// </summary> /// <returns> /// bool - true if "value" is equal to "this". /// </returns> /// <param name='value'>The Vector to compare to "this"</param> public bool Equals(Vector value) { return Vector.Equals(this, value); }
/// <summary> /// Compares two Vector instances for object equality. In this equality /// Double.NaN is equal to itself, unlike in numeric equality. /// Note that double values can acquire error when operated upon, such that /// an exact comparison between two values which /// are logically equal may fail. /// </summary> /// <returns> /// bool - true if the two Vector instances are exactly equal, false otherwise /// </returns> /// <param name='vector1'>The first Vector to compare</param> /// <param name='vector2'>The second Vector to compare</param> public static bool Equals(Vector vector1, Vector vector2) { return vector1.X.Equals(vector2.X) && vector1.Y.Equals(vector2.Y); }
/// <summary> /// Subtract: Point - Vector /// </summary> /// <returns> /// Point - The result of the subtraction /// </returns> /// <param name="point"> The Point from which the Vector is subtracted </param> /// <param name="vector"> The Vector which is subtracted from the Point </param> public static Point Subtract(Point point, Vector vector) { return new Point(point._x - vector._x, point._y - vector._y); }
/// <summary> /// Offset - return the result of offsetting rect by the offset provided /// If this is Empty, this method is illegal. /// </summary> public static Rect Offset(Rect rect, Vector offsetVector) { rect.Offset(offsetVector.X, offsetVector.Y); return rect; }
/// <summary> /// Multiply: double * Vector /// </summary> public static Vector Multiply(double scalar, Vector vector) { return new Vector(vector._x * scalar, vector._y * scalar); }
/// <summary> /// Multiply: Vector / double /// </summary> public static Vector Divide(Vector vector, double scalar) { return vector * (1.0 / scalar); }
/// <summary> /// Subtract: Vector - Vector /// </summary> public static Vector Subtract(Vector vector1, Vector vector2) { return new Vector(vector1._x - vector2._x, vector1._y - vector2._y); }
/// <summary> /// Add: Vector + Point /// </summary> public static Point Add(Vector vector, Point point) { return new Point(point._x + vector._x, point._y + vector._y); }
/// <summary> /// Add: Vector + Vector /// </summary> public static Vector Add(Vector vector1, Vector vector2) { return new Vector(vector1._x + vector2._x, vector1._y + vector2._y); }
/// <summary> /// Transform - Transforms each Vector in the array by this matrix. /// </summary> /// <param name="vectors"> The Vector array to transform </param> public void Transform(Vector[] vectors) { if (vectors != null) { for (int i = 0; i < vectors.Length; i++) { MultiplyVector(ref vectors[i]._x, ref vectors[i]._y); } } }
/// <summary> /// Transform - returns the result of transforming the Vector by this matrix. /// </summary> /// <returns> /// The transformed vector /// </returns> /// <param name="vector"> The Vector to transform </param> public Vector Transform(Vector vector) { Vector newVector = vector; MultiplyVector(ref newVector._x, ref newVector._y); return newVector; }
public static double DistancePointLine(Point pt, Point lnA, Point lnB) { Vector v1 = new Vector(lnA, lnB); Vector v2 = new Vector(lnA, pt); v1.Normalize(); return Math.Abs(CrossProduct(v2, v1)); }
/// <summary> /// Multiply: Vector * Matrix /// </summary> public static Vector Multiply(Vector vector, Matrix matrix) { return matrix.Transform(vector); }
/// <summary> /// CrossProduct - Returns the cross product: vector1.X*vector2.Y - vector1.Y*vector2.X /// </summary> /// <returns> /// Returns the cross product: vector1.X*vector2.Y - vector1.Y*vector2.X /// </returns> /// <param name="vector1"> The first Vector </param> /// <param name="vector2"> The second Vector </param> public static double CrossProduct(Vector vector1, Vector vector2) { return vector1._x * vector2._y - vector1._y * vector2._x; }
/// <summary> /// Multiply - Returns the dot product: vector1.X*vector2.X + vector1.Y*vector2.Y /// </summary> /// <returns> /// Returns the dot product: vector1.X*vector2.X + vector1.Y*vector2.Y /// </returns> /// <param name="vector1"> The first Vector </param> /// <param name="vector2"> The second Vector </param> public static double Multiply(Vector vector1, Vector vector2) { return vector1._x * vector2._x + vector1._y * vector2._y; }
/// <summary> /// Offset - translate the Location by the offset provided. /// If this is Empty, this method is illegal. /// </summary> public void Offset(Vector offsetVector) { if (IsEmpty) { throw new System.InvalidOperationException("Rect_CannotCallMethod"); } _x += offsetVector._x; _y += offsetVector._y; }
/// <summary> /// Determinant - Returns the determinant det(vector1, vector2) /// </summary> /// <returns> /// Returns the determinant: vector1.X*vector2.Y - vector1.Y*vector2.X /// </returns> /// <param name="vector1"> The first Vector </param> /// <param name="vector2"> The second Vector </param> public static double Determinant(Vector vector1, Vector vector2) { return vector1._x * vector2._y - vector1._y * vector2._x; }
/// <summary> /// Constructor which sets the initial values to bound the point provided and the point /// which results from point + vector. /// </summary> public Rect(Point point, Vector vector) : this(point, point + vector) { }
/// <summary> /// Compares two Vector instances for fuzzy equality. This function /// helps compensate for the fact that double values can /// acquire error when operated upon /// </summary> /// <param name='vector1'>The first Vector to compare</param> /// <param name='vector2'>The second Vector to compare</param> /// <returns>Whether or not the two Vector instances are equal</returns> public static bool AreClose(Vector vector1, Vector vector2) { return DoubleUtil.AreClose(vector1.X, vector2.X) && DoubleUtil.AreClose(vector1.Y, vector2.Y); }