Class Vec2


  • public class Vec2
    extends java.lang.Object
    Two-component vector with X and Y coordinates.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      double x
      The vector's X component.
      double y
      The vector's Y component.
    • Constructor Summary

      Constructors 
      Constructor Description
      Vec2()
      Constructs a two-component vector with X and Y both 0.
      Vec2​(double x, double y)
      Constructs a two-component vector with a specified X and Y.
      Vec2​(Vec2 vector)
      Constructs a two-component vector with the X and Y of a specified vector.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Vec2 add​(Vec2 vector)
      Adds a specified vector to this vector.
      double distanceTo​(Vec2 vector)
      Computes the distance from this vector to another vector.
      double distanceToSquared​(Vec2 vector)
      Computes the squared distance from this vector to a specified vector.
      Vec2 divide​(double divisor)
      Divides this vector by a scalar.
      double dot​(Vec2 vector)
      Computes the scalar dot product of this vector and a specified vector.
      boolean equals​(java.lang.Object o)  
      int hashCode()  
      double magnitude()
      Computes the magnitude of this vector.
      double magnitudeSquared()
      Computes the squared magnitude of this vector.
      Vec2 mix​(Vec2 vector, double weight)
      Mixes (interpolates) a specified vector with this vector, modifying this vector.
      Vec2 multiply​(double scalar)
      Multiplies this vector by a scalar.
      Vec2 multiplyByMatrix​(Matrix3 matrix)
      Multiplies this vector by a 3x3 matrix.
      Vec2 negate()
      Negates the components of this vector.
      Vec2 normalize()
      Normalizes this vector to a unit vector.
      Vec2 set​(double x, double y)
      Sets this vector to the specified X and Y.
      Vec2 set​(Vec2 vector)
      Sets this vector to the X and Y of a specified vector.
      Vec2 subtract​(Vec2 vector)
      Subtracts a specified vector from this vector.
      Vec2 swap​(Vec2 vector)
      Swaps this vector with the specified vector.
      float[] toArray​(float[] result, int offset)
      Copies this vector's components to the specified single precision array.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • x

        public double x
        The vector's X component.
      • y

        public double y
        The vector's Y component.
    • Constructor Detail

      • Vec2

        public Vec2()
        Constructs a two-component vector with X and Y both 0.
      • Vec2

        public Vec2​(double x,
                    double y)
        Constructs a two-component vector with a specified X and Y.
        Parameters:
        x - the vector's X component
        y - the vector's Y component
      • Vec2

        public Vec2​(Vec2 vector)
        Constructs a two-component vector with the X and Y of a specified vector.
        Parameters:
        vector - the vector specifying the components
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
    • Method Detail

      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        @NonNull
        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toArray

        public float[] toArray​(float[] result,
                               int offset)
        Copies this vector's components to the specified single precision array. The result is compatible with GLSL uniform vectors, and can be passed to the function glUniform2fv.
        Parameters:
        result - a pre-allocated array of length 2 in which to return the components
        Returns:
        the result argument set to this vector's components
      • magnitude

        public double magnitude()
        Computes the magnitude of this vector.
        Returns:
        the magnitude of this vector
      • magnitudeSquared

        public double magnitudeSquared()
        Computes the squared magnitude of this vector. This is equivalent to squaring the result of magnitude but is potentially much more efficient.
        Returns:
        the squared magnitude of this vector
      • distanceTo

        public double distanceTo​(Vec2 vector)
        Computes the distance from this vector to another vector.
        Parameters:
        vector - the vector to compute the distance to
        Returns:
        the distance between the vectors
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
      • distanceToSquared

        public double distanceToSquared​(Vec2 vector)
        Computes the squared distance from this vector to a specified vector. This is equivalent to squaring the result of distanceTo but is potentially much more efficient.
        Parameters:
        vector - the vector to compute the distance to
        Returns:
        the squared distance between the vectors
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
      • set

        public Vec2 set​(double x,
                        double y)
        Sets this vector to the specified X and Y.
        Parameters:
        x - the new X component
        y - the new Y component
        Returns:
        this vector set to the specified values
      • set

        public Vec2 set​(Vec2 vector)
        Sets this vector to the X and Y of a specified vector.
        Parameters:
        vector - the vector specifying the new components
        Returns:
        this vector with its X and Y set to that of the specified vector
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
      • swap

        public Vec2 swap​(Vec2 vector)
        Swaps this vector with the specified vector. This vector's components are set to the values of the specified vector's components, and the specified vector's components are set to the values of this vector's components.
        Parameters:
        vector - the vector to swap with this vector
        Returns:
        this vector set to the values of the specified vector
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
      • add

        public Vec2 add​(Vec2 vector)
        Adds a specified vector to this vector.
        Parameters:
        vector - the vector to add
        Returns:
        this vector after adding the specified vector to it
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
      • subtract

        public Vec2 subtract​(Vec2 vector)
        Subtracts a specified vector from this vector.
        Parameters:
        vector - the vector to subtract
        Returns:
        this vector after subtracting the specified vector from it
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
      • multiply

        public Vec2 multiply​(double scalar)
        Multiplies this vector by a scalar.
        Parameters:
        scalar - the scalar to multiply this vector by
        Returns:
        this vector multiplied by the specified scalar
      • multiplyByMatrix

        public Vec2 multiplyByMatrix​(Matrix3 matrix)
        Multiplies this vector by a 3x3 matrix. The multiplication is performed with an implicit Z component of 1. The resultant Z component of the product is then divided through the X and Y components.
        Parameters:
        matrix - the matrix to multiply this vector by
        Returns:
        this vector multiplied by the specified matrix
        Throws:
        java.lang.IllegalArgumentException - If the matrix is null
      • divide

        public Vec2 divide​(double divisor)
        Divides this vector by a scalar.
        Parameters:
        divisor - the scalar to divide this vector by
        Returns:
        this vector divided by the specified scalar
      • negate

        public Vec2 negate()
        Negates the components of this vector.
        Returns:
        this vector, negated
      • normalize

        public Vec2 normalize()
        Normalizes this vector to a unit vector.
        Returns:
        this vector, normalized
      • dot

        public double dot​(Vec2 vector)
        Computes the scalar dot product of this vector and a specified vector.
        Parameters:
        vector - the vector to multiply
        Returns:
        the dot product of the two vectors
        Throws:
        java.lang.IllegalArgumentException - If the vector is null
      • mix

        public Vec2 mix​(Vec2 vector,
                        double weight)
        Mixes (interpolates) a specified vector with this vector, modifying this vector.
        Parameters:
        vector - The vector to mix with this one
        weight - The relative weight of this vector, typically in the range [0,1]
        Returns:
        this vector modified to the mix of itself and the specified vector
        Throws:
        java.lang.IllegalArgumentException - If the vector is null