} // End Function Schnittpunktli // https://en.wikipedia.org/wiki/Determinant // | a b | // | c d | private static T Determinant2d(T a, T b, T c, T d) { T f1 = Arithmetics <T> .Multiply(a, d); T f2 = Arithmetics <T> .Multiply(b, c); T result = Arithmetics <T> .Subtract(f1, f2); return(result); } // End Function Determinant2d
} // End Operator * public static MyVector2D <T> operator *(MyVector2D <T> a, T b) { MyVector2D <T> v = a.Clone(); v.X = Arithmetics <T> .Multiply(v.X, b); v.Y = Arithmetics <T> .Multiply(v.Y, b); return(v); } // End Operator *
} // End function CrossP // The dot product (also called the scalar product) is the magnitude of // vector b multiplied by the size of the projection of a onto b. // The size of the projection is a cosθ (where θ is the angle between the 2 vectors). // https://coderwall.com/p/icvt-g/2d-vector-dot-product // where theta is the angle between u and v, given by the dot product // cos(theta) = u dotp v public static T DotP(MyVector2D <T> a, MyVector2D <T> b) { T s1 = Arithmetics <T> .Multiply(a.X, b.X); T s2 = Arithmetics <T> .Multiply(a.Y, b.Y); //A * B = ax*bx+ay*by+az*bz T retValue = Arithmetics <T> .Add(s1, s2); return(retValue); } // End function DotP
} // End Property NormalVector // http://mathworld.wolfram.com/CrossProduct.html // the cross product is a vector that is perpendicular to both // a and b and thus normal to the plane containing them // |u x v| = |u| x |v| * sin(phi) public static T CrossP(MyVector2D <T> a, MyVector2D <T> b) { // crossp = det(a,b) = a.X*b.Y- a.Y*b.X T s1 = Arithmetics <T> .Multiply(a.X, b.Y); T s2 = Arithmetics <T> .Multiply(a.Y, b.X); T retValue = Arithmetics <T> .Subtract(s1, s2); return(retValue); } // End function CrossP
} // End Function ToString /// <summary> /// Returns a new Vector that is the linear blend of the 2 given Vectors /// </summary> /// <param name="a">First input vector</param> /// <param name="b">Second input vector</param> /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param> /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns> public static MyPoint2D <T> Lerp(MyPoint2D <T> a, MyPoint2D <T> b, T blend) { T bxax = Arithmetics <T> .Subtract(b.X, a.X); T byay = Arithmetics <T> .Subtract(b.Y, a.Y); T f1 = Arithmetics <T> .Multiply(blend, bxax); T f2 = Arithmetics <T> .Multiply(blend, byay); T x = Arithmetics <T> .Add(f1, a.X); T y = Arithmetics <T> .Add(f2, a.Y); return(new MyPoint2D <T>(x, y)); } // End Function Lerp
} // End Function ToString /// <summary> /// Returns a new Vector that is the linear blend of the 2 given Vectors /// </summary> /// <param name="a">First input vector</param> /// <param name="b">Second input vector</param> /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param> /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns> public static MyVector3D <T> Lerp(MyVector3D <T> a, MyVector3D <T> b, T blend) { T bxax = Arithmetics <T> .Subtract(b.X, a.X); T byay = Arithmetics <T> .Subtract(b.Y, a.Y); T bzaz = Arithmetics <T> .Subtract(b.Z, a.Z); T f1 = Arithmetics <T> .Multiply(blend, bxax); T f2 = Arithmetics <T> .Multiply(blend, byay); T f3 = Arithmetics <T> .Multiply(blend, bzaz); T x = Arithmetics <T> .Add(f1, a.X); T y = Arithmetics <T> .Add(f2, a.Y); T z = Arithmetics <T> .Add(f3, a.Z); return(new MyVector3D <T>(x, y, z)); } // End Function Lerp
} // End function DotP public static T Angle_Rad(MyVector2D <T> a, MyVector2D <T> b) { T axbx = Arithmetics <T> .Multiply(a.X, b.X); T ayby = Arithmetics <T> .Multiply(a.Y, b.Y); T azbz = Arithmetics <T> .ZERO; T sumAB = Arithmetics <T> .Sum(axbx, ayby, azbz); T ax2 = Arithmetics <T> .Pow(a.X, 2); T ay2 = Arithmetics <T> .Pow(a.Y, 2); T az2 = Arithmetics <T> .ZERO; T bx2 = Arithmetics <T> .Pow(b.X, 2); T by2 = Arithmetics <T> .Pow(b.Y, 2); T bz2 = Arithmetics <T> .ZERO; T aSquare = Arithmetics <T> .Sum(ax2, ay2, az2); T bSquare = Arithmetics <T> .Sum(bx2, by2, bz2); T val = Arithmetics <T> .Divide(sumAB, ( Arithmetics <T> .Multiply(Arithmetics <T> .Sqrt(aSquare), Arithmetics <T> .Sqrt(bSquare)) ) ); T nReturnValue = Arithmetics <T> .Acos(val); return(nReturnValue); } // End function Angle_Rad
} // End Function GetNormalVector /// <summary> /// Returns the CrossProduct. /// </summary> /// <returns>Vector<T> containing the value of the CrossProduct.</returns> public static MyVector3D <T> CrossP(MyVector3D <T> a, MyVector3D <T> b) { T x1 = Arithmetics <T> .Multiply(a.Y, b.Z); T x2 = Arithmetics <T> .Multiply(a.Z, b.Y); T y1 = Arithmetics <T> .Multiply(a.Z, b.X); T y2 = Arithmetics <T> .Multiply(a.X, b.Z); T z1 = Arithmetics <T> .Multiply(a.X, b.Y); T z2 = Arithmetics <T> .Multiply(a.Y, b.X); //A × B = [(ay*bz-az*by),(az*bx-ax*bz),(ax*by-ay*bx)] MyVector3D <T> vecReturnValue = new MyVector3D <T>( Arithmetics <T> .Subtract(x1, x2) , Arithmetics <T> .Subtract(y1, y2) , Arithmetics <T> .Subtract(z1, z2) ); return(vecReturnValue); } // End function CrossP
public void TestMethod1() { Arithmetics a = new Arithmetics(); Assert.AreEqual(5, a.Multiply(2, 2)); }