public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D>
Modifier and Type | Class and Description |
---|---|
static class |
Vector2D.Sum
Class used to create high-accuracy sums of vectors.
|
static class |
Vector2D.Unit
Represents unit vectors.
|
Modifier and Type | Field and Description |
---|---|
static Comparator<Vector2D> |
COORDINATE_ASCENDING_ORDER
Comparator that sorts vectors in component-wise ascending order.
|
static Vector2D |
NaN
A vector with all coordinates set to NaN.
|
static Vector2D |
NEGATIVE_INFINITY
A vector with all coordinates set to negative infinity.
|
static Vector2D |
POSITIVE_INFINITY
A vector with all coordinates set to positive infinity.
|
static Vector2D |
ZERO
Zero vector (coordinates: 0, 0).
|
Modifier and Type | Method and Description |
---|---|
Vector2D |
add(double factor,
Vector2D v) |
Vector2D |
add(Vector2D v) |
double |
angle(Vector2D v) |
static Vector2D |
centroid(Iterable<Vector2D> pts)
Compute the centroid of the given points.
|
static Vector2D |
centroid(Vector2D first,
Vector2D... more)
Compute the centroid of the given points.
|
Vector2D.Unit |
directionTo(Vector2D v)
Return the unit vector representing the direction of displacement from this
vector to the given vector.
|
double |
distance(Vector2D v) |
double |
distanceSq(Vector2D v) |
double |
dot(Vector2D v) |
boolean |
eq(Vector2D vec,
Precision.DoubleEquivalence precision)
Return true if the current instance and given vector are considered equal as evaluated by the
given precision context.
|
boolean |
equals(Object other)
Test for the equality of two vector instances.
|
int |
getDimension() |
double |
getX()
Returns the abscissa (first coordinate value) of the instance.
|
double |
getY()
Returns the ordinate (second coordinate value) of the instance.
|
Vector2D |
getZero() |
int |
hashCode()
Get a hashCode for the 2D coordinates.
|
boolean |
isFinite() |
boolean |
isInfinite() |
boolean |
isNaN() |
Vector2D |
lerp(Vector2D p,
double t)
Get a vector constructed by linearly interpolating between this vector and the given vector.
|
static Vector2D |
max(Iterable<Vector2D> vecs)
Return a vector containing the maximum component values from all input vectors.
|
static Vector2D |
max(Vector2D first,
Vector2D... more)
Return a vector containing the maximum component values from all input vectors.
|
static Vector2D |
min(Iterable<Vector2D> vecs)
Return a vector containing the minimum component values from all input vectors.
|
static Vector2D |
min(Vector2D first,
Vector2D... more)
Return a vector containing the minimum component values from all input vectors.
|
Vector2D |
multiply(double a) |
Vector2D |
negate() |
double |
norm() |
Vector2D.Unit |
normalize() |
Vector2D.Unit |
normalizeOrNull() |
double |
normSq() |
static Vector2D |
of(double[] v)
Creates a vector from the coordinates in the given 2-element array.
|
static Vector2D |
of(double x,
double y)
Returns a vector with the given coordinate values.
|
Vector2D.Unit |
orthogonal()
Get a unit vector orthogonal to the instance.
|
Vector2D.Unit |
orthogonal(Vector2D dir)
Get a unit vector orthogonal to the current vector and pointing in the direction
of
dir . |
static Vector2D |
parse(String str)
Parses the given string and returns a new vector instance.
|
Vector2D |
project(Vector2D base)
Get the projection of the instance onto the given base vector.
|
Vector2D |
reject(Vector2D base)
Get the rejection of the instance from the given base vector.
|
double |
signedArea(Vector2D v)
Compute the signed area of the parallelogram with sides formed by this instance
and the given vector.
|
Vector2D |
subtract(double factor,
Vector2D v) |
Vector2D |
subtract(Vector2D v) |
double[] |
toArray()
Get the coordinates for this instance as a dimension 2 array.
|
String |
toString() |
Vector2D |
transform(UnaryOperator<Vector2D> fn)
Convenience method to apply a function to this vector.
|
Vector2D |
vectorTo(Vector2D v)
Return the vector representing the displacement from this vector
to the given vector.
|
Vector2D |
withNorm(double magnitude) |
getCheckedNorm, isZero
public static final Vector2D POSITIVE_INFINITY
public static final Vector2D NEGATIVE_INFINITY
public static final Comparator<Vector2D> COORDINATE_ASCENDING_ORDER
public double getX()
public double getY()
public double[] toArray()
public int getDimension()
public boolean isNaN()
public boolean isInfinite()
public boolean isFinite()
public Vector2D vectorTo(Vector2D v)
v.subtract(thisVector)
but with a method name that is much easier to visualize.vectorTo
in class EuclideanVector<Vector2D>
v
- the vector that the returned vector will be directed towardpublic Vector2D.Unit directionTo(Vector2D v)
v.subtract(thisVector).normalize()
but without the intermediate vector instance.directionTo
in class EuclideanVector<Vector2D>
v
- the vector that the returned vector will be directed towardpublic Vector2D lerp(Vector2D p, double t)
V = (1 - t)*A + t*B
, where A
is the current vector and B
is the given vector. This means that if t = 0
, a
vector equal to the current vector will be returned. If t = 1
, a vector equal to the
argument will be returned. The t
parameter is not constrained to the range [0, 1]
,
meaning that linear extrapolation can also be performed with this method.lerp
in class EuclideanVector<Vector2D>
p
- other vectort
- interpolation parameterpublic double norm()
public double normSq()
public Vector2D.Unit normalize()
public Vector2D.Unit normalizeOrNull()
public double distanceSq(Vector2D v)
public double angle(Vector2D v)
This method computes the angular separation between the two vectors using the dot product for well separated vectors and the cross product for almost aligned vectors. This allows to have a good accuracy in all cases, even for vectors very close to each other.
public Vector2D project(Vector2D base)
base
. Vector projection and rejection onto
a given base are related by the equation
v = vprojection + vrejection
project
in class MultiDimensionalEuclideanVector<Vector2D>
base
- base vectorbase
MultiDimensionalEuclideanVector.reject(MultiDimensionalEuclideanVector)
public Vector2D reject(Vector2D base)
base
. This operation can be interpreted as
returning the orthogonal projection of the instance onto the hyperplane
orthogonal to base
. Vector projection and rejection onto
a given base are related by the equation
v = vprojection + vrejection
reject
in class MultiDimensionalEuclideanVector<Vector2D>
base
- base vectorbase
MultiDimensionalEuclideanVector.project(MultiDimensionalEuclideanVector)
public Vector2D.Unit orthogonal()
pi/2
radians
counterclockwise around the origin and normalizing. For example, if this method is
called on a vector pointing along the positive x-axis, then a unit vector representing
the positive y-axis is returned.orthogonal
in class MultiDimensionalEuclideanVector<Vector2D>
IllegalArgumentException
- if the norm of the current instance is zero, NaN, or infinitepublic Vector2D.Unit orthogonal(Vector2D dir)
dir
. This method is equivalent to calling dir.reject(vec).normalize()
except that no intermediate vector object is produced.orthogonal
in class MultiDimensionalEuclideanVector<Vector2D>
dir
- the direction to use for generating the orthogonal vectordir
that does not lie along the current vectorpublic double signedArea(Vector2D v)
The parallelogram in question can be visualized by taking the current instance as the
first side and placing v
at the end of it to create the second. The other sides
are formed by lines parallel to these two vectors. If v
points to the left of
the current instance (ie, the parallelogram is wound counter-clockwise), then the
returned area is positive. If v
points to the right of the current instance,
(ie, the parallelogram is wound clockwise), then the returned area is negative. If
the vectors are collinear (ie, they lie on the same line), then 0 is returned. The area of
the triangle formed by the two vectors is exactly half of the returned value.
v
- vector representing the second side of the constructed parallelogrampublic Vector2D transform(UnaryOperator<Vector2D> fn)
fn
- the function to applypublic boolean eq(Vector2D vec, Precision.DoubleEquivalence precision)
Equality is determined by comparing each pair of components in turn from the two
vectors. If all components evaluate as equal, then the vectors are considered equal. If any are
not equal, then the vectors are not considered equal. Note that this approach means that the
calculated distance between two "equal" vectors may be as much as √(n * eps2)
,
where n
is the number of components in the vector and eps
is the maximum epsilon
value allowed by the precision context.
eq
in class EuclideanVector<Vector2D>
vec
- vector to check for equalityprecision
- precision context used to determine floating point equalitypublic int hashCode()
All NaN values have the same hash code.
public boolean equals(Object other)
If all coordinates of two vectors are exactly the same, and none are
Double.NaN
, the two instances are considered to be equal.
NaN
coordinates are considered to globally affect the vector
and be equal to each other - i.e, if either (or all) coordinates of the
vector are equal to Double.NaN
, the vector is equal to
NaN
.
public static Vector2D of(double x, double y)
x
- abscissa (first coordinate value)y
- abscissa (second coordinate value)public static Vector2D of(double[] v)
v
- coordinates arrayIllegalArgumentException
- if the array does not have 2 elementspublic static Vector2D parse(String str)
toString()
.str
- the string to parseIllegalArgumentException
- if the given string has an invalid formatpublic static Vector2D max(Vector2D first, Vector2D... more)
first
- first vectormore
- additional vectorspublic static Vector2D max(Iterable<Vector2D> vecs)
vecs
- input vectorsIllegalArgumentException
- if the argument does not contain any vectorspublic static Vector2D min(Vector2D first, Vector2D... more)
first
- first vectormore
- more vectorspublic static Vector2D min(Iterable<Vector2D> vecs)
vecs
- input vectorsIllegalArgumentException
- if the argument does not contain any vectorspublic static Vector2D centroid(Vector2D first, Vector2D... more)
first
- first pointmore
- additional pointspublic static Vector2D centroid(Iterable<Vector2D> pts)
pts
- the points to compute the centroid ofIllegalArgumentException
- if the argument contains no pointsCopyright © 2016–2021 The Apache Software Foundation. All rights reserved.