static public Vector2D operator*(Vector2D A, Vector2D B) { Vector2D temp = new Vector2D(); temp.x = A.x * B.x; temp.y = A.y * B.y; return temp; }
static public Vector2D operator *(Vector2D A, double B) { Vector2D temp = new Vector2D(); temp.x = A.x * B; temp.y = A.y * B; return temp; }
static public Vector2D operator *(float B, Vector2D A) { Vector2D temp = new Vector2D(); temp.x = A.x * (double)B; temp.y = A.y * (double)B; return temp; }
public void LengthAndDistance() { Random Rand = new Random(); Vector2D Test1 = new Vector2D(Rand.NextDouble() * 1000, Rand.NextDouble() * 1000); Vector2D Test2 = new Vector2D(Rand.NextDouble() * 1000, Rand.NextDouble() * 1000); Vector2D Test3 = Test1 + Test2; double Distance1 = Test2.GetLength(); double Distance2 = Vector2D.GetDistanceBetween(Test1, Test3); Assert.IsTrue(Distance1 < Distance2 + .001f && Distance1 > Distance2 - .001f); }
public void DotProduct() { Random Rand = new Random(); Vector2D TestVector2D1 = new Vector2D(Rand.NextDouble() * 1000, Rand.NextDouble() * 1000); Vector2D TestVector2D2 = new Vector2D(Rand.NextDouble() * 1000, Rand.NextDouble() * 1000); double Cross2D = TestVector2D1.Dot(TestVector2D2); Vector3D TestVector3D1 = new Vector3D(TestVector2D1.x, TestVector2D1.y, 0); Vector3D TestVector3D2 = new Vector3D(TestVector2D2.x, TestVector2D2.y, 0); double Cross3D = TestVector3D1.Dot(TestVector3D2); Assert.IsTrue(Cross3D == Cross2D); }
public void Rotate() { Vector2D TestVector = new Vector2D(1, 0); TestVector.Rotate((double)System.Math.PI / 2); Assert.IsTrue(TestVector.Equals(new Vector2D(0, 1), .001f)); }
public void ScalerOperations() { Vector2D ScalarMultiplicationArgument = new Vector2D(5.0f, 4.0f); Assert.IsTrue(ScalarMultiplicationArgument * -.5 == new Vector2D(-2.5f, -2)); Assert.IsTrue(ScalarMultiplicationArgument / 2 == new Vector2D(2.5f, 2)); Assert.IsTrue(2 / ScalarMultiplicationArgument == new Vector2D(2.5f, 2)); Assert.IsTrue(5 * ScalarMultiplicationArgument == new Vector2D(25, 20)); }
public void GetLengthAndNormalize() { Vector2D Point3 = new Vector2D(3, -4); Assert.IsTrue(Point3.GetLength() > 4.999f && Point3.GetLength() < 5.001f); Point3.Normalize(); Assert.IsTrue(Point3.GetLength() > 0.99f && Point3.GetLength() < 1.01f); }
// are they the same within the error value? public bool Equals(Vector2D OtherVector, double ErrorValue) { if ((x < OtherVector.x + ErrorValue && x > OtherVector.x - ErrorValue) && (y < OtherVector.y + ErrorValue && y > OtherVector.y - ErrorValue)) { return true; } return false; }
public double Cross(Vector2D B) { return x * B.y - y * B.x; }
public double Dot(Vector2D B) { return (x*B.x + y*B.y); }
public double GetDeltaAngle(Vector2D A) { return (double)GetDeltaAngle(GetAngle0To2PI(), A.GetAngle0To2PI()); }
public double GetSquaredDistanceTo(Vector2D Other) { return ((x - Other.x) * (x - Other.x) + (y - Other.y) * (y - Other.y)); }
public static double GetDistanceBetweenSquared(Vector2D A, Vector2D B) { return ((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); }
public static double GetDistanceBetween(Vector2D A, Vector2D B) { return (double)System.Math.Sqrt(GetDistanceBetweenSquared(A, B)); }
public Vector2D GetPerpendicular() { Vector2D temp = new Vector2D(y, -x); return temp; }
public void ArithmaticOperations() { Vector2D Point1 = new Vector2D(); Point1.Set(1, 1); Vector2D Point2 = new Vector2D(); Point2.Set(2, 2); Vector2D Point3 = Point1 + Point2; Assert.IsTrue(Point3 == new Vector2D(3, 3)); Point3 = Point1 - Point2; Assert.IsTrue(Point3 == new Vector2D(-1, -1)); Point3 += Point1; Assert.IsTrue(Point3 == new Vector2D(0, 0)); Point3 += Point2; Assert.IsTrue(Point3 == new Vector2D(2, 2)); Point3 *= 6; Assert.IsTrue(Point3 == new Vector2D(12, 12)); Vector2D InlineOpLeftSide = new Vector2D(5, -3); Vector2D InlineOpRightSide = new Vector2D(-5, 4); Assert.IsTrue(InlineOpLeftSide + InlineOpRightSide == new Vector2D(.0f, 1)); Assert.IsTrue(InlineOpLeftSide - InlineOpRightSide == new Vector2D(10.0f, -7)); }
static public Vector2D operator /(double B, Vector2D A) { Vector2D temp = new Vector2D(); temp.x = A.x / B; temp.y = A.y / B; return temp; }