Class Vector1D.Unit
- java.lang.Object
-
- org.apache.commons.geometry.euclidean.EuclideanVector<Vector1D>
-
- org.apache.commons.geometry.euclidean.oned.Vector1D
-
- org.apache.commons.geometry.euclidean.oned.Vector1D.Unit
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.commons.geometry.euclidean.oned.Vector1D
Vector1D.Sum, Vector1D.Unit
-
-
Field Summary
Fields Modifier and Type Field Description static Vector1D.Unit
MINUS
Negation of unit vector (coordinates: -1).static Vector1D.Unit
PLUS
Unit vector (coordinates: 1).-
Fields inherited from class org.apache.commons.geometry.euclidean.oned.Vector1D
COORDINATE_ASCENDING_ORDER, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, ZERO
-
-
Constructor Summary
Constructors Modifier Constructor Description private
Unit(double x)
Simple constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Vector1D.Unit
from(double x)
Create a normalized vector.static Vector1D.Unit
from(Vector1D v)
Create a normalized vector.Vector1D
negate()
Get the negation of the instance.double
norm()
Get the L2 norm (commonly known as the Euclidean norm) for the vector.Vector1D.Unit
normalize()
Get a normalized vector aligned with the instance.Vector1D.Unit
normalizeOrNull()
Attempt to compute a normalized vector aligned with the instance, returning null if such a vector cannot be computed.double
normSq()
Get the square of the L2 norm (also known as the Euclidean norm) for the vector.private static Vector1D.Unit
tryCreateNormalized(double x, boolean throwOnFailure)
Attempt to create a normalized vector from the given coordinate values.Vector1D
withNorm(double mag)
Returns a vector with the same direction but with the given norm.-
Methods inherited from class org.apache.commons.geometry.euclidean.oned.Vector1D
add, add, angle, directionTo, distance, distanceSq, dot, eq, equals, getDimension, getX, getZero, hashCode, isFinite, isInfinite, isNaN, lerp, multiply, of, parse, subtract, subtract, toString, transform, vectorTo
-
Methods inherited from class org.apache.commons.geometry.euclidean.EuclideanVector
getCheckedNorm, isZero
-
-
-
-
Field Detail
-
PLUS
public static final Vector1D.Unit PLUS
Unit vector (coordinates: 1).
-
MINUS
public static final Vector1D.Unit MINUS
Negation of unit vector (coordinates: -1).
-
-
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 Vector1D.Unit normalize()
Get a normalized vector aligned with the instance. The returned vector has a magnitude of 1.
-
normalizeOrNull
public Vector1D.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:
normalizeOrNull
in interfaceVector<Vector1D>
- Overrides:
normalizeOrNull
in classVector1D
- 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()
-
withNorm
public Vector1D 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 Vector1D negate()
Get the negation of the instance.
-
from
public static Vector1D.Unit from(double x)
Create a normalized vector.- Parameters:
x
- 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 Vector1D.Unit from(Vector1D 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 Vector1D.Unit tryCreateNormalized(double x, boolean throwOnFailure)
Attempt to create a normalized vector from the given coordinate values. IfthrowOnFailure
is true, an exception is thrown if a normalized vector cannot be created. Otherwise, null is returned.- Parameters:
x
- x 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
throwOnFailure
is false - Throws:
java.lang.IllegalArgumentException
- if the computed norm is zero, NaN, or infinite
-
-