Record Class Vector

java.lang.Object
java.lang.Record
nl.colorize.multimedialib.math.Vector

public record Vector(Point2D origin, Angle direction, float magnitude) extends Record
A two-dimensional vector expressed using an origin, a direction, and a magnitude. All coordinates are defined with float precision.

The direction of the vector is defined by an angle in degrees. An angle of 0 degrees represents the vector (1, 0). Angles greater than 0 rotate in a clockwise direction, so an angle of 180 degress would represent the vector (-1, 0).

  • Constructor Details

    • Vector

      public Vector(Angle direction, float magnitude)
    • Vector

      public Vector(float direction, float magnitude)
    • Vector

      public Vector(Point2D origin, Angle direction, float magnitude)
      Creates an instance of a Vector record class.
      Parameters:
      origin - the value for the origin record component
      direction - the value for the direction record component
      magnitude - the value for the magnitude record component
  • Method Details

    • getX

      public float getX()
    • getY

      public float getY()
    • toPoint

      public Point2D toPoint()
    • withDirection

      public Vector withDirection(Angle newDirection)
    • withDirection

      public Vector withDirection(float newDirection)
    • withMagnitude

      public Vector withMagnitude(float newMagnitude)
    • 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
    • fromPoint

      public static Vector fromPoint(Point2D p)
      Creates a new vector with a direction and magnitude so that getX() and getY() will refer to the specified point.
    • 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. Reference components are compared with Objects::equals(Object,Object); primitive components 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.
    • origin

      public Point2D origin()
      Returns the value of the origin record component.
      Returns:
      the value of the origin record component
    • direction

      public Angle direction()
      Returns the value of the direction record component.
      Returns:
      the value of the direction record component
    • magnitude

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