Class Vector2D
- All Implemented Interfaces:
Serializable,Point<Euclidean2D>,Vector<Euclidean2D>
Instances of this class are guaranteed to be immutable.
- Since:
- 3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Vector2DA vector with all coordinates set to NaN.static final Vector2DA vector with all coordinates set to negative infinity.static final Vector2DA vector with all coordinates set to positive infinity.static final Vector2DOrigin (coordinates: 0, 0). -
Constructor Summary
ConstructorsConstructorDescriptionVector2D(double[] v) Simple constructor.Vector2D(double x, double y) Simple constructor.Multiplicative constructor Build a vector from another one and a scale factor.Linear constructor Build a vector from two other ones and corresponding scale factors.Linear constructor Build a vector from three other ones and corresponding scale factors.Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, double a3, Vector2D u3, double a4, Vector2D u4) Linear constructor Build a vector from four other ones and corresponding scale factors. -
Method Summary
Modifier and TypeMethodDescriptionadd(double factor, Vector<Euclidean2D> v) Add a scaled vector to the instance.add(Vector<Euclidean2D> v) Add a vector to the instance.static doubleCompute the angular separation between two vectors.doublecrossProduct(Vector2D p1, Vector2D p2) Compute the cross-product of the instance and the given points.static doubleCompute the distance between two vectors according to the L2 norm.doubledistance(Point<Euclidean2D> p) Compute the distance between the instance and another point.doubleCompute the distance between the instance and another vector according to the L2 norm.doubleCompute the distance between the instance and another vector according to the L1 norm.static doubledistanceInf(Vector2D p1, Vector2D p2) Compute the distance between two vectors according to the L∞ norm.doubleCompute the distance between the instance and another vector according to the L∞ norm.static doubledistanceSq(Vector2D p1, Vector2D p2) Compute the square of the distance between two vectors.doubleCompute the square of the distance between the instance and another vector.doubleCompute the dot-product of the instance and another vector.booleanTest for the equality of two 2D vectors.doublegetNorm()Get the L2 norm for the vector.doublegetNorm1()Get the L1 norm for the vector.doubleGet the L∞ norm for the vector.doubleGet the square of the norm for the vector.getSpace()Get the space to which the point belongs.doublegetX()Get the abscissa of the vector.doublegetY()Get the ordinate of the vector.getZero()Get the null vector of the vectorial space or origin point of the affine space.inthashCode()Get a hashCode for the 2D vector.booleanReturns true if any coordinate of this vector is infinite and none are NaN; false otherwisebooleanisNaN()Returns true if any coordinate of this point is NaN; false otherwisenegate()Get the opposite of the instance.Get a normalized vector aligned with the instance.scalarMultiply(double a) Multiply the instance by a scalar.subtract(double factor, Vector<Euclidean2D> v) Subtract a scaled vector from the instance.Subtract a vector from the instance.double[]toArray()Get the vector coordinates as a dimension 2 array.toString()Get a string representation of this vector.toString(NumberFormat format) Get a string representation of this vector.
-
Field Details
-
ZERO
Origin (coordinates: 0, 0). -
NaN
A vector with all coordinates set to NaN. -
POSITIVE_INFINITY
A vector with all coordinates set to positive infinity. -
NEGATIVE_INFINITY
A vector with all coordinates set to negative infinity.
-
-
Constructor Details
-
Vector2D
public Vector2D(double x, double y) Simple constructor. Build a vector from its coordinates- Parameters:
x- abscissay- ordinate- See Also:
-
Vector2D
Simple constructor. Build a vector from its coordinates- Parameters:
v- coordinates array- Throws:
DimensionMismatchException- if array does not have 2 elements- See Also:
-
Vector2D
Multiplicative constructor Build a vector from another one and a scale factor. The vector built will be a * u- Parameters:
a- scale factoru- base (unscaled) vector
-
Vector2D
Linear constructor Build a vector from two other ones and corresponding scale factors. The vector built will be a1 * u1 + a2 * u2- Parameters:
a1- first scale factoru1- first base (unscaled) vectora2- second scale factoru2- second base (unscaled) vector
-
Vector2D
Linear constructor Build a vector from three other ones and corresponding scale factors. The vector built will be a1 * u1 + a2 * u2 + a3 * u3- Parameters:
a1- first scale factoru1- first base (unscaled) vectora2- second scale factoru2- second base (unscaled) vectora3- third scale factoru3- third base (unscaled) vector
-
Vector2D
public Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, double a3, Vector2D u3, double a4, Vector2D u4) Linear constructor Build a vector from four other ones and corresponding scale factors. The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4- Parameters:
a1- first scale factoru1- first base (unscaled) vectora2- second scale factoru2- second base (unscaled) vectora3- third scale factoru3- third base (unscaled) vectora4- fourth scale factoru4- fourth base (unscaled) vector
-
-
Method Details
-
getX
public double getX()Get the abscissa of the vector.- Returns:
- abscissa of the vector
- See Also:
-
getY
public double getY()Get the ordinate of the vector.- Returns:
- ordinate of the vector
- See Also:
-
toArray
public double[] toArray()Get the vector coordinates as a dimension 2 array.- Returns:
- vector coordinates
- See Also:
-
getSpace
Get the space to which the point belongs.- Specified by:
getSpacein interfacePoint<Euclidean2D>- Returns:
- containing space
-
getZero
Get the null vector of the vectorial space or origin point of the affine space.- Specified by:
getZeroin interfaceVector<Euclidean2D>- Returns:
- null vector of the vectorial space or origin point of the affine space
-
getNorm1
public double getNorm1()Get the L1 norm for the vector.- Specified by:
getNorm1in interfaceVector<Euclidean2D>- Returns:
- L1 norm for the vector
-
getNorm
public double getNorm()Get the L2 norm for the vector.- Specified by:
getNormin interfaceVector<Euclidean2D>- Returns:
- Euclidean norm for the vector
-
getNormSq
public double getNormSq()Get the square of the norm for the vector.- Specified by:
getNormSqin interfaceVector<Euclidean2D>- Returns:
- square of the Euclidean norm for the vector
-
getNormInf
public double getNormInf()Get the L∞ norm for the vector.- Specified by:
getNormInfin interfaceVector<Euclidean2D>- Returns:
- L∞ norm for the vector
-
add
Add a vector to the instance.- Specified by:
addin interfaceVector<Euclidean2D>- Parameters:
v- vector to add- Returns:
- a new vector
-
add
Add a scaled vector to the instance.- Specified by:
addin interfaceVector<Euclidean2D>- Parameters:
factor- scale factor to apply to v before adding itv- vector to add- Returns:
- a new vector
-
subtract
Subtract a vector from the instance.- Specified by:
subtractin interfaceVector<Euclidean2D>- Parameters:
p- vector to subtract- Returns:
- a new vector
-
subtract
Subtract a scaled vector from the instance.- Specified by:
subtractin interfaceVector<Euclidean2D>- Parameters:
factor- scale factor to apply to v before subtracting itv- vector to subtract- Returns:
- a new vector
-
normalize
Get a normalized vector aligned with the instance.- Specified by:
normalizein interfaceVector<Euclidean2D>- Returns:
- a new normalized vector
- Throws:
MathArithmeticException- if the norm is zero
-
angle
Compute the angular separation between two vectors.This method computes the angular separation between 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.
- Parameters:
v1- first vectorv2- second vector- Returns:
- angular separation between v1 and v2
- Throws:
MathArithmeticException- if either vector has a null norm
-
negate
Get the opposite of the instance.- Specified by:
negatein interfaceVector<Euclidean2D>- Returns:
- a new vector which is opposite to the instance
-
scalarMultiply
Multiply the instance by a scalar.- Specified by:
scalarMultiplyin interfaceVector<Euclidean2D>- Parameters:
a- scalar- Returns:
- a new vector
-
isNaN
public boolean isNaN()Returns true if any coordinate of this point is NaN; false otherwise- Specified by:
isNaNin interfacePoint<Euclidean2D>- Returns:
- true if any coordinate of this point is NaN; false otherwise
-
isInfinite
public boolean isInfinite()Returns true if any coordinate of this vector is infinite and none are NaN; false otherwise- Specified by:
isInfinitein interfaceVector<Euclidean2D>- Returns:
- true if any coordinate of this vector is infinite and none are NaN; false otherwise
-
distance1
Compute the distance between the instance and another vector according to the L1 norm.Calling this method is equivalent to calling:
q.subtract(p).getNorm1()except that no intermediate vector is built- Specified by:
distance1in interfaceVector<Euclidean2D>- Parameters:
p- second vector- Returns:
- the distance between the instance and p according to the L1 norm
-
distance
Compute the distance between the instance and another vector according to the L2 norm.Calling this method is equivalent to calling:
q.subtract(p).getNorm()except that no intermediate vector is built- Specified by:
distancein interfaceVector<Euclidean2D>- Parameters:
p- second vector- Returns:
- the distance between the instance and p according to the L2 norm
-
distance
Compute the distance between the instance and another point.- Specified by:
distancein interfacePoint<Euclidean2D>- Parameters:
p- second point- Returns:
- the distance between the instance and p
-
distanceInf
Compute the distance between the instance and another vector according to the L∞ norm.Calling this method is equivalent to calling:
q.subtract(p).getNormInf()except that no intermediate vector is built- Specified by:
distanceInfin interfaceVector<Euclidean2D>- Parameters:
p- second vector- Returns:
- the distance between the instance and p according to the L∞ norm
-
distanceSq
Compute the square of the distance between the instance and another vector.Calling this method is equivalent to calling:
q.subtract(p).getNormSq()except that no intermediate vector is built- Specified by:
distanceSqin interfaceVector<Euclidean2D>- Parameters:
p- second vector- Returns:
- the square of the distance between the instance and p
-
dotProduct
Compute the dot-product of the instance and another vector.- Specified by:
dotProductin interfaceVector<Euclidean2D>- Parameters:
v- second vector- Returns:
- the dot product this.v
-
crossProduct
Compute the cross-product of the instance and the given points.The cross product can be used to determine the location of a point with regard to the line formed by (p1, p2) and is calculated as: \[ P = (x_2 - x_1)(y_3 - y_1) - (y_2 - y_1)(x_3 - x_1) \] with \(p3 = (x_3, y_3)\) being this instance.
If the result is 0, the points are collinear, i.e. lie on a single straight line L; if it is positive, this point lies to the left, otherwise to the right of the line formed by (p1, p2).
- Parameters:
p1- first point of the linep2- second point of the line- Returns:
- the cross-product
- See Also:
-
distance
Compute the distance between two vectors according to the L2 norm.Calling this method is equivalent to calling:
p1.subtract(p2).getNorm()except that no intermediate vector is built- Parameters:
p1- first vectorp2- second vector- Returns:
- the distance between p1 and p2 according to the L2 norm
-
distanceInf
Compute the distance between two vectors according to the L∞ norm.Calling this method is equivalent to calling:
p1.subtract(p2).getNormInf()except that no intermediate vector is built- Parameters:
p1- first vectorp2- second vector- Returns:
- the distance between p1 and p2 according to the L∞ norm
-
distanceSq
Compute the square of the distance between two vectors.Calling this method is equivalent to calling:
p1.subtract(p2).getNormSq()except that no intermediate vector is built- Parameters:
p1- first vectorp2- second vector- Returns:
- the square of the distance between p1 and p2
-
equals
Test for the equality of two 2D vectors.If all coordinates of two 2D vectors are exactly the same, and none are
Double.NaN, the two 2D vectors are considered to be equal.NaNcoordinates are considered to affect globally the vector and be equals to each other - i.e, if either (or all) coordinates of the 2D vector are equal toDouble.NaN, the 2D vector is equal toNaN. -
hashCode
public int hashCode()Get a hashCode for the 2D vector.All NaN values have the same hash code.
-
toString
Get a string representation of this vector. -
toString
Get a string representation of this vector.- Specified by:
toStringin interfaceVector<Euclidean2D>- Parameters:
format- the custom format for components- Returns:
- a string representation of this vector
-