Record Class Vector2D

java.lang.Object
java.lang.Record
technology.sola.math.linear.Vector2D
Record Components:
x - the x coordinate
y - the y coordinate

@NullMarked public record Vector2D(float x, float y) extends Record
The Vector2D class is an implementation of a linear algebra vector.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Vector2D
    A Vector2D with 0 for the x and y.
  • 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.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    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.
    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.
    Returns a string representation of this record class.
    float
    x()
    Returns the value of the x record component.
    float
    y()
    Returns the value of the y record component.

    Methods inherited from class java.lang.Object

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

    • ZERO_VECTOR

      public static final Vector2D ZERO_VECTOR
      A Vector2D with 0 for the x and y.
  • 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
    • 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, not null
      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, not null
      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()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • x

      public float x()
      Returns the value of the x record component.
      Returns:
      the value of the x record component
    • y

      public float y()
      Returns the value of the y record component.
      Returns:
      the value of the y record component