Class Vector2D.Unit
- java.lang.Object
-
- org.apache.commons.geometry.euclidean.EuclideanVector<V>
-
- org.apache.commons.geometry.euclidean.MultiDimensionalEuclideanVector<Vector2D>
-
- org.apache.commons.geometry.euclidean.twod.Vector2D
-
- org.apache.commons.geometry.euclidean.twod.Vector2D.Unit
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.commons.geometry.euclidean.twod.Vector2D
Vector2D.Sum, Vector2D.Unit
-
-
Field Summary
Fields Modifier and Type Field Description static Vector2D.UnitMINUS_XNegation of unit vector (coordinates: -1, 0).static Vector2D.UnitMINUS_YNegation of unit vector (coordinates: 0, -1).static Vector2D.UnitPLUS_XUnit vector (coordinates: 1, 0).static Vector2D.UnitPLUS_YUnit vector (coordinates: 0, 1).private static doubleSCALE_DOWN_FACTORFactor used to scale down coordinate values in order to produce normalized coordinates without overflow or underflow.private static doubleSCALE_UP_FACTORFactor used to scale up coordinate values in order to produce normalized coordinates without overflow or underflow.private static doubleUNSCALED_MAXMaximum coordinate value for computing normalized vectors with raw, unscaled values.-
Fields inherited from class org.apache.commons.geometry.euclidean.twod.Vector2D
COORDINATE_ASCENDING_ORDER, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, ZERO
-
-
Constructor Summary
Constructors Modifier Constructor Description privateUnit(double x, double y)Simple constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Vector2D.Unitfrom(double x, double y)Create a normalized vector.static Vector2D.Unitfrom(Vector2D v)Create a normalized vector.Vector2D.Unitnegate()Get the negation of the instance.doublenorm()Get the L2 norm (commonly known as the Euclidean norm) for the vector.Vector2D.Unitnormalize()Get a normalized vector aligned with the instance.Vector2D.UnitnormalizeOrNull()Attempt to compute a normalized vector aligned with the instance, returning null if such a vector cannot be computed.doublenormSq()Get the square of the L2 norm (also known as the Euclidean norm) for the vector.Vector2D.Unitorthogonal()Get a unit vector orthogonal to the instance.private static Vector2D.UnittryCreateNormalized(double x, double y, boolean throwOnFailure)Attempt to create a normalized vector from the given coordinate values.Vector2DwithNorm(double mag)Returns a vector with the same direction but with the given norm.-
Methods inherited from class org.apache.commons.geometry.euclidean.twod.Vector2D
add, add, angle, centroid, centroid, directionTo, distance, distanceSq, dot, eq, equals, getDimension, getX, getY, getZero, hashCode, isFinite, isInfinite, isNaN, lerp, max, max, min, min, multiply, of, of, orthogonal, parse, project, reject, signedArea, subtract, subtract, toArray, toString, transform, vectorTo
-
Methods inherited from class org.apache.commons.geometry.euclidean.EuclideanVector
getCheckedNorm, isZero
-
-
-
-
Field Detail
-
PLUS_X
public static final Vector2D.Unit PLUS_X
Unit vector (coordinates: 1, 0).
-
MINUS_X
public static final Vector2D.Unit MINUS_X
Negation of unit vector (coordinates: -1, 0).
-
PLUS_Y
public static final Vector2D.Unit PLUS_Y
Unit vector (coordinates: 0, 1).
-
MINUS_Y
public static final Vector2D.Unit MINUS_Y
Negation of unit vector (coordinates: 0, -1).
-
UNSCALED_MAX
private static final double UNSCALED_MAX
Maximum coordinate value for computing normalized vectors with raw, unscaled values.- See Also:
- Constant Field Values
-
SCALE_UP_FACTOR
private static final double SCALE_UP_FACTOR
Factor used to scale up coordinate values in order to produce normalized coordinates without overflow or underflow.- See Also:
- Constant Field Values
-
SCALE_DOWN_FACTOR
private static final double SCALE_DOWN_FACTOR
Factor used to scale down coordinate values in order to produce normalized coordinates without overflow or underflow.- See Also:
- Constant Field Values
-
-
Method Detail
-
norm
public double norm()
Get the L2 norm (commonly known as the Euclidean norm) for the vector. This corresponds to the common notion of vector magnitude or length and is defined as the square root of the sum of the squares of all vector components.
-
normSq
public double normSq()
Get the square of the L2 norm (also known as the Euclidean norm) for the vector. This is equal to the sum of the squares of all vector components.
-
normalize
public Vector2D.Unit normalize()
Get a normalized vector aligned with the instance. The returned vector has a magnitude of 1.
-
normalizeOrNull
public Vector2D.Unit normalizeOrNull()
Attempt to compute a normalized vector aligned with the instance, returning null if such a vector cannot be computed. This method is equivalent toVector.normalize()but returns null instead of throwing an exception on failure.- Specified by:
normalizeOrNullin interfaceVector<Vector2D>- Overrides:
normalizeOrNullin classVector2D- Returns:
- normalized vector or null if such a vector cannot be computed, i.e. if the norm is zero, NaN, or infinite
- See Also:
Vector.normalize()
-
orthogonal
public Vector2D.Unit orthogonal()
Get a unit vector orthogonal to the instance. The returned vector is computed by rotating the current instancepi/2radians 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.- Overrides:
orthogonalin classVector2D- Returns:
- a unit vector orthogonal to the current instance
-
withNorm
public Vector2D withNorm(double mag)
Returns a vector with the same direction but with the given norm. This is equivalent to callingvec.normalize().scalarMultiply(mag)but without the intermediate vector.
-
negate
public Vector2D.Unit negate()
Get the negation of the instance.
-
from
public static Vector2D.Unit from(double x, double y)
Create a normalized vector.- Parameters:
x- Vector coordinate.y- Vector coordinate.- Returns:
- a vector whose norm is 1.
- Throws:
java.lang.IllegalArgumentException- if the norm of the given value is zero, NaN, or infinite
-
from
public static Vector2D.Unit from(Vector2D v)
Create a normalized vector.- Parameters:
v- Vector.- Returns:
- a vector whose norm is 1.
- Throws:
java.lang.IllegalArgumentException- if the norm of the given value is zero, NaN, or infinite
-
tryCreateNormalized
private static Vector2D.Unit tryCreateNormalized(double x, double y, boolean throwOnFailure)
Attempt to create a normalized vector from the given coordinate values. IfthrowOnFailureis true, an exception is thrown if a normalized vector cannot be created. Otherwise, null is returned.- Parameters:
x- x coordinatey- y coordinatethrowOnFailure- if true, an exception will be thrown if a normalized vector cannot be created- Returns:
- normalized vector or null if one cannot be created and
throwOnFailureis false - Throws:
java.lang.IllegalArgumentException- if the computed norm is zero, NaN, or infinite
-
-