Package com.google.common.geometry
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 -
Constructor Summary
ConstructorsConstructorDescriptionR2Vector()
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 TypeMethodDescriptionstatic R2Vector
Returns the vector result ofp1 - 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
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
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
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
Returns a new vector scaled to magnitude 1, or a copy of the original vector if magnitude was 0.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
Sets the position of this vector from the given other vector.static R2Vector
Returns the vector result ofp1 - p2
.toString()
double
x()
Returns the x coordinate of this R2 vector.double
y()
Returns the y coordinate of this R2 vector.
-
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
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
Returns the vector result ofp1 - p2
. -
sub
Returns the vector result ofp1 - p2
. -
mul
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
Returns a new vector scaled to magnitude 1, or a copy of the original vector if magnitude was 0. -
ortho
Returns a new R2 vector orthogonal to the current one with the same norm and counterclockwise to it. -
dotProd
Returns the dot product of the given vectors. -
dotProd
Returns the dot product of this vector with that vector. -
crossProd
Returns the cross product of this vector with that vector. -
lessThan
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
Returns true if that object is an R2Vector with exactly the same x and y coordinates. -
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. -
toString
-