Class Vector

java.lang.Object
ch.epfl.cs107.play.math.Vector
All Implemented Interfaces:
Serializable

public final class Vector extends Object implements Serializable
Represents an immutable 2D floating-point vector.
See Also:
  • Field Details

    • EPSILON

      public static final double EPSILON
      Small value for double precision in vector comparison
      See Also:
    • ZERO

      public static final Vector ZERO
      The zero vector (0, 0)
    • X

      public static final Vector X
      The unit X vector (1, 0)
    • Y

      public static final Vector Y
      The unit Y vector (0, 1)
    • x

      public final float x
    • y

      public final float y
  • Constructor Details

    • Vector

      public Vector(float x, float y)
      Creates a new vector.
      Parameters:
      x - (float): abscissa
      y - (float): ordinate
  • Method Details

    • getX

      public float getX()
      Returns:
      (float): abscissa
    • getY

      public float getY()
      Returns:
      (float): ordinate
    • getLength

      public float getLength()
      Returns:
      (float): euclidian length
    • getAngle

      public float getAngle()
      Returns:
      (float): angle in standard trigonometrical system, in radians
    • opposite

      public Vector opposite()
      Returns:
      (Vector): negated vector
    • add

      public Vector add(Vector other)
      Parameters:
      other - (Vector): right-hand operand, not null
      Returns:
      (Vector): sum, not null
    • add

      public Vector add(float x, float y)
      Parameters:
      x - (float): right-hand abcissa
      y - (float): right-hand ordinate
      Returns:
      (Vector): sum, not null
    • sub

      public Vector sub(Vector other)
      Parameters:
      other - (Vector): right-hand operand, not null
      Returns:
      (Vector): difference, not null
    • sub

      public Vector sub(float x, float y)
      Parameters:
      x - (float): right-hand abcissa
      y - (float): right-hand ordinate
      Returns:
      (Vector): difference, not null
    • mul

      public Vector mul(Vector other)
      Parameters:
      other - (Vector): right-hand operand, not null
      Returns:
      (Vector): component-wise multiplication, not null
    • mul

      public Vector mul(float x, float y)
      Parameters:
      x - (float): right-hand abcissa
      y - (float): right-hand ordinate
      Returns:
      (Vector): component-wise multiplication, not null
    • mul

      public Vector mul(float s)
      Parameters:
      s - (float): right-hand operand
      Returns:
      (Vector): scaled vector, not null
    • div

      public Vector div(Vector other)
      Parameters:
      other - (Vector): right-hand operand, not null
      Returns:
      (Vector): component-wise division, not null
    • div

      public Vector div(float x, float y)
      Parameters:
      x - (float): right-hand abcissa
      y - (float): right-hand ordinate
      Returns:
      (Vector): component-wise division, not null
    • div

      public Vector div(float s)
      Parameters:
      s - (float): right-hand operand
      Returns:
      (Vector):scaled vector, not null
    • dot

      public float dot(Vector other)
      Parameters:
      other - (Vector): right-hand operand, not null
      Returns:
      (Vector): dot product
    • min

      public Vector min(Vector other)
      Parameters:
      other - (Vector): right-hand operand, not null
      Returns:
      (Vector): component-wise minimum, not null
    • min

      public float min()
      Returns:
      (float): smallest component
    • max

      public Vector max(Vector other)
      Parameters:
      other - (Vector): right-hand operand, not null
      Returns:
      (Vector): component-wise maximum, not null
    • max

      public float max()
      Returns:
      (float): largest component
    • normalized

      public Vector normalized()
      Computes unit vector of same direction, or (1, 0) if zero.
      Returns:
      (Vector): rescaled vector, not null
    • resized

      public Vector resized(float length)
      Resizes vector to specified length, or (length, 0) if zero.
      Parameters:
      length - (float): new length
      Returns:
      (Vector): rescaled vector, not null
    • mirrored

      public Vector mirrored(Vector normal)
      Computes mirrored vector, with respect to specified normal.
      Parameters:
      normal - (Vector): vector perpendicular to the symmetry plane, not null
      Returns:
      (Vector): rotated vector, not null
    • rotated

      public Vector rotated(double angle)
      Computes rotated vector, in a counter-clockwise manner.
      Parameters:
      angle - (double): rotation, in radians
      Returns:
      (Vector): rotated vector, not null
    • clockwise

      public Vector clockwise()
      Returns:
      (Vector): vector rotated by -90°, not null
    • counterClockwise

      public Vector counterClockwise()
      Returns:
      (Vector): vector rotated by 90°, not null
    • round

      public Vector round()
      Returns:
      (Vector): a rounded vector (x and y rounded to the closest int)
    • mixed

      public Vector mixed(Vector other, float factor)
      Computes linear interpolation between two vectors.
      Parameters:
      other - (Vector): second vector, not null
      factor - (float) weight of the second vector
      Returns:
      (Vector): interpolated vector, not null
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object