Class R2Vector

java.lang.Object
com.google.common.geometry.R2Vector
All Implemented Interfaces:
Serializable

@GwtCompatible(serializable=true) public final class R2Vector extends Object implements Serializable
R2Vector represents a vector in the two-dimensional space. It defines the basic geometrical operations for 2D vectors, e.g. cross product, addition, norm, comparison, etc.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) double
     
    (package private) double
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new R2Vector at the origin [0,0] of the R2 coordinate system.
    R2Vector(double[] coord)
    Constructs a new R2 vector from the given coordinates array, which must have length 2.
    R2Vector(double x, double y)
    Constructs a new R2 vector from the given x and y coordinates.
  • Method Summary

    Modifier and Type
    Method
    Description
    static R2Vector
    Returns the vector result of p1 - p2.
    double
    Returns the cross product of this vector with that vector.
    double
    Returns the dot product of this vector with that vector.
    static double
    Returns the dot product of the given vectors.
    boolean
    equals(Object that)
    Returns true if that object is an R2Vector with exactly the same x and y coordinates.
    double
    get(int index)
    Returns the coordinate of the given axis, which will be the x axis if index is 0, and the y axis if index is 1.
    int
    Calcualates hashcode based on stored coordinates.
    boolean
    Returns true if this vector is less than that vector, with the x-axis as the primary sort key and the y-axis as the secondary sort key.
    static R2Vector
    mul(R2Vector p, double m)
    Returns the element-wise multiplication of p1 and p2, e.g.
    double
    Returns the vector magnitude.
    double
    Returns the square of the vector magnitude.
    static R2Vector
    Returns a new vector scaled to magnitude 1, or a copy of the original vector if magnitude was 0.
    Returns a new R2 vector orthogonal to the current one with the same norm and counterclockwise to it.
    (package private) void
    set(double x, double y)
    Sets the position of this vector from the given values.
    (package private) void
    Sets the position of this vector from the given other vector.
    static R2Vector
    Returns the vector result of p1 - p2.
     
    double
    x()
    Returns the x coordinate of this R2 vector.
    double
    y()
    Returns the y coordinate of this R2 vector.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • x

      double x
    • y

      double y
  • Constructor Details

    • R2Vector

      public R2Vector()
      Constructs a new R2Vector at the origin [0,0] of the R2 coordinate system.
    • R2Vector

      public R2Vector(double x, double y)
      Constructs a new R2 vector from the given x and y coordinates.
    • R2Vector

      public R2Vector(double[] coord)
      Constructs a new R2 vector from the given coordinates array, which must have length 2.
  • Method Details

    • x

      public double x()
      Returns the x coordinate of this R2 vector.
    • y

      public double y()
      Returns the y coordinate of this R2 vector.
    • get

      public double get(int index)
      Returns the coordinate of the given axis, which will be the x axis if index is 0, and the y axis if index is 1.
      Throws:
      ArrayIndexOutOfBoundsException - Thrown if the given index is not 0 or 1.
    • set

      void set(R2Vector v)
      Sets the position of this vector from the given other vector. Package private since this is only mutable for S2.
    • set

      void set(double x, double y)
      Sets the position of this vector from the given values. Package private since this is only mutable for S2.
    • add

      public static R2Vector add(R2Vector p1, R2Vector p2)
      Returns the vector result of p1 - p2.
    • sub

      public static R2Vector sub(R2Vector p1, R2Vector p2)
      Returns the vector result of p1 - p2.
    • mul

      public static R2Vector mul(R2Vector p, double m)
      Returns the element-wise multiplication of p1 and p2, e.g. vector [p1.x*p2.x, p1.y*p2.y].
    • norm

      public double norm()
      Returns the vector magnitude.
    • norm2

      public double norm2()
      Returns the square of the vector magnitude.
    • normalize

      public static R2Vector normalize(R2Vector vector)
      Returns a new vector scaled to magnitude 1, or a copy of the original vector if magnitude was 0.
    • ortho

      public R2Vector ortho()
      Returns a new R2 vector orthogonal to the current one with the same norm and counterclockwise to it.
    • dotProd

      public static double dotProd(R2Vector p1, R2Vector p2)
      Returns the dot product of the given vectors.
    • dotProd

      public double dotProd(R2Vector that)
      Returns the dot product of this vector with that vector.
    • crossProd

      public double crossProd(R2Vector that)
      Returns the cross product of this vector with that vector.
    • lessThan

      public boolean lessThan(R2Vector that)
      Returns true if this vector is less than that vector, with the x-axis as the primary sort key and the y-axis as the secondary sort key.
    • equals

      public boolean equals(Object that)
      Returns true if that object is an R2Vector with exactly the same x and y coordinates.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Calcualates hashcode based on stored coordinates. Since we want +0.0 and -0.0 to be treated the same, we ignore the sign of the coordinates.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object