Vec
public abstract class Vec
ImmutableVec |
An immutable two-dimensional vector, i.e. an (x, y) coordinate pair. |
MutableVec |
A mutable two-dimensional vector, i.e. an (x, y) coordinate pair. |
A two-dimensional vector, i.e. an (x, y) coordinate pair. It can be used to represent either:
-
A two-dimensional offset, i.e. the difference between two points
-
A point in space, i.e. treating the vector as an offset from the origin
Summary
Public fields |
|
|---|---|
static final @NonNull ImmutableVec |
The origin of the coordinate system, i.e. (0, 0). |
Public methods |
|
|---|---|
static final @AngleDegreesFloat @FloatRange(from = 0.0, to = 180.0) float |
absoluteAngleBetweenInDegrees(@NonNull Vec lhs, @NonNull Vec rhs)Returns the absolute angle between the given vectors. |
static final void |
Adds the x and y values of both |
final @FloatRange(from = -180.0, to = 180.0) @AngleDegreesFloat float |
The angle between the positive x-axis and this vec, in the direction of the positive y-axis. |
final @FloatRange(from = 0.0) float |
The length of the |
final @FloatRange(from = 0.0) float |
The squared length of the |
final @NonNull ImmutableVec |
Returns a newly allocated vector with the same magnitude, but pointing in the opposite direction. |
final @NonNull MutableVec |
computeNegation(@NonNull MutableVec outVec)Modifies |
final @NonNull ImmutableVec |
Returns a newly allocated vector with the same magnitude as this one, but rotated by (positive) 90 degrees. |
final @NonNull MutableVec |
computeOrthogonal(@NonNull MutableVec outVec)Modifies |
final @NonNull ImmutableVec |
Returns a newly allocated vector with the same direction as this one, but with a magnitude of |
final @NonNull MutableVec |
computeUnitVec(@NonNull MutableVec outVec)Modifies |
static final float |
determinant(@NonNull Vec lhs, @NonNull Vec rhs)Returns the determinant (×) of the two vectors. |
static final void |
divide(@NonNull Vec lhs, float rhs, @NonNull MutableVec output)Divides the x and y values of the |
static final float |
dotProduct(@NonNull Vec lhs, @NonNull Vec rhs)Returns the dot product (⋅) of the two vectors. |
abstract float |
getX()The |
abstract float |
getY()The |
final boolean |
isAlmostEqual(@NonNull Vec other, @FloatRange(from = 0.0) float tolerance)Compares this |
final boolean |
isParallelTo(Returns true if the angle formed by |
final boolean |
isPerpendicularTo(Returns true if the angle formed by |
static final void |
multiply(float lhs, @NonNull Vec rhs, @NonNull MutableVec output)Multiplies the x and y values of the |
static final void |
multiply(@NonNull Vec lhs, float rhs, @NonNull MutableVec output)Multiplies the x and y values of the |
static final @AngleDegreesFloat @FloatRange(from = -180.0, to = 180.0, fromInclusive = false) float |
signedAngleBetweenInDegrees(@NonNull Vec lhs, @NonNull Vec rhs)Returns the signed angle between the given vectors. |
static final void |
Subtracts the x and y values of |
Extension functions |
|
|---|---|
final @NonNull PointF |
AndroidGraphicsConverter.populatePointF( |
final @NonNull PointF |
AndroidGraphicsConverter.toPointF(@NonNull Vec receiver) |
Public fields
ORIGIN
public static final @NonNull ImmutableVec ORIGIN
The origin of the coordinate system, i.e. (0, 0).
Public methods
absoluteAngleBetweenInDegrees
public static final @AngleDegreesFloat @FloatRange(from = 0.0, to = 180.0) float absoluteAngleBetweenInDegrees(@NonNull Vec lhs, @NonNull Vec rhs)
Returns the absolute angle between the given vectors. The return value will lie in the interval 0, 180.0.
add
public static final void add(@NonNull Vec lhs, @NonNull Vec rhs, @NonNull MutableVec output)
Adds the x and y values of both Vec objects and stores the result in output.
computeDirectionDegrees
public final @FloatRange(from = -180.0, to = 180.0) @AngleDegreesFloat float computeDirectionDegrees()
The angle between the positive x-axis and this vec, in the direction of the positive y-axis. If either component of the vector is NaN, this returns NaN. Otherwise, the returned value will lie in the interval -180, 180, and will have the same sign as the vector's y-component.
Following the behavior of atan2, this will return either ±0 or ±180 for the zero vector, depending on the signs of the zeros.
computeMagnitude
public final @FloatRange(from = 0.0) float computeMagnitude()
The length of the Vec.
computeMagnitudeSquared
public final @FloatRange(from = 0.0) float computeMagnitudeSquared()
The squared length of the Vec.
computeNegation
public final @NonNull ImmutableVec computeNegation()
Returns a newly allocated vector with the same magnitude, but pointing in the opposite direction. For performance-sensitive code, use computeNegation with a pre-allocated instance of MutableVec.
computeNegation
public final @NonNull MutableVec computeNegation(@NonNull MutableVec outVec)
Modifies outVec into a vector with the same magnitude, but pointing in the opposite direction. Returns outVec.
computeOrthogonal
public final @NonNull ImmutableVec computeOrthogonal()
Returns a newly allocated vector with the same magnitude as this one, but rotated by (positive) 90 degrees. For performance-sensitive code, use computeOrthogonal with a pre-allocated instance of MutableVec.
computeOrthogonal
public final @NonNull MutableVec computeOrthogonal(@NonNull MutableVec outVec)
Modifies outVec into a vector with the same magnitude as this one, but rotated by (positive) 90 degrees. Returns outVec.
computeUnitVec
public final @NonNull ImmutableVec computeUnitVec()
Returns a newly allocated vector with the same direction as this one, but with a magnitude of 1. This is equivalent to (but faster than) calling ImmutableVec.fromDirectionInDegreesAndMagnitude with computeDirectionDegrees and 1.
In keeping with the above equivalence, this will return <±1, ±0> for the zero vector, depending on the signs of the zeros.
For performance-sensitive code, use computeUnitVec with a pre-allocated instance of MutableVec.
computeUnitVec
public final @NonNull MutableVec computeUnitVec(@NonNull MutableVec outVec)
Modifies outVec into a vector with the same direction as this one, but with a magnitude of 1. Returns outVec. This is equivalent to (but faster than) calling MutableVec.fromDirectionInDegreesAndMagnitude with computeDirectionDegrees and 1.
In keeping with the above equivalence, this will return <±1, ±0> for the zero vector, depending on the signs of the zeros.
determinant
public static final float determinant(@NonNull Vec lhs, @NonNull Vec rhs)
Returns the determinant (×) of the two vectors. The determinant can be thought of as the z-component of the 3D cross product of the two vectors, if they were placed on the xy-plane in 3D space. The determinant has the property that, for vectors a and b: a × b = ‖a‖ * ‖b‖ * sin(θ) where ‖d‖ is the magnitude of the vector, and θ is the signed angle from a to b.
divide
public static final void divide(@NonNull Vec lhs, float rhs, @NonNull MutableVec output)
Divides the x and y values of the Vec by the Float and stores the result in output.
dotProduct
public static final float dotProduct(@NonNull Vec lhs, @NonNull Vec rhs)
Returns the dot product (⋅) of the two vectors. The dot product has the property that, for vectors a and b: a ⋅ b = ‖a‖ * ‖b‖ * cos(θ) where ‖d‖ is the magnitude of the vector, and θ is the angle from a to b.
isAlmostEqual
public final boolean isAlmostEqual(@NonNull Vec other, @FloatRange(from = 0.0) float tolerance)
Compares this Vec with other, and returns true if the difference between x and other.x is less than tolerance, and likewise for y.
isParallelTo
public final boolean isParallelTo(
@NonNull Vec other,
@AngleDegreesFloat @FloatRange(from = 0.0) float toleranceDegrees
)
Returns true if the angle formed by this and other is within toleranceDegrees of 0 degrees or 180 degrees.
isPerpendicularTo
public final boolean isPerpendicularTo(
@NonNull Vec other,
@AngleDegreesFloat @FloatRange(from = 0.0) float toleranceDegrees
)
Returns true if the angle formed by this and other is within toleranceDegrees of ±90 degrees.
multiply
public static final void multiply(float lhs, @NonNull Vec rhs, @NonNull MutableVec output)
Multiplies the x and y values of the Vec by the Float and stores the result in output.
multiply
public static final void multiply(@NonNull Vec lhs, float rhs, @NonNull MutableVec output)
Multiplies the x and y values of the Vec by the Float and stores the result in output.
signedAngleBetweenInDegrees
public static final @AngleDegreesFloat @FloatRange(from = -180.0, to = 180.0, fromInclusive = false) float signedAngleBetweenInDegrees(@NonNull Vec lhs, @NonNull Vec rhs)
Returns the signed angle between the given vectors. The return value will lie in the interval (-180.0, 180.0]. A positive result indicates the angle between the first vector and the second is in the direction from the positive x-axis towards the positive y-axis.
Extension functions
AndroidGraphicsConverter.populatePointF
public final @NonNull PointF AndroidGraphicsConverter.populatePointF(
@NonNull Vec receiver,
@NonNull PointF out
)
Writes the values from this Vec to out.
Returns the modified PointF instance to allow chaining calls.
AndroidGraphicsConverter.toPointF
public final @NonNull PointF AndroidGraphicsConverter.toPointF(@NonNull Vec receiver)