Package civitas.celestis.math.vector
Class Vector2
java.lang.Object
civitas.celestis.util.tuple.Double2
civitas.celestis.math.vector.Vector2
- All Implemented Interfaces:
Vector<Vector2>,BaseTuple<Double>,DoubleTuple,Serializable
An immutable two-dimensional vector which uses the type
double.- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(double s) Adds a scalar to this vector component-wise, then returns the resulting vector.Adds another vector to this vector, then returns the resulting vector.Between this vector and the provided boundary vectorsminandmax, this returns a new vector whose components are clamped to respect the range of[min, max].doubleReturns the Euclidean distance between this vector and the provided vectorv.doubleReturns the squared Euclidean distance between this vector and the provided vectorv.doubleReturns the Manhattan distance between this vector and the provided vectorv.divide(double s) Divides this vector by a scalar component-wise, then returns the resulting vector.doubleReturns the dot product between this vector and the provided vectorv.booleanChecks for equality between this vector and the provided vectorv.Applies the provided mapper functionfto each component of this tuple, then returns a new tuple containing the return values of the functionf.Between this vector and the provided boundary vectorv, this takes the maximum of each corresponding pair of components, then returns a new vector whose components are populated from that of the maximum values between each pair of components.merge(Vector2 v, BinaryOperator<Double> f) Between this vector and the provided vectorv, this applies the merger functionfto each corresponding pair of components, then returns a new vector whose components are populated from the return values of the functionf.Between this vector and the provided boundary vectorv, this takes the minimum of each corresponding pair of components, then returns a new vector whose components are populated from that of the minimum values between each pair of components.multiply(double s) Multiplies this vector by a scalar component-wise, then returns the resulting vector.Returns the complex number product between this vector and the provided vectorv.negate()Negates all components of this vector, then returns the resulting vector.doublenorm()Returns the Euclidean norm (the magnitude) of this vector.doublenorm2()Returns the squared Euclidean norm (the squared magnitude) of this vector.Normalizes this vector to have a Euclidean norm (magnitude) of1.Normalizes this vector to have a Euclidean norm (magnitude) of1.Normalizes this vector ot have a Euclidean norm (magnitude) of1.doubleReturns the Manhattan norm of this vector.rotate(double t) Rotates this vector counter-clockwise by the provided anglet.subtract(double s) Subtracts this vector by a scalar component-wise, then returns the resulting vector.Subtracts another vector from this vector, then returns the resulting vector.Methods inherited from class civitas.celestis.util.tuple.Double2
array, boxed, contains, containsAll, equals, get, hashCode, isFinite, isInfinite, isNaN, isZero, list, mapToObj, size, stream, toString, x, yMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface civitas.celestis.util.tuple.DoubleTuple
array, boxed, contains, containsAll, doubleArray, equals, get, isFinite, isInfinite, isNaN, isZero, list, mapToObj, size, stream, toString
-
Field Details
-
ZERO
A vector of no direction or magnitude. Represents origin. -
POSITIVE_X
The positive X unit vector. -
POSITIVE_Y
The positive Y unit vector. -
NEGATIVE_X
The negative X unit vector. -
NEGATIVE_Y
The negative Y unit vector.
-
-
Constructor Details
-
Vector2
public Vector2(double x, double y) Creates a new vector.- Parameters:
x- The X component of this vectory- The Y component of this vector
-
Vector2
public Vector2(@Nonnull double[] components) Creates a new vector.- Parameters:
components- An array containing the components of this vector in XY order- Throws:
IllegalArgumentException- When the provided array's length is not2
-
Vector2
Creates a new vector.- Parameters:
t- The tuple of which to copy component values from- Throws:
IllegalArgumentException- When the provided tuplet's size is not2
-
-
Method Details
-
norm
public double norm()Returns the Euclidean norm (the magnitude) of this vector. -
norm2
public double norm2()Returns the squared Euclidean norm (the squared magnitude) of this vector. -
normManhattan
public double normManhattan()Returns the Manhattan norm of this vector.- Specified by:
normManhattanin interfaceVector<Vector2>- Returns:
- The Manhattan norm of this vector
-
add
Adds a scalar to this vector component-wise, then returns the resulting vector. -
subtract
Subtracts this vector by a scalar component-wise, then returns the resulting vector. -
multiply
Multiplies this vector by a scalar component-wise, then returns the resulting vector. -
divide
Divides this vector by a scalar component-wise, then returns the resulting vector.- Specified by:
dividein interfaceVector<Vector2>- Parameters:
s- The scalar of which to divide each component of this vector by- Returns:
- The resulting vector
- Throws:
ArithmeticException- When the denominatorsis zero
-
add
Adds another vector to this vector, then returns the resulting vector. -
subtract
Subtracts another vector from this vector, then returns the resulting vector. -
multiply
Returns the complex number product between this vector and the provided vectorv.- Parameters:
v- The vector of which to get the complex number product between- Returns:
- The complex number product between the two vectors
-
dot
Returns the dot product between this vector and the provided vectorv. -
normalize
Normalizes this vector to have a Euclidean norm (magnitude) of1.- Specified by:
normalizein interfaceVector<Vector2>- Returns:
- The normalized vector of this vector
- Throws:
ArithmeticException- When the Euclidean norm of this vector is zero
-
normalizeOrZero
Normalizes this vector ot have a Euclidean norm (magnitude) of1. If this vector's Euclidean norm is zero, this will return itself. (which is guaranteed to be a vector where all components are zero, as only zero vectors have a Euclidean norm of zero)- Specified by:
normalizeOrZeroin interfaceVector<Vector2>- Returns:
- The normalized vector of this vector if successful, zero otherwise
-
normalizeOrDefault
Normalizes this vector to have a Euclidean norm (magnitude) of1. If this vector's Euclidean norm is zero, this will return the provided fallback valuev.- Specified by:
normalizeOrDefaultin interfaceVector<Vector2>- Parameters:
v- The value of which to return in case of division by zero- Returns:
- The normalized vector of this vector if successful, the fallback value
votherwise
-
negate
Negates all components of this vector, then returns the resulting vector. -
min
Between this vector and the provided boundary vectorv, this takes the minimum of each corresponding pair of components, then returns a new vector whose components are populated from that of the minimum values between each pair of components. -
max
Between this vector and the provided boundary vectorv, this takes the maximum of each corresponding pair of components, then returns a new vector whose components are populated from that of the maximum values between each pair of components. -
clamp
Between this vector and the provided boundary vectorsminandmax, this returns a new vector whose components are clamped to respect the range of[min, max]. This is the equivalent of clamping each component to the corresponding pair of minimum and maximum values. -
distance
Returns the Euclidean distance between this vector and the provided vectorv. -
distance2
Returns the squared Euclidean distance between this vector and the provided vectorv. -
distanceManhattan
Returns the Manhattan distance between this vector and the provided vectorv.- Specified by:
distanceManhattanin interfaceVector<Vector2>- Parameters:
v- The vector of which to get the Manhattan distance between- Returns:
- The Manhattan distance between the two vectors
-
map
Applies the provided mapper functionfto each component of this tuple, then returns a new tuple containing the return values of the functionf. -
merge
Between this vector and the provided vectorv, this applies the merger functionfto each corresponding pair of components, then returns a new vector whose components are populated from the return values of the functionf. -
rotate
Rotates this vector counter-clockwise by the provided anglet. Angle is denoted in radians.- Parameters:
t- The angle of rotation to apply in radians- Returns:
- The rotated vector
- See Also:
-
equals
Checks for equality between this vector and the provided vectorv.
-