Record Class Vec2

java.lang.Object
java.lang.Record
cit244.api.geom.Vec2
Record Components:
x - horizontal component
y - vertical component

public record Vec2(double x, double y) extends Record
An immutable two-dimensional vector, also usable as a position.

Coordinates use the same units as the values supplied to them: typically pixels for positions and pixels per second for velocities. Operations return new vectors; they never modify this one. Components must be finite.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Vec2
    The zero vector, with both components equal to zero.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Vec2(double x, double y)
    Constructs a vector from its two components.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Vec2 other)
    Adds another vector component-wise.
    double
    Returns the Euclidean distance to another position.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    double
    Returns the vector's Euclidean length (magnitude).
    static Vec2
    lerp(Vec2 a, Vec2 b, double t)
    Blends between two vectors.
    scaled(double factor)
    Multiplies both components by a scalar.
    subtract(Vec2 other)
    Subtracts another vector component-wise.
    final String
    Returns a string representation of this record class.
    translated(double dx, double dy)
    Returns this vector displaced by the given horizontal and vertical amounts.
    double
    x()
    Returns the value of the x record component.
    double
    y()
    Returns the value of the y record component.

    Methods inherited from class java.lang.Object

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

    • ZERO

      public static final Vec2 ZERO
      The zero vector, with both components equal to zero.
  • Constructor Details

    • Vec2

      public Vec2(double x, double y)
      Constructs a vector from its two components.
      Throws:
      IllegalArgumentException - if either component is NaN or infinite
  • Method Details

    • add

      public Vec2 add(Vec2 other)
      Adds another vector component-wise.
      Parameters:
      other - vector to add
      Returns:
      the sum
    • subtract

      public Vec2 subtract(Vec2 other)
      Subtracts another vector component-wise.
      Parameters:
      other - vector to subtract
      Returns:
      the difference
    • scaled

      public Vec2 scaled(double factor)
      Multiplies both components by a scalar.
      Parameters:
      factor - scale factor
      Returns:
      the scaled vector
    • translated

      public Vec2 translated(double dx, double dy)
      Returns this vector displaced by the given horizontal and vertical amounts. Equivalent to adding (dx, dy).
      Parameters:
      dx - horizontal displacement
      dy - vertical displacement
      Returns:
      the translated vector
    • length

      public double length()
      Returns the vector's Euclidean length (magnitude).
      Returns:
      nonnegative length
    • distanceTo

      public double distanceTo(Vec2 other)
      Returns the Euclidean distance to another position.
      Parameters:
      other - destination position
      Returns:
      nonnegative distance
    • lerp

      public static Vec2 lerp(Vec2 a, Vec2 b, double t)
      Blends between two vectors. t = 0 gives a, t = 1 gives b, and t = 0.5 gives the midpoint. Values outside [0, 1] continue along the same line, like Mathf.lerp(double, double, double).

      To ease the motion, shape t first: Vec2.lerp(start, end, Curve.SMOOTHSTEP.apply(t)).

      Parameters:
      a - starting vector
      b - ending vector
      t - interpolation factor
      Returns:
      interpolated vector
    • toString

      public final 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 the compare method from their corresponding wrapper classes.
      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 double x()
      Returns the value of the x record component.
      Returns:
      the value of the x record component
    • y

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