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:
- 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 R2Vectoradd(R2Vector p1, R2Vector p2)Returns the vector result ofp1 - p2.doublecrossProd(R2Vector that)Returns the cross product of this vector with that vector.doubledotProd(R2Vector that)Returns the dot product of this vector with that vector.static doubledotProd(R2Vector p1, R2Vector p2)Returns the dot product of the given vectors.booleanequals(Object that)Returns true if that object is an R2Vector with exactly the same x and y coordinates.doubleget(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.inthashCode()Calcualates hashcode based on stored coordinates.booleanlessThan(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 R2Vectormul(R2Vector p, double m)Returns the element-wise multiplication of p1 and p2, e.g.doublenorm()Returns the vector magnitude.doublenorm2()Returns the square of the vector magnitude.static R2Vectornormalize(R2Vector vector)Returns a new vector scaled to magnitude 1, or a copy of the original vector if magnitude was 0.R2Vectorortho()Returns a new R2 vector orthogonal to the current one with the same norm and counterclockwise to it.static R2Vectorsub(R2Vector p1, R2Vector p2)Returns the vector result ofp1 - p2.StringtoString()doublex()Returns the x coordinate of this R2 vector.doubley()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:
ArrayIndexOutOfBoundsException- Thrown if the given index is not 0 or 1.
-
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.
-
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.
-
-