Class VectorSupport

java.lang.Object
cnuphys.swim.util.VectorSupport

public class VectorSupport extends Object
Static classes for treating arrays of doubles as 2D or 3D vectors
Author:
heddle
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    angleBetween(double[] v1, double[] v2)
    Get the angle between two vectors
    static double[]
    createVector(double... comp)
    Create a vector as a double array from its components
    static double[]
    cross(double[] v1, double[] v2)
    Get the vector cross product.
    static double[]
    diff(double[] a, double[] b)
    The vector difference a - b
    static double
    dot(double[] v1, double[] v2)
    Usual inner product
    static double
    length(double[] v)
    Get the magnitude of the vector.
    static double
    length(float[] v)
    Get the magnitude of the vector.
    static double
    lengthSquared(double[] v)
    Get the square of the magnitude of the vector.
    static double
    lengthSquared(float[] v)
    Get the square of the magnitude of the vector.
    static double[]
    project(double[] v, double[] a)
    Project one vector onto another vector
    void
    scale(double[] v, double scaleFactor)
    Scale the vector
    static void
    set(double[] v, double... components)
    Set all the components
    static String
    toString(double[] v)
    Returns a string representation of the form: "(x,y,z)" using three decimals points.
    static String
    toString(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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 vector
      scaleFactor - the scale factor
    • toString

      public static String toString(double[] v, int numDec)
      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

      public static String toString(double[] v)
      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 length
      v2 - 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 vector
      v2 - 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 projected
      a - 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 vector
      b - the vector being subtracted
      Returns:
      the difference a - b