Package cnuphys.swim.util
Class VectorSupport
java.lang.Object
cnuphys.swim.util.VectorSupport
Static classes for treating arrays of doubles as 2D or 3D vectors
- Author:
- heddle
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleangleBetween(double[] v1, double[] v2) Get the angle between two vectorsstatic double[]createVector(double... comp) Create a vector as a double array from its componentsstatic double[]cross(double[] v1, double[] v2) Get the vector cross product.static double[]diff(double[] a, double[] b) The vector difference a - bstatic doubledot(double[] v1, double[] v2) Usual inner productstatic doublelength(double[] v) Get the magnitude of the vector.static doublelength(float[] v) Get the magnitude of the vector.static doublelengthSquared(double[] v) Get the square of the magnitude of the vector.static doublelengthSquared(float[] v) Get the square of the magnitude of the vector.static double[]project(double[] v, double[] a) Project one vector onto another vectorvoidscale(double[] v, double scaleFactor) Scale the vectorstatic voidset(double[] v, double... components) Set all the componentsstatic StringtoString(double[] v) Returns a string representation of the form: "(x,y,z)" using three decimals points.static StringtoString(double[] v, int numDec) Returns a string representation of the form: "(x,y,z)".static double[]unitVector(double[] v) Obtain a unit vector in the same direction as this vector
-
Constructor Details
-
VectorSupport
public VectorSupport()
-
-
Method Details
-
createVector
public static double[] createVector(double... comp) Create a vector as a double array from its components- Parameters:
comp- an arbitrary number of components- Returns:
- the vector
-
lengthSquared
public static double lengthSquared(float[] v) Get the square of the magnitude of the vector.- Parameters:
v- the vector (of any length)- Returns:
- the square of the magnitude of the vector
-
lengthSquared
public static double lengthSquared(double[] v) Get the square of the magnitude of the vector.- Parameters:
v- the vector (of any length)- Returns:
- the square of the magnitude of the vector
-
length
public static double length(float[] v) Get the magnitude of the vector.- Parameters:
v- the vector (of any length)- Returns:
- the magnitude of the vector
-
length
public static double length(double[] v) Get the magnitude of the vector.- Parameters:
v- the vector (of any length)- Returns:
- the magnitude of the vector
-
set
public static void set(double[] v, double... components) Set all the components- Parameters:
v- the vector (of any length)components- the vector components. The number of entries should match the length of v.
-
scale
public void scale(double[] v, double scaleFactor) Scale the vector- Parameters:
v- the vectorscaleFactor- the scale factor
-
toString
Returns a string representation of the form: "(x,y,z)".- Parameters:
v- the vector (of any length)numDec- the number of decimal places for each coordinate.- Returns:
- a String representation of the vector
-
toString
Returns a string representation of the form: "(x,y,z)" using three decimals points.- Parameters:
v- the vector (of any length)numDec- the number of decimal places for each coordinate.- Returns:
- a String representation of the vector
-
unitVector
public static double[] unitVector(double[] v) Obtain a unit vector in the same direction as this vector- Parameters:
v- the vector (of any length)- Returns:
- a unit vector in the same direction as this vector
-
dot
public static double dot(double[] v1, double[] v2) Usual inner product- Parameters:
v1- one vector on any lengthv2- another vector of matching length- Returns:
- the dot product
-
cross
public static double[] cross(double[] v1, double[] v2) Get the vector cross product. The vectors must match and be 2D or 3D. A 3D vector is return. For for two 2D inputs, the result look like- Parameters:
v1- one vectorv2- another vector- Returns:
- the magnitude of the cross product
-
angleBetween
public static double angleBetween(double[] v1, double[] v2) Get the angle between two vectors- Parameters:
v1- one vector of any length (2 or 3)v2- another vector of matching length- Returns:
- the angle between the vectors in degrees.
-
project
public static double[] project(double[] v, double[] a) Project one vector onto another vector- Parameters:
v- the vector being projecteda- the vector defining the projection direction.- Returns:
- the result of projecting v in the direction of a.
-
diff
public static double[] diff(double[] a, double[] b) The vector difference a - b- Parameters:
a- one vectorb- the vector being subtracted- Returns:
- the difference a - b
-