Class Vector2

java.lang.Object
civitas.celestis.util.tuple.Double2
civitas.celestis.math.vector.Vector2
All Implemented Interfaces:
Vector<Vector2>, BaseTuple<Double>, DoubleTuple, Serializable

public class Vector2 extends Double2 implements Vector<Vector2>
An immutable two-dimensional vector which uses the type double.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Vector2
    The negative X unit vector.
    static final Vector2
    The negative Y unit vector.
    static final Vector2
    The positive X unit vector.
    static final Vector2
    The positive Y unit vector.
    static final Vector2
    A vector of no direction or magnitude.

    Fields inherited from class civitas.celestis.util.tuple.Double2

    x, y
  • Constructor Summary

    Constructors
    Constructor
    Description
    Vector2(double[] components)
    Creates a new vector.
    Vector2(double x, double y)
    Creates a new vector.
    Creates a new vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(double s)
    Adds a scalar to this vector component-wise, then returns the resulting vector.
    Adds another vector to this vector, then returns the resulting vector.
    clamp(Vector2 min, Vector2 max)
    Between this vector and the provided boundary vectors min and max, this returns a new vector whose components are clamped to respect the range of [min, max].
    double
    Returns the Euclidean distance between this vector and the provided vector v.
    double
    Returns the squared Euclidean distance between this vector and the provided vector v.
    double
    Returns the Manhattan distance between this vector and the provided vector v.
    divide(double s)
    Divides this vector by a scalar component-wise, then returns the resulting vector.
    double
    Returns the dot product between this vector and the provided vector v.
    boolean
    Checks for equality between this vector and the provided vector v.
    Applies the provided mapper function f to each component of this tuple, then returns a new tuple containing the return values of the function f.
    Between this vector and the provided boundary vector v, this takes the maximum of each corresponding pair of components, then returns a new vector whose components are populated from that of the maximum values between each pair of components.
    Between this vector and the provided vector v, this applies the merger function f to each corresponding pair of components, then returns a new vector whose components are populated from the return values of the function f.
    Between this vector and the provided boundary vector v, this takes the minimum of each corresponding pair of components, then returns a new vector whose components are populated from that of the minimum values between each pair of components.
    multiply(double s)
    Multiplies this vector by a scalar component-wise, then returns the resulting vector.
    Returns the complex number product between this vector and the provided vector v.
    Negates all components of this vector, then returns the resulting vector.
    double
    Returns the Euclidean norm (the magnitude) of this vector.
    double
    Returns the squared Euclidean norm (the squared magnitude) of this vector.
    Normalizes this vector to have a Euclidean norm (magnitude) of 1.
    Normalizes this vector to have a Euclidean norm (magnitude) of 1.
    Normalizes this vector ot have a Euclidean norm (magnitude) of 1.
    double
    Returns the Manhattan norm of this vector.
    rotate(double t)
    Rotates this vector counter-clockwise by the provided angle t.
    subtract(double s)
    Subtracts this vector by a scalar component-wise, then returns the resulting vector.
    Subtracts another vector from this vector, then returns the resulting vector.

    Methods inherited from class civitas.celestis.util.tuple.Double2

    array, boxed, contains, containsAll, equals, get, hashCode, isFinite, isInfinite, isNaN, isZero, list, mapToObj, size, stream, toString, x, y

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface civitas.celestis.util.tuple.DoubleTuple

    array, boxed, contains, containsAll, doubleArray, equals, get, isFinite, isInfinite, isNaN, isZero, list, mapToObj, size, stream, toString
  • Field Details

    • ZERO

      public static final Vector2 ZERO
      A vector of no direction or magnitude. Represents origin.
    • POSITIVE_X

      public static final Vector2 POSITIVE_X
      The positive X unit vector.
    • POSITIVE_Y

      public static final Vector2 POSITIVE_Y
      The positive Y unit vector.
    • NEGATIVE_X

      public static final Vector2 NEGATIVE_X
      The negative X unit vector.
    • NEGATIVE_Y

      public static final Vector2 NEGATIVE_Y
      The negative Y unit vector.
  • Constructor Details

    • Vector2

      public Vector2(double x, double y)
      Creates a new vector.
      Parameters:
      x - The X component of this vector
      y - The Y component of this vector
    • Vector2

      public Vector2(@Nonnull double[] components)
      Creates a new vector.
      Parameters:
      components - An array containing the components of this vector in XY order
      Throws:
      IllegalArgumentException - When the provided array's length is not 2
    • Vector2

      public Vector2(@Nonnull DoubleTuple t)
      Creates a new vector.
      Parameters:
      t - The tuple of which to copy component values from
      Throws:
      IllegalArgumentException - When the provided tuple t's size is not 2
  • Method Details

    • norm

      public double norm()
      Returns the Euclidean norm (the magnitude) of this vector.
      Specified by:
      norm in interface Vector<Vector2>
      Returns:
      The Euclidean norm of this vector
    • norm2

      public double norm2()
      Returns the squared Euclidean norm (the squared magnitude) of this vector.
      Specified by:
      norm2 in interface Vector<Vector2>
      Returns:
      The squared Euclidean norm of this vector
    • normManhattan

      public double normManhattan()
      Returns the Manhattan norm of this vector.
      Specified by:
      normManhattan in interface Vector<Vector2>
      Returns:
      The Manhattan norm of this vector
    • add

      @Nonnull public Vector2 add(double s)
      Adds a scalar to this vector component-wise, then returns the resulting vector.
      Specified by:
      add in interface Vector<Vector2>
      Parameters:
      s - The scalar of which to add to each component of this vector
      Returns:
      The resulting vector
    • subtract

      @Nonnull public Vector2 subtract(double s)
      Subtracts this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      subtract in interface Vector<Vector2>
      Parameters:
      s - The scalar of which to subtract from each component of this vector
      Returns:
      The resulting vector
    • multiply

      @Nonnull public Vector2 multiply(double s)
      Multiplies this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      multiply in interface Vector<Vector2>
      Parameters:
      s - The scalar of which to multiply each component of this vector by
      Returns:
      The resulting vector
    • divide

      @Nonnull public Vector2 divide(double s) throws ArithmeticException
      Divides this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      divide in interface Vector<Vector2>
      Parameters:
      s - The scalar of which to divide each component of this vector by
      Returns:
      The resulting vector
      Throws:
      ArithmeticException - When the denominator s is zero
    • add

      @Nonnull public Vector2 add(@Nonnull Vector2 v)
      Adds another vector to this vector, then returns the resulting vector.
      Specified by:
      add in interface Vector<Vector2>
      Parameters:
      v - The vector of which to add to this vector
      Returns:
      The resulting vector
    • subtract

      @Nonnull public Vector2 subtract(@Nonnull Vector2 v)
      Subtracts another vector from this vector, then returns the resulting vector.
      Specified by:
      subtract in interface Vector<Vector2>
      Parameters:
      v - The vector of which to subtract from this vector
      Returns:
      The resulting vector
    • multiply

      @Nonnull public Vector2 multiply(@Nonnull Vector2 v)
      Returns the complex number product between this vector and the provided vector v.
      Parameters:
      v - The vector of which to get the complex number product between
      Returns:
      The complex number product between the two vectors
    • dot

      public double dot(@Nonnull Vector2 v)
      Returns the dot product between this vector and the provided vector v.
      Specified by:
      dot in interface Vector<Vector2>
      Parameters:
      v - The vector of which to get the dot product between
      Returns:
      The dot product between the two vectors
    • normalize

      @Nonnull public Vector2 normalize() throws ArithmeticException
      Normalizes this vector to have a Euclidean norm (magnitude) of 1.
      Specified by:
      normalize in interface Vector<Vector2>
      Returns:
      The normalized vector of this vector
      Throws:
      ArithmeticException - When the Euclidean norm of this vector is zero
    • normalizeOrZero

      @Nonnull public Vector2 normalizeOrZero()
      Normalizes this vector ot have a Euclidean norm (magnitude) of 1. If this vector's Euclidean norm is zero, this will return itself. (which is guaranteed to be a vector where all components are zero, as only zero vectors have a Euclidean norm of zero)
      Specified by:
      normalizeOrZero in interface Vector<Vector2>
      Returns:
      The normalized vector of this vector if successful, zero otherwise
    • normalizeOrDefault

      @Nonnull public Vector2 normalizeOrDefault(@Nonnull Vector2 v)
      Normalizes this vector to have a Euclidean norm (magnitude) of 1. If this vector's Euclidean norm is zero, this will return the provided fallback value v.
      Specified by:
      normalizeOrDefault in interface Vector<Vector2>
      Parameters:
      v - The value of which to return in case of division by zero
      Returns:
      The normalized vector of this vector if successful, the fallback value v otherwise
    • negate

      @Nonnull public Vector2 negate()
      Negates all components of this vector, then returns the resulting vector.
      Specified by:
      negate in interface Vector<Vector2>
      Returns:
      The negation of this vector
    • min

      @Nonnull public Vector2 min(@Nonnull Vector2 v)
      Between this vector and the provided boundary vector v, this takes the minimum of each corresponding pair of components, then returns a new vector whose components are populated from that of the minimum values between each pair of components.
      Specified by:
      min in interface Vector<Vector2>
      Parameters:
      v - The boundary vector of which to compare to
      Returns:
      The minimum vector of the two vectors
    • max

      @Nonnull public Vector2 max(@Nonnull Vector2 v)
      Between this vector and the provided boundary vector v, this takes the maximum of each corresponding pair of components, then returns a new vector whose components are populated from that of the maximum values between each pair of components.
      Specified by:
      max in interface Vector<Vector2>
      Parameters:
      v - The boundary vector of which to compare to
      Returns:
      The maximum vector of the two vectors
    • clamp

      @Nonnull public Vector2 clamp(@Nonnull Vector2 min, @Nonnull Vector2 max)
      Between this vector and the provided boundary vectors min and max, this returns a new vector whose components are clamped to respect the range of [min, max]. This is the equivalent of clamping each component to the corresponding pair of minimum and maximum values.
      Specified by:
      clamp in interface Vector<Vector2>
      Parameters:
      min - The minimum boundary vector to compare to
      max - The maximum boundary vector to compare to
      Returns:
      The clamped vector which respects the boundaries of [min, max]
    • distance

      public double distance(@Nonnull Vector2 v)
      Returns the Euclidean distance between this vector and the provided vector v.
      Specified by:
      distance in interface Vector<Vector2>
      Parameters:
      v - The vector of which get the Euclidean distance between
      Returns:
      The Euclidean distance between the two vectors
    • distance2

      public double distance2(@Nonnull Vector2 v)
      Returns the squared Euclidean distance between this vector and the provided vector v.
      Specified by:
      distance2 in interface Vector<Vector2>
      Parameters:
      v - The vector of which to get the squared Euclidean distance between
      Returns:
      The squared Euclidean distance between the two vectors
    • distanceManhattan

      public double distanceManhattan(@Nonnull Vector2 v)
      Returns the Manhattan distance between this vector and the provided vector v.
      Specified by:
      distanceManhattan in interface Vector<Vector2>
      Parameters:
      v - The vector of which to get the Manhattan distance between
      Returns:
      The Manhattan distance between the two vectors
    • map

      @Nonnull public Vector2 map(@Nonnull DoubleUnaryOperator f)
      Applies the provided mapper function f to each component of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      map in interface DoubleTuple
      Specified by:
      map in interface Vector<Vector2>
      Overrides:
      map in class Double2
      Parameters:
      f - The function of which to apply to each component of this tuple
      Returns:
      The resulting tuple
    • merge

      @Nonnull public Vector2 merge(@Nonnull Vector2 v, @Nonnull BinaryOperator<Double> f)
      Between this vector and the provided vector v, this applies the merger function f to each corresponding pair of components, then returns a new vector whose components are populated from the return values of the function f.
      Specified by:
      merge in interface Vector<Vector2>
      Parameters:
      v - The vector of which to merge this vector with
      f - The merger function to handle the merging of the two vectors
      Returns:
      The resulting vector
    • rotate

      @Nonnull public Vector2 rotate(double t)
      Rotates this vector counter-clockwise by the provided angle t. Angle is denoted in radians.
      Parameters:
      t - The angle of rotation to apply in radians
      Returns:
      The rotated vector
      See Also:
    • equals

      public boolean equals(@Nullable Vector2 v)
      Checks for equality between this vector and the provided vector v.
      Specified by:
      equals in interface Vector<Vector2>
      Parameters:
      v - The vector to compare to
      Returns:
      true if the component values are equal