Package com.google.common.geometry
Class R2Vector
- java.lang.Object
-
- com.google.common.geometry.R2Vector
-
- All Implemented Interfaces:
java.io.Serializable
@GwtCompatible(serializable=true) public final class R2Vector extends java.lang.Object implements java.io.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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description R2Vector()
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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static R2Vector
add(R2Vector p1, R2Vector p2)
Returns the vector result ofp1 - p2
.double
crossProd(R2Vector that)
Returns the cross product of this vector with that vector.double
dotProd(R2Vector that)
Returns the dot product of this vector with that vector.static double
dotProd(R2Vector p1, R2Vector p2)
Returns the dot product of the given vectors.boolean
equals(java.lang.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
hashCode()
Calcualates hashcode based on stored coordinates.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.static R2Vector
mul(R2Vector p, double m)
Returns the element-wise multiplication of p1 and p2, e.g.double
norm()
Returns the vector magnitude.double
norm2()
Returns the square of the vector magnitude.static R2Vector
normalize(R2Vector vector)
Returns a new vector scaled to magnitude 1, or a copy of the original vector if magnitude was 0.R2Vector
ortho()
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
set(R2Vector v)
Sets the position of this vector from the given other vector.static R2Vector
sub(R2Vector p1, R2Vector p2)
Returns the vector result ofp1 - p2
.java.lang.String
toString()
double
x()
Returns the x coordinate of this R2 vector.double
y()
Returns the y coordinate of this R2 vector.
-
-
-
Constructor Detail
-
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 Detail
-
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:
java.lang.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.
-
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(java.lang.Object that)
Returns true if that object is an R2Vector with exactly the same x and y coordinates.- Overrides:
equals
in classjava.lang.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 classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-