Class Vector


  • public class Vector
    extends Object
    A class to describe a two or three dimensional vector. This class API aims to conform that of the great Processing PVector.

    The result of all functions are applied to the vector itself, with the exception of cross(), which returns a new vector (or writes to a specified 'target' vector). That is, add() will add the contents of one vector to this one. Using add() with additional parameters allows you to put the result into a new vector. Functions that act on multiple vectors also include static versions. Because creating new objects can be computationally expensive, most functions include an optional 'target' vector, so that a new vector object is not created with each operation.

    Initially based on the Processing PVector class.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      float[] _vector
      The x, y and z coordinates of the Vector.
      static Vector minusI
      Opposite of the first canonical vector (coordinates: -1, 0, 0).
      static Vector minusJ
      Opposite of the second canonical vector (coordinates: 0, -1, 0).
      static Vector minusK
      Opposite of the third canonical vector (coordinates: 0, 0, -1).
      static Vector plusI
      First canonical vector (coordinates: 1, 0, 0).
      static Vector plusJ
      Second canonical vector (coordinates: 0, 1, 0).
      static Vector plusK
      Third canonical vector (coordinates: 0, 0, 1).
      static Vector zero
      Zero vector (coordinates: 0, 0, 0).
    • Constructor Summary

      Constructors 
      Constructor Description
      Vector()
      Constructor for an empty vector: x, y, and z are set to 0.
      Vector​(float x, float y)
      Constructor for a 2D vector: z coordinate is set to 0.
      Vector​(float x, float y, float z)
      Constructor for a 3D vector.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(float x, float y, float z)  
      void add​(Vector vector)
      Add a vector to this vector
      static Vector add​(Vector vector1, Vector vector2)
      Add two vectors.
      static Vector add​(Vector vector1, Vector vector2, Vector target)
      Add two vectors into a target vector.
      static float angleBetween​(Vector vector1, Vector vector2)
      Calculate the angle between two vectors, using the dot product.
      Vector copy()
      Returns a deep copy of this vector.
      Vector cross​(Vector v)
      Return a vector composed of the cross product between this and another.
      Vector cross​(Vector vector, Vector target)
      Perform cross product between this and another vector, and store the result in 'target'.
      static Vector cross​(Vector vector1, Vector vector2, Vector target)
      Cross product: target = vector1 * vector2.
      float distance​(Vector vector)
      Calculate the Euclidean distance between two points (considering a point as a vector object).
      static float distance​(Vector vector1, Vector vector2)
      Calculate the Euclidean distance between two points (considering a point as a vector object).
      void divide​(float n)
      Divide this vector by a scalar.
      static Vector divide​(Vector vector, float n)
      Divide a vector by a scalar and return the result in a new vector.
      static Vector divide​(Vector vector, float n, Vector target)
      Divide a vector by a scalar and store the result in another vector.
      float dot​(float x, float y, float z)  
      float dot​(Vector vector)
      Calculate the dot product with another vector.
      static float dot​(Vector vector1, Vector vector2)  
      static Vector fromAngle​(float angle)
      Make a new 2D unit vector from an angle.
      static Vector fromAngle​(float angle, Vector target)
      Make a new 2D unit vector from an angle.
      float[] get​(float[] target)
      Returns this vector as an array.
      float heading()
      Calculate the angle of rotation for this vector (only 2D vectors).
      static float lerp​(float start, float stop, float amount)
      Calculates a number between two numbers at a specific increment.
      void lerp​(float x, float y, float z, float amount)
      Linear interpolate the vector to x,y,z values.
      void lerp​(Vector vector, float amount)
      Linear interpolate the vector to another vector.
      static Vector lerp​(Vector vector1, Vector vector2, float amount)
      Linear interpolate between two vectors (returns a new Vector object).
      void limit​(float maximum)
      Limit the magnitude of this vector.
      float magnitude()
      Calculate the magnitude (length) of the vector
      boolean matches​(Vector vector)
      Returns whether or not this vector matches other.
      void multiply​(float n)
      Multiply this vector by a scalar.
      static Vector multiply​(Vector vector, float n)
      Multiply a vector by a scalar.
      static Vector multiply​(Vector vector, float n, Vector target)
      Multiply a vector by a scalar, and write the result into a target vector.
      void normalize()
      Normalize the vector to length 1 (make it a unit vector).
      Vector normalize​(Vector target)
      Normalize this vector, storing the result in another vector.
      Vector orthogonalVector()
      Same as return orthogonalVector(this).
      static Vector orthogonalVector​(Vector vector)
      Utility function that returns a vector orthogonal to vector.
      Vector projectVectorOnAxis​(Vector direction)
      Same as return projectVectorOnAxis(this, direction).
      static Vector projectVectorOnAxis​(Vector vector, Vector direction)
      Projects the vector on the axis defined by direction (which does not need to be normalized, but must be non null) that passes through the origin.
      Vector projectVectorOnPlane​(Vector normal)
      Same as projectVectorOnPlane(this, normal).
      static Vector projectVectorOnPlane​(Vector vector, Vector normal)
      Projects vector on the plane defined by normal (which does not need to be normalized, but must be non null) that passes through the origin.
      static Vector random()
      Returns a normalized random vector.
      void randomize()
      Randomize this vector.
      void reset()
      Sets all vector components to 0.
      void rotate​(float theta)
      Rotate the vector by an angle (only 2D vectors), magnitude remains the same.
      static float scalarProjection​(Vector a, Vector b)
      Returns the scalar projection of a onto b.
      void set​(float[] source)
      Set the x, y (and maybe z) coordinates using a float[] array as the source.
      void set​(float x, float y)
      Set x and y coordinates.
      void set​(float x, float y, float z)
      Set x, y, and z coordinates.
      void set​(Vector vector)
      Set x, y, and z coordinates from a Vector object.
      void setMagnitude​(float magnitude)
      Sets the magnitude of the vector to an arbitrary amount.
      Vector setMagnitude​(Vector target, float magnitude)
      Sets the magnitude of this vector, storing the result in another vector.
      void setX​(float x)
      Sets the x component of the vector.
      void setY​(float y)
      Sets the y component of the vector.
      void setZ​(float z)
      Sets the z component of the vector.
      float squaredMagnitude()
      Calculate the squared magnitude of the vector.
      float squaredNorm()
      Same as return squaredNorm(this).
      static float squaredNorm​(Vector vector)
      Utility function that returns the squared norm of the vector.
      void subtract​(float x, float y, float z)  
      void subtract​(Vector vector)
      Subtract a vector from this vector.
      static Vector subtract​(Vector vector1, Vector vector2)
      Subtract one vector from another.
      static Vector subtract​(Vector vector1, Vector vector2, Vector target)
      Subtract one vector from another and store in another vector.
      String toString()
      Return this vector components as a String.
      static Vector vectorProjection​(Vector a, Vector b)
      Returns the vector projection of a onto b.
      float x()
      Returns the x component of the vector.
      float y()
      Returns the y component of the vector.
      float z()
      Returns the z component of the vector.
    • Field Detail

      • _vector

        public float[] _vector
        The x, y and z coordinates of the Vector.
      • plusI

        public static final Vector plusI
        First canonical vector (coordinates: 1, 0, 0).
      • minusI

        public static final Vector minusI
        Opposite of the first canonical vector (coordinates: -1, 0, 0).
      • plusJ

        public static final Vector plusJ
        Second canonical vector (coordinates: 0, 1, 0).
      • minusJ

        public static final Vector minusJ
        Opposite of the second canonical vector (coordinates: 0, -1, 0).
      • plusK

        public static final Vector plusK
        Third canonical vector (coordinates: 0, 0, 1).
      • minusK

        public static final Vector minusK
        Opposite of the third canonical vector (coordinates: 0, 0, -1).
      • zero

        public static final Vector zero
        Zero vector (coordinates: 0, 0, 0).
    • Constructor Detail

      • Vector

        public Vector()
        Constructor for an empty vector: x, y, and z are set to 0.
      • Vector

        public Vector​(float x,
                      float y,
                      float z)
        Constructor for a 3D vector.
        Parameters:
        x - the x coordinate.
        y - the y coordinate.
        z - the y coordinate.
      • Vector

        public Vector​(float x,
                      float y)
        Constructor for a 2D vector: z coordinate is set to 0.
        Parameters:
        x - the x coordinate.
        y - the y coordinate.
    • Method Detail

      • matches

        public boolean matches​(Vector vector)
        Returns whether or not this vector matches other.
        Parameters:
        vector - other vector
      • randomize

        public void randomize()
        Randomize this vector. The vector is normalized too.
        See Also:
        random()
      • random

        public static Vector random()
        Returns a normalized random vector.
        See Also:
        randomize()
      • x

        public float x()
        Returns the x component of the vector.
      • y

        public float y()
        Returns the y component of the vector.
      • z

        public float z()
        Returns the z component of the vector.
      • setX

        public void setX​(float x)
        Sets the x component of the vector.
      • setY

        public void setY​(float y)
        Sets the y component of the vector.
      • setZ

        public void setZ​(float z)
        Sets the z component of the vector.
      • vectorProjection

        public static Vector vectorProjection​(Vector a,
                                              Vector b)
        Returns the vector projection of a onto b. Vector b should be normalized. See: https://en.wikipedia.org/wiki/Vector_projection
      • scalarProjection

        public static float scalarProjection​(Vector a,
                                             Vector b)
        Returns the scalar projection of a onto b. Vector b should be normalized. See: https://en.wikipedia.org/wiki/Scalar_projection
      • projectVectorOnAxis

        public static Vector projectVectorOnAxis​(Vector vector,
                                                 Vector direction)
        Projects the vector on the axis defined by direction (which does not need to be normalized, but must be non null) that passes through the origin.
      • projectVectorOnPlane

        public static Vector projectVectorOnPlane​(Vector vector,
                                                  Vector normal)
        Projects vector on the plane defined by normal (which does not need to be normalized, but must be non null) that passes through the origin.
      • squaredNorm

        public float squaredNorm()
        Same as return squaredNorm(this).
        See Also:
        squaredNorm(Vector)
      • squaredNorm

        public static float squaredNorm​(Vector vector)
        Utility function that returns the squared norm of the vector.
      • orthogonalVector

        public static Vector orthogonalVector​(Vector vector)
        Utility function that returns a vector orthogonal to vector. Its magnitude() depends on the vector, but is zero only for a null vector. Note that the function that associates an orthogonalVector() to a vector is not continuous.
      • reset

        public void reset()
        Sets all vector components to 0.
      • set

        public void set​(float x,
                        float y)
        Set x and y coordinates. The z coordinate is set to 0.
        Parameters:
        x - the x coordinate.
        y - the y coordinate.
        See Also:
        set(float, float, float)
      • set

        public void set​(float x,
                        float y,
                        float z)
        Set x, y, and z coordinates.
        Parameters:
        x - the x coordinate.
        y - the y coordinate.
        z - the z coordinate.
        See Also:
        set(float, float)
      • set

        public void set​(Vector vector)
        Set x, y, and z coordinates from a Vector object.
        Parameters:
        vector - the Vector object to be copied
      • set

        public void set​(float[] source)
        Set the x, y (and maybe z) coordinates using a float[] array as the source.
        Parameters:
        source - array to copy from
      • copy

        public Vector copy()
        Returns a deep copy of this vector.
      • get

        public float[] get​(float[] target)
        Returns this vector as an array.
      • magnitude

        public float magnitude()
        Calculate the magnitude (length) of the vector
        Returns:
        the magnitude of the vector
      • squaredMagnitude

        public float squaredMagnitude()
        Calculate the squared magnitude of the vector.
        Returns:
        squared magnitude of the vector
      • add

        public void add​(Vector vector)
        Add a vector to this vector
        Parameters:
        vector - the vector to be added
      • add

        public void add​(float x,
                        float y,
                        float z)
        Parameters:
        x - x component of the vector
        y - y component of the vector
        z - z component of the vector
      • add

        public static Vector add​(Vector vector1,
                                 Vector vector2)
        Add two vectors.
        Parameters:
        vector1 - a vector
        vector2 - another vector
        Returns:
        a new vector that is the sum of vector1 and vector2
      • add

        public static Vector add​(Vector vector1,
                                 Vector vector2,
                                 Vector target)
        Add two vectors into a target vector.
        Parameters:
        vector1 - a vector
        vector2 - another vector
        target - the target vector (if null, a new vector will be created)
        Returns:
        a new vector that is the sum of vector1 and vector2
      • subtract

        public void subtract​(Vector vector)
        Subtract a vector from this vector.
        Parameters:
        vector - the vector to be subtracted
      • subtract

        public void subtract​(float x,
                             float y,
                             float z)
        Parameters:
        x - the x component of the vector
        y - the y component of the vector
        z - the z component of the vector
      • subtract

        public static Vector subtract​(Vector vector1,
                                      Vector vector2)
        Subtract one vector from another.
        Parameters:
        vector1 - a vector
        vector2 - another vector
        Returns:
        a new vector that is vector1 - vector2
      • subtract

        public static Vector subtract​(Vector vector1,
                                      Vector vector2,
                                      Vector target)
        Subtract one vector from another and store in another vector.
        Parameters:
        vector1 - the x, y, and z components of a Vector object
        vector2 - the x, y, and z components of a Vector object
        target - Vector in which to store the result
      • multiply

        public void multiply​(float n)
        Multiply this vector by a scalar.
        Parameters:
        n - the value to multiply by
      • multiply

        public static Vector multiply​(Vector vector,
                                      float n)
        Multiply a vector by a scalar.
        Parameters:
        vector - a vector
        n - scalar
        Returns:
        a new vector that is vector * n
      • multiply

        public static Vector multiply​(Vector vector,
                                      float n,
                                      Vector target)
        Multiply a vector by a scalar, and write the result into a target vector.
        Parameters:
        vector - a vector
        n - scalar
        target - Vector to store the result
        Returns:
        the target vector, now set to vector * n
      • divide

        public void divide​(float n)
        Divide this vector by a scalar.
        Parameters:
        n - the value to divide by
      • divide

        public static Vector divide​(Vector vector,
                                    float n)
        Divide a vector by a scalar and return the result in a new vector.
        Parameters:
        vector - a vector
        n - scalar
        Returns:
        a new vector that is vector / n
      • divide

        public static Vector divide​(Vector vector,
                                    float n,
                                    Vector target)
        Divide a vector by a scalar and store the result in another vector.
        Parameters:
        target - Vector in which to store the result
      • distance

        public float distance​(Vector vector)
        Calculate the Euclidean distance between two points (considering a point as a vector object).
        Parameters:
        vector - another vector
        Returns:
        the Euclidean distance between
      • distance

        public static float distance​(Vector vector1,
                                     Vector vector2)
        Calculate the Euclidean distance between two points (considering a point as a vector object).
        Parameters:
        vector1 - a vector
        vector2 - another vector
        Returns:
        the Euclidean distance between vector1 and vector2
      • dot

        public float dot​(Vector vector)
        Calculate the dot product with another vector.
        Returns:
        the dot product
      • dot

        public float dot​(float x,
                         float y,
                         float z)
        Parameters:
        x - x component of the vector
        y - y component of the vector
        z - z component of the vector
      • dot

        public static float dot​(Vector vector1,
                                Vector vector2)
        Parameters:
        vector1 - any variable of type Vector
        vector2 - any variable of type Vector
      • cross

        public Vector cross​(Vector v)
        Return a vector composed of the cross product between this and another.
      • cross

        public Vector cross​(Vector vector,
                            Vector target)
        Perform cross product between this and another vector, and store the result in 'target'. If target is null, a new vector is created.
      • cross

        public static Vector cross​(Vector vector1,
                                   Vector vector2,
                                   Vector target)
        Cross product: target = vector1 * vector2.
        Parameters:
        vector1 - any variable of type Vector
        vector2 - any variable of type Vector
        target - Vector to store the result
      • normalize

        public void normalize()
        Normalize the vector to length 1 (make it a unit vector).
      • normalize

        public Vector normalize​(Vector target)
        Normalize this vector, storing the result in another vector.
        Parameters:
        target - Set to null to create a new vector
        Returns:
        a new vector (if target was null), or target
      • limit

        public void limit​(float maximum)
        Limit the magnitude of this vector.
        Parameters:
        maximum - the maximum length to limit this vector
      • setMagnitude

        public void setMagnitude​(float magnitude)
        Sets the magnitude of the vector to an arbitrary amount.
        Parameters:
        magnitude - the new length for this vector
      • setMagnitude

        public Vector setMagnitude​(Vector target,
                                   float magnitude)
        Sets the magnitude of this vector, storing the result in another vector.
        Parameters:
        target - Set to null to create a new vector
        magnitude - the new length for the new vector
        Returns:
        a new vector (if target was null), or target
      • heading

        public float heading()
        Calculate the angle of rotation for this vector (only 2D vectors).
        Returns:
        the angle of rotation
      • rotate

        public void rotate​(float theta)
        Rotate the vector by an angle (only 2D vectors), magnitude remains the same.
        Parameters:
        theta - the angle of rotation
      • lerp

        public static float lerp​(float start,
                                 float stop,
                                 float amount)
        Calculates a number between two numbers at a specific increment. The amount parameter is the amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, etc.
      • lerp

        public void lerp​(Vector vector,
                         float amount)
        Linear interpolate the vector to another vector.
        Parameters:
        vector - the vector to lerp to
        amount - The amt parameter is the amount to interpolate between the two vectors where 1.0 equal to the new vector 0.1 is very near the new vector, 0.5 is half-way in between.
      • lerp

        public static Vector lerp​(Vector vector1,
                                  Vector vector2,
                                  float amount)
        Linear interpolate between two vectors (returns a new Vector object).
        Parameters:
        vector1 - the vector to start from
        vector2 - the vector to lerp to
      • lerp

        public void lerp​(float x,
                         float y,
                         float z,
                         float amount)
        Linear interpolate the vector to x,y,z values.
        Parameters:
        x - the x component to lerp to
        y - the y component to lerp to
        z - the z component to lerp to
      • angleBetween

        public static float angleBetween​(Vector vector1,
                                         Vector vector2)
        Calculate the angle between two vectors, using the dot product.
        Parameters:
        vector1 - a vector
        vector2 - another vector
        Returns:
        the angle between the vectors
      • fromAngle

        public static Vector fromAngle​(float angle)
        Make a new 2D unit vector from an angle.
        Parameters:
        angle - the angle
        Returns:
        the new unit PVec
      • fromAngle

        public static Vector fromAngle​(float angle,
                                       Vector target)
        Make a new 2D unit vector from an angle.
        Parameters:
        angle - the angle
        target - the target vector (if null, a new vector will be created)
        Returns:
        the Vector
      • toString

        public String toString()
        Return this vector components as a String.
        Overrides:
        toString in class Object