Package bagel.util

Class Vector2

java.lang.Object
bagel.util.Vector2

public class Vector2 extends Object
Immutable class representing a two-dimensional real vector.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Vector2
    A unit vector pointing downwards.
    static final Vector2
    A unit vector pointing to the left.
    static final Vector2
    A unit vector pointing to the right.
    static final Vector2
    A unit vector pointing upwards.
    final double
     
    final double
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct the zero vector.
    Vector2(double x, double y)
    Create a vector with a given x and y coordinate.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Vector2 rhs)
    Returns the result of adding rhs to this vector.
    Converts the vector to a Point with the same x and y values.
    div(double scalar)
    Returns the result of dividing this vector by scalar.
    double
    dot(Vector2 rhs)
    Returns the dot (scalar) product of this vector and rhs.
    boolean
    Performs exact equality checking.
    double
    Returns the length of this vector.
    double
    Returns the square of the length of this vector.
    mul(double scalar)
    Returns the result of multiplying this vector by scalar.
    Returns this vector normalised to have a length of 1 by dividing by its length.
    sub(Vector2 rhs)
    Returns the result of subtracting rhs from this vector.
     

    Methods inherited from class java.lang.Object

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

    • x

      public final double x
    • y

      public final double y
    • left

      public static final Vector2 left
      A unit vector pointing to the left.
    • up

      public static final Vector2 up
      A unit vector pointing upwards.
    • down

      public static final Vector2 down
      A unit vector pointing downwards.
  • Constructor Details

    • Vector2

      public Vector2()
      Construct the zero vector.
    • Vector2

      public Vector2(double x, double y)
      Create a vector with a given x and y coordinate.
  • Method Details

    • add

      public Vector2 add(Vector2 rhs)
      Returns the result of adding rhs to this vector.
    • sub

      public Vector2 sub(Vector2 rhs)
      Returns the result of subtracting rhs from this vector.
    • mul

      public Vector2 mul(double scalar)
      Returns the result of multiplying this vector by scalar.
    • div

      public Vector2 div(double scalar)
      Returns the result of dividing this vector by scalar.
    • dot

      public double dot(Vector2 rhs)
      Returns the dot (scalar) product of this vector and rhs. Calculated by `this.x * rhs.x + this.y * rhs.y`.
    • length

      public double length()
      Returns the length of this vector.
    • lengthSquared

      public double lengthSquared()
      Returns the square of the length of this vector.
    • asPoint

      public Point asPoint()
      Converts the vector to a Point with the same x and y values.
    • normalised

      public Vector2 normalised()
      Returns this vector normalised to have a length of 1 by dividing by its length.
    • equals

      public boolean equals(Object rhs)
      Performs exact equality checking.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object