Class Vector

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Vector
    extends java.lang.Object
    implements java.io.Serializable
    A class for defining a 2-dimensional vector. With different helpful methods to work with and manipulate vectors.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Vector ONE
      The constant ONE.
      static Vector UNIT_X
      The constant UNIT_X.
      static Vector UNIT_Y
      The constant UNIT_Y.
      float x
      The X.
      float y
      The Y.
      static Vector ZERO
      The constant ZERO.
    • Constructor Summary

      Constructors 
      Constructor Description
      Vector()
      Instantiates a new Vector.
      Vector​(float x, float y)
      Instantiates a new Vector.
      Vector​(Vector vec)
      Instantiates a new Vector.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Vector add​(float x, float y)
      Adds vector component-wise to a copy of this instance.
      Vector add​(Vector vec)
      Adds vectors component-wise and returns a new instance.
      float distance​(Vector p)
      Computes the Euclidean distance between two vectors.
      float distanceSqr​(Vector p)
      Computes the squared Euclidean distance between two vectors.
      Vector div​(float a)
      Divides both components of a copy of this instance by dividend.
      Vector div​(float x, float y)
      Divides vectors component-wise and returns a new instance.
      Vector div​(Vector vec)
      Divides vectors component-wise and returns a new instance.
      float dot​(Vector vec)
      Returns dot product (scalar product) of vectors.
      boolean equals​(Vector other)
      Checks if vectors have same values.
      Vector getNormalized()
      Get the normalized vector.
      float getX()
      Get the x component from the vector.
      float getY()
      Get the x component from the vector.
      float length()
      Returns the length of this vector.
      float lengthSqr()
      Returns the squared length of this vector.
      Vector lerp​(Vector vec, float t)
      Performs linear interpolation between two vectors.
      Vector mult​(float a)
      Multiplies both components of a copy of this instance with factor.
      Vector mult​(float x, float y)
      Multiplies vectors component-wise and returns a new instance.
      Vector mult​(Vector vec)
      Multiplies vectors component-wise and returns a new instance.
      void normalize()
      Normalize this vector.
      Vector sub​(float x, float y)
      Subtracts vector component-wise from a copy of this instance.
      Vector sub​(Vector vec)
      Subtracts vectors component-wise and returns a new instance.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • ZERO

        public static final Vector ZERO
        The constant ZERO.
      • ONE

        public static final Vector ONE
        The constant ONE.
      • UNIT_X

        public static final Vector UNIT_X
        The constant UNIT_X.
      • UNIT_Y

        public static final Vector UNIT_Y
        The constant UNIT_Y.
      • x

        public float x
        The X.
      • y

        public float y
        The Y.
    • Constructor Detail

      • Vector

        public Vector()
        Instantiates a new Vector.
      • Vector

        public Vector​(Vector vec)
        Instantiates a new Vector.
        Parameters:
        vec - the vec
      • Vector

        public Vector​(float x,
                      float y)
        Instantiates a new Vector.
        Parameters:
        x - the x
        y - the y
    • Method Detail

      • getX

        public float getX()
        Get the x component from the vector.
        Returns:
        the float
      • getY

        public float getY()
        Get the x component from the vector.
        Returns:
        the float
      • add

        public Vector add​(Vector vec)
        Adds vectors component-wise and returns a new instance. (Won't change this instance)
        Parameters:
        vec - incoming vector to add
        Returns:
        new resulting vector
      • add

        public Vector add​(float x,
                          float y)
        Adds vector component-wise to a copy of this instance. (Won't change this instance)
        Parameters:
        x - incoming x-Value to add
        y - incoming y-Value to add
        Returns:
        new resulting vector
      • sub

        public Vector sub​(Vector vec)
        Subtracts vectors component-wise and returns a new instance. (Won't change this instance)
        Parameters:
        vec - incoming vector to subtract
        Returns:
        new resulting vector
      • sub

        public Vector sub​(float x,
                          float y)
        Subtracts vector component-wise from a copy of this instance. (Won't change this instance)
        Parameters:
        x - incoming x-Value to subtract
        y - incoming y-Value to subtract
        Returns:
        new resulting vector
      • mult

        public Vector mult​(float a)
        Multiplies both components of a copy of this instance with factor. (Won't change this instance)
        Parameters:
        a - incoming factor to multiply
        Returns:
        new resulting vector
      • mult

        public Vector mult​(float x,
                           float y)
        Multiplies vectors component-wise and returns a new instance. (Won't change this instance)
        Parameters:
        x - incoming x-Value to multiply
        y - incoming y-Value to multiply
        Returns:
        new resulting vector
      • mult

        public Vector mult​(Vector vec)
        Multiplies vectors component-wise and returns a new instance. (Won't change this instance)
        Parameters:
        vec - incoming vector to multiply
        Returns:
        new resulting vector
      • div

        public Vector div​(float a)
        Divides both components of a copy of this instance by dividend. (Won't change this instance)
        Parameters:
        a - incoming dividend for division
        Returns:
        new resulting vector
      • div

        public Vector div​(float x,
                          float y)
        Divides vectors component-wise and returns a new instance. (Won't change this instance)
        Parameters:
        x - incoming x-Value to divide
        y - incoming y-Value to divide
        Returns:
        new resulting vector
      • div

        public Vector div​(Vector vec)
        Divides vectors component-wise and returns a new instance. (Won't change this instance)
        Parameters:
        vec - incoming vector to divide
        Returns:
        new resulting vector
      • dot

        public float dot​(Vector vec)
        Returns dot product (scalar product) of vectors.
        Parameters:
        vec - incoming vector
        Returns:
        dot product (scalar product)
      • length

        public float length()
        Returns the length of this vector.
        Returns:
        length float
      • lengthSqr

        public float lengthSqr()
        Returns the squared length of this vector.
        Returns:
        squared length
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • lerp

        public Vector lerp​(Vector vec,
                           float t)
        Performs linear interpolation between two vectors.
        Parameters:
        vec - second vector
        t - interpolation value [0,1]
        Returns:
        interpolated vector rounded integers
      • distance

        public float distance​(Vector p)
        Computes the Euclidean distance between two vectors.
        Parameters:
        p - second vector
        Returns:
        the Euclidean distance between this and the second vector
      • distanceSqr

        public float distanceSqr​(Vector p)
        Computes the squared Euclidean distance between two vectors.
        Parameters:
        p - second vector
        Returns:
        squared Euclidean distance between this and the second vector
      • equals

        public boolean equals​(Vector other)
        Checks if vectors have same values.
        Parameters:
        other - vector to be checked for equality
        Returns:
        if vectors have same values
      • normalize

        public void normalize()
        Normalize this vector. If this is a Zero-Vector, components will be set to NaN.
      • getNormalized

        public Vector getNormalized()
        Get the normalized vector. (Won't change this instance)
        Returns:
        This vector scaled to the length of 1. If this is a Zero-Vector, components will be set to NaN.