public bool Equals(Vector2f obj) { return obj.x == this.x && obj.y == this.y; }
/// <summary> /// Obtains the square distance between two points. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <returns>Square distance.</returns> public static float SquareDistance(Vector2f u, Vector2f v) { return (u.X - v.X)*(u.X - v.X) + (u.Y - v.Y)*(u.Y - v.Y); }
/// </summary> /// Check if the components of two vectors are approximate equals. /// <param name="obj">Vector2f.</param> /// <param name="threshold">Maximun tolerance.</param> /// <returns>True if the three components are almost equal or false in anyother case.</returns> public bool Equals(Vector2f obj, float threshold) { if (Math.Abs(obj.X - this.x) > threshold) { return false; } if (Math.Abs(obj.Y - this.y) > threshold) { return false; } return true; }
/// <summary> /// Obtains the counter clockwise perpendicular vector . /// </summary> /// <param name="u">Vector2f.</param> /// <returns>Vector2f.</returns> public static Vector2f Perpendicular(Vector2f u) { return new Vector2f(-u.Y, u.X); }
/// <summary> /// Rounds the components of a vector. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="numDigits">Number of significative defcimal digits.</param> /// <returns>Vector2f.</returns> public static Vector2f Round(Vector2f u, int numDigits) { return new Vector2f((float) Math.Round(u.X, numDigits), (float) Math.Round(u.Y, numDigits)); }
/// <summary> /// Obtains the dot product of two vectors. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <returns>The dot product.</returns> public static float DotProduct(Vector2f u, Vector2f v) { return (u.X*v.X) + (u.Y*v.Y); }
/// <summary> /// Obtains the midpoint. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <returns>Vector2f.</returns> public static Vector2f MidPoint(Vector2f u, Vector2f v) { return new Vector2f((v.X + u.X)*0.5F, (v.Y + u.Y)*0.5F); }
/// <summary> /// Obtains the distance between two points. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <returns>Distancie.</returns> public static float Distance(Vector2f u, Vector2f v) { return (float) (Math.Sqrt((u.X - v.X)*(u.X - v.X) + (u.Y - v.Y)*(u.Y - v.Y))); }
/// <summary> /// Obtains the cross product of two vectors. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <returns>Vector2f.</returns> public static float CrossProduct(Vector2f u, Vector2f v) { return (u.X*v.Y) - (u.Y*v.X); }
/// <summary> /// Checks if two vectors are perpendicular. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <param name="threshold">Tolerance used.</param> /// <returns>True if are penpendicular or false in anyother case.</returns> public static bool ArePerpendicular(Vector2f u, Vector2f v, float threshold) { return MathHelper.IsZero(DotProduct(u, v), threshold); }
/// <summary> /// Checks if two vectors are parallel. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <param name="threshold">Tolerance used.</param> /// <returns>True if are parallel or false in anyother case.</returns> public static bool AreParallel(Vector2f u, Vector2f v, float threshold) { float a = u.X*v.Y - u.Y*v.X; return MathHelper.IsZero(a, threshold); }
/// <summary> /// Obtains the angle between two vectors. /// </summary> /// <param name="u">Vector2f.</param> /// <param name="v">Vector2f.</param> /// <returns>Angle in radians.</returns> public static float AngleBetween(Vector2f u, Vector2f v) { float cos = DotProduct(u, v)/(u.Modulus()*v.Modulus()); if (MathHelper.IsOne(cos)) { return 0; } if (MathHelper.IsOne(-cos)) { return (float) Math.PI; } return (float) Math.Acos(cos); //if (AreParallel(u, v)) //{ // if (Math.Sign(u.X) == Math.Sign(v.X) && Math.Sign(u.Y) == Math.Sign(v.Y)) // { // return 0; // } // return (float)Math.PI; //} //Vector3f normal = Vector3f.CrossProduct(new Vector3f(u.X, u.Y, 0), new Vector3f(v.X, v.Y, 0)); //if (normal.Z < 0) //{ // return (float)(2 * Math.PI - Math.Acos(DotProduct(u, v) / (u.Modulus() * v.Modulus()))); //} //return (float)(Math.Acos(DotProduct(u, v) / (u.Modulus() * v.Modulus()))); }