/// <summary> /// Turns the vector into a unit vector of lenght 1. /// </summary> /// <returns></returns> public Vect3D ToUnitVector() { Vect3D unitVector = this; unitVector *= (1 / GetLength()); return(unitVector); }
/// <summary> /// Calculates the angle between two vectors in radians. /// </summary> /// <param name="v1">First vector.</param> /// <param name="v2">Second vector.</param> /// <returns>Angle of the vectors in degrees.</returns> public static double AngleBetween(Vect3D v1, Vect3D v2) { double angle = Math.Round(v1 * v2 / (v1.GetLength() * v2.GetLength()), 10); angle = Math.Acos(angle); return(Math.Round(angle, 3)); }
/// <summary> /// Creates the cross product/vector product of two vectors. /// </summary> /// <param name="v1">First vector.</param> /// <param name="v2">Second vector.</param> /// <returns>Resulting vector.</returns> public static Vect3D VectorProduct(Vect3D v1, Vect3D v2) { Vect3D v3 = new Vect3D(); v3.X = v1.Y * v2.Z - v1.Z * v2.Y; v3.Y = v1.Z * v2.X - v1.X * v2.Z; v3.Z = v1.X * v2.Y - v1.Y * v2.X; return(v3); }
/// <summary> /// Checks if two vectors are parallel to each other. /// </summary> /// <param name="vector1">First vector.</param> /// <param name="vector2">Second vector.</param> /// <returns></returns> public static bool IsParallel(Vect3D vector1, Vect3D vector2) { //UNCHECKED may need to be rounded double scalar = vector1.X / vector2.X; if (vector1.Y == vector2.Y * scalar && vector1.Z == vector2.Z * scalar) { return(true); } return(false); }
/// <summary> /// Rotates a vector clockwise around a specified axis by the given angle. /// </summary> /// <param name="axis">Rotation axis.</param> /// <param name="angle">Rotation angle in radians.</param> /// <param name="copy">Creates a copy of the vector. False by default.</param> /// <returns></returns> public Vect3D RotateVector(Vect3D axis, double angle, bool copy = false) { axis *= (1 / axis.GetLength()); Matrix rotationMatrix = Matrix.CreateRotationMatrix(axis, angle); Vect3D rotated = rotationMatrix * this; if (!copy) { this = rotated; } return(rotated); }
/// <summary> /// Calculates the angle between a plane and a vector in radians. /// </summary> /// <param name="plane">First line.</param> /// <param name="vector">Second line.</param> /// <returns></returns> public static double AngleBetween(Plane3D plane, Vect3D vector) { return(Math.PI / 2 - AngleBetween(plane.NormalVector, vector)); }
/// <summary> /// Calculates the angle between a line and a vector in radians. /// </summary> /// <param name="line">A line.</param> /// <param name="vector">A vector.</param> /// <returns></returns> public static double AngleBetween(Line3D line, Vect3D vector) { return(AngleBetween(line.DirectionVector, vector)); }