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 doubleing-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 double x
    • y

      public final double y
  • Constructor Details

    • Vector

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

    • getX

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

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

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

      public double getAngle()
      Returns:
      (double): 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(double x, double y)
      Parameters:
      x - (double): right-hand abcissa
      y - (double): 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(double x, double y)
      Parameters:
      x - (double): right-hand abcissa
      y - (double): 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(double x, double y)
      Parameters:
      x - (double): right-hand abcissa
      y - (double): right-hand ordinate
      Returns:
      (Vector): component-wise multiplication, not null
    • mul

      public Vector mul(double s)
      Parameters:
      s - (double): 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(double x, double y)
      Parameters:
      x - (double): right-hand abcissa
      y - (double): right-hand ordinate
      Returns:
      (Vector): component-wise division, not null
    • div

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

      public double 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 double min()
      Returns:
      (double): 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 double max()
      Returns:
      (double): 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(double length)
      Resizes vector to specified length, or (length, 0) if zero.
      Parameters:
      length - (double): 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, double factor)
      Computes linear interpolation between two vectors.
      Parameters:
      other - (Vector): second vector, not null
      factor - (double) 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