Class Vector2D

java.lang.Object
technology.sola.math.linear.Vector2D

@NullMarked public class Vector2D extends Object
The Vector2D class is an implementation of a linear algebra vector.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Vector2D(float x, float y)
    Create a Vector2D instance with x and y set.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Vector2D vector2D)
    Calculates the sum of two vectors and returns the sum as a new vector object.
    float
    distance(Vector2D vector2D)
    Calculates the distance between two vectors.
    float
    distanceSq(Vector2D vector2D)
    Calculates the distance squared between two vectors.
    float
    dot(Vector2D vector2D)
    Calculates the dot product of two vectors.
    boolean
     
    int
     
    static Vector2D
    headingVectorFromAngle(double angle)
    Creates a vector from a heading angle.
    float
    Calculates the magnitude of this vector (the distance from origin).
    float
    Calculates the magnitude squared of this vector.
    void
    mutateAdd(float x, float y)
    Adds the desired x and y values to this vector.
    void
    mutateAdd(Vector2D vector2D)
    Adds the desired vector to this vector.
    void
    mutateScalar(float scalar)
    Multiplies this vector by the desired scalar.
    void
    mutateSubtract(float x, float y)
    Subtracts the desired x and y values from this vector.
    void
    Subtracts the desired vector from this vector.
    void
    mutateX(float x)
    Sets the x coordinate of this vector.
    void
    mutateY(float y)
    Sets the y coordinate of this vector.
    Calculates the normalized vector (where the vector has a length of one).
    reflect(Vector2D normal)
    Calculates the reflected vector off of desired Vector2D normal.
    rotate(double angle)
    Calculates the rotation for a vector about the origin.
    scalar(float scalar)
    Calculates the scalar of this vector and returns the result as a new vector object.
    subtract(Vector2D vector2D)
    Calculates the difference of two vectors and returns the sum as a new vector object.
     
    float
    x()
     
    float
    y()
     
    static Vector2D
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Vector2D

      public Vector2D(float x, float y)
      Create a Vector2D instance with x and y set.
      Parameters:
      x - the x coordinate
      y - the y coordinate
  • Method Details

    • headingVectorFromAngle

      public static Vector2D headingVectorFromAngle(double angle)
      Creates a vector from a heading angle.
      Parameters:
      angle - angle in radians
      Returns:
      the heading vector
    • zeroVector

      public static Vector2D zeroVector()
      Returns:
      A Vector2D with 0 for the x and y.
    • x

      public float x()
      Returns:
      the x coordinate
    • y

      public float y()
      Returns:
      the y coordinate
    • mutateX

      public void mutateX(float x)
      Sets the x coordinate of this vector.
      Parameters:
      x - the new x coordinate
    • mutateY

      public void mutateY(float y)
      Sets the y coordinate of this vector.
      Parameters:
      y - the new y coordinate
    • mutateAdd

      public void mutateAdd(Vector2D vector2D)
      Adds the desired vector to this vector.
      Parameters:
      vector2D - the vector to add to this vector
    • mutateAdd

      public void mutateAdd(float x, float y)
      Adds the desired x and y values to this vector.
      Parameters:
      x - the x value to add
      y - the y value to add
    • mutateSubtract

      public void mutateSubtract(Vector2D vector2D)
      Subtracts the desired vector from this vector.
      Parameters:
      vector2D - the vector to subtract from this vector
    • mutateSubtract

      public void mutateSubtract(float x, float y)
      Subtracts the desired x and y values from this vector.
      Parameters:
      x - the x value to subtract
      y - the y value to subtract
    • mutateScalar

      public void mutateScalar(float scalar)
      Multiplies this vector by the desired scalar.
      Parameters:
      scalar - the scalar to multiply this vector by
    • add

      public Vector2D add(Vector2D vector2D)
      Calculates the sum of two vectors and returns the sum as a new vector object.
      Parameters:
      vector2D - the vector to add to this vector
      Returns:
      a new vector with the result of this + vector2D
    • subtract

      public Vector2D subtract(Vector2D vector2D)
      Calculates the difference of two vectors and returns the sum as a new vector object.
      Parameters:
      vector2D - the vector to subtract the value of
      Returns:
      a new vector with the result of this - vector2D
    • scalar

      public Vector2D scalar(float scalar)
      Calculates the scalar of this vector and returns the result as a new vector object.
      Parameters:
      scalar - the scalar
      Returns:
      a new vector with the result of scalar applied to this
    • magnitude

      public float magnitude()
      Calculates the magnitude of this vector (the distance from origin).
      Returns:
      the magnitude of the vector
    • magnitudeSq

      public float magnitudeSq()
      Calculates the magnitude squared of this vector.
      Returns:
      the magnitude squared of this vector
    • distance

      public float distance(Vector2D vector2D)
      Calculates the distance between two vectors.
      Parameters:
      vector2D - the vector to get the distance between
      Returns:
      the distance between the vectors
    • distanceSq

      public float distanceSq(Vector2D vector2D)
      Calculates the distance squared between two vectors. This is faster than calculating the distance since a sqrt call is not made.
      Parameters:
      vector2D - the vector to get the distance squared between
      Returns:
      the distance squared between the vectors
    • normalize

      public Vector2D normalize()
      Calculates the normalized vector (where the vector has a length of one).
      Returns:
      the normalized vector as a new object
    • dot

      public float dot(Vector2D vector2D)
      Calculates the dot product of two vectors.
      Parameters:
      vector2D - the vector to calculate the dot product with
      Returns:
      the calculated dot product as a new vector object
    • rotate

      public Vector2D rotate(double angle)
      Calculates the rotation for a vector about the origin.
      Parameters:
      angle - the angle to rotate the vector in radians
      Returns:
      the calculated rotation vector as a new vector object
    • reflect

      public Vector2D reflect(Vector2D normal)
      Calculates the reflected vector off of desired Vector2D normal.
      Parameters:
      normal - the normal to reflect off of
      Returns:
      the reflected vector
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object