Class Vector2D.Unit

All Implemented Interfaces:
Point<Vector2D>, Spatial, Vector<Vector2D>
Enclosing class:
Vector2D

public static final class Vector2D.Unit extends Vector2D
Represents unit vectors. This allows optimizations for certain operations.
  • Field Details

    • PLUS_X

      public static final Vector2D.Unit PLUS_X
      Unit vector (coordinates: 1, 0).
    • MINUS_X

      public static final Vector2D.Unit MINUS_X
      Negation of unit vector (coordinates: -1, 0).
    • PLUS_Y

      public static final Vector2D.Unit PLUS_Y
      Unit vector (coordinates: 0, 1).
    • MINUS_Y

      public static final Vector2D.Unit MINUS_Y
      Negation of unit vector (coordinates: 0, -1).
    • UNSCALED_MAX

      private static final double UNSCALED_MAX
      Maximum coordinate value for computing normalized vectors with raw, unscaled values.
      See Also:
    • SCALE_UP_FACTOR

      private static final double SCALE_UP_FACTOR
      Factor used to scale up coordinate values in order to produce normalized coordinates without overflow or underflow.
      See Also:
    • SCALE_DOWN_FACTOR

      private static final double SCALE_DOWN_FACTOR
      Factor used to scale down coordinate values in order to produce normalized coordinates without overflow or underflow.
      See Also:
  • Constructor Details

    • Unit

      private Unit(double x, double y)
      Simple constructor. Callers are responsible for ensuring that the given values represent a normalized vector.
      Parameters:
      x - abscissa (first coordinate value)
      y - abscissa (second coordinate value)
  • Method Details

    • norm

      public double norm()
      Get the L2 norm (commonly known as the Euclidean norm) for the vector. This corresponds to the common notion of vector magnitude or length and is defined as the square root of the sum of the squares of all vector components.
      Specified by:
      norm in interface Vector<Vector2D>
      Overrides:
      norm in class Vector2D
      Returns:
      L2 norm for the vector
      See Also:
    • normSq

      public double normSq()
      Get the square of the L2 norm (also known as the Euclidean norm) for the vector. This is equal to the sum of the squares of all vector components.
      Specified by:
      normSq in interface Vector<Vector2D>
      Overrides:
      normSq in class Vector2D
      Returns:
      square of the L2 norm for the vector
      See Also:
    • normalize

      public Vector2D.Unit normalize()
      Get a normalized vector aligned with the instance. The returned vector has a magnitude of 1.
      Specified by:
      normalize in interface Vector<Vector2D>
      Overrides:
      normalize in class Vector2D
      Returns:
      normalized vector
      See Also:
    • normalizeOrNull

      public Vector2D.Unit normalizeOrNull()
      Attempt to compute a normalized vector aligned with the instance, returning null if such a vector cannot be computed. This method is equivalent to Vector.normalize() but returns null instead of throwing an exception on failure.
      Specified by:
      normalizeOrNull in interface Vector<Vector2D>
      Overrides:
      normalizeOrNull in class Vector2D
      Returns:
      normalized vector or null if such a vector cannot be computed, i.e. if the norm is zero, NaN, or infinite
      See Also:
    • orthogonal

      public Vector2D.Unit orthogonal()
      Get a unit vector orthogonal to the instance. The returned vector is computed by rotating the current instance pi/2 radians counterclockwise around the origin and normalizing. For example, if this method is called on a vector pointing along the positive x-axis, then a unit vector representing the positive y-axis is returned.
      Overrides:
      orthogonal in class Vector2D
      Returns:
      a unit vector orthogonal to the current instance
    • withNorm

      public Vector2D withNorm(double mag)
      Returns a vector with the same direction but with the given norm. This is equivalent to calling vec.normalize().scalarMultiply(mag) but without the intermediate vector.
      Specified by:
      withNorm in interface Vector<Vector2D>
      Overrides:
      withNorm in class Vector2D
      Parameters:
      mag - The vector norm
      Returns:
      a vector with the same direction as the current instance but the given norm
    • negate

      public Vector2D.Unit negate()
      Get the negation of the instance.
      Specified by:
      negate in interface Vector<Vector2D>
      Overrides:
      negate in class Vector2D
      Returns:
      a new vector which is the negation of the instance
    • from

      public static Vector2D.Unit from(double x, double y)
      Create a normalized vector.
      Parameters:
      x - Vector coordinate.
      y - Vector coordinate.
      Returns:
      a vector whose norm is 1.
      Throws:
      IllegalArgumentException - if the norm of the given value is zero, NaN, or infinite
    • from

      public static Vector2D.Unit from(Vector2D v)
      Create a normalized vector.
      Parameters:
      v - Vector.
      Returns:
      a vector whose norm is 1.
      Throws:
      IllegalArgumentException - if the norm of the given value is zero, NaN, or infinite
    • tryCreateNormalized

      private static Vector2D.Unit tryCreateNormalized(double x, double y, boolean throwOnFailure)
      Attempt to create a normalized vector from the given coordinate values. If throwOnFailure is true, an exception is thrown if a normalized vector cannot be created. Otherwise, null is returned.
      Parameters:
      x - x coordinate
      y - y coordinate
      throwOnFailure - if true, an exception will be thrown if a normalized vector cannot be created
      Returns:
      normalized vector or null if one cannot be created and throwOnFailure is false
      Throws:
      IllegalArgumentException - if the computed norm is zero, NaN, or infinite