Vec
-
android
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 companion functions |
||
|---|---|---|
@AngleDegreesFloat @FloatRange(from = 0.0, to = 180.0) Float |
absoluteAngleBetweenInDegrees(lhs: Vec, rhs: Vec)Returns the absolute angle between the given vectors. |
android
|
Unit |
add(lhs: Vec, rhs: Vec, output: MutableVec)Adds the x and y values of both |
android
|
Float |
determinant(lhs: Vec, rhs: Vec)Returns the determinant (×) of the two vectors. |
android
|
Unit |
divide(lhs: Vec, rhs: Float, output: MutableVec)Divides the x and y values of the |
android
|
Float |
dotProduct(lhs: Vec, rhs: Vec)Returns the dot product (⋅) of the two vectors. |
android
|
Unit |
multiply(lhs: Float, rhs: Vec, output: MutableVec)Multiplies the x and y values of the |
android
|
Unit |
multiply(lhs: Vec, rhs: Float, output: MutableVec)Multiplies the x and y values of the |
android
|
@AngleDegreesFloat @FloatRange(from = -180.0, to = 180.0, fromInclusive = false) Float |
signedAngleBetweenInDegrees(lhs: Vec, rhs: Vec)Returns the signed angle between the given vectors. |
android
|
Unit |
subtract(lhs: Vec, rhs: Vec, output: MutableVec)Subtracts the x and y values of |
android
|
Public companion properties |
||
|---|---|---|
ImmutableVec |
The origin of the coordinate system, i.e. (0, 0). |
android
|
Public functions |
||
|---|---|---|
@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. |
android
|
@FloatRange(from = 0.0) Float |
The length of the |
android
|
@FloatRange(from = 0.0) Float |
The squared length of the |
android
|
ImmutableVec |
Returns a newly allocated vector with the same magnitude, but pointing in the opposite direction. |
android
|
MutableVec |
computeNegation(outVec: MutableVec)Modifies |
android
|
ImmutableVec |
Returns a newly allocated vector with the same magnitude as this one, but rotated by (positive) 90 degrees. |
android
|
MutableVec |
computeOrthogonal(outVec: MutableVec)Modifies |
android
|
ImmutableVec |
Returns a newly allocated vector with the same direction as this one, but with a magnitude of |
android
|
MutableVec |
computeUnitVec(outVec: MutableVec)Modifies |
android
|
Boolean |
isAlmostEqual(other: Vec, tolerance: @FloatRange(from = 0.0) Float)Compares this |
android
|
Boolean |
isParallelTo(Returns true if the angle formed by |
android
|
Boolean |
isPerpendicularTo(Returns true if the angle formed by |
android
|
Public properties |
||
|---|---|---|
abstract Float |
The |
android
|
abstract Float |
The |
android
|
Public companion functions
absoluteAngleBetweenInDegrees
fun absoluteAngleBetweenInDegrees(lhs: Vec, rhs: Vec): @AngleDegreesFloat @FloatRange(from = 0.0, to = 180.0) Float
Returns the absolute angle between the given vectors. The return value will lie in the interval 0, 180.0.
add
fun add(lhs: Vec, rhs: Vec, output: MutableVec): Unit
Adds the x and y values of both Vec objects and stores the result in output.
determinant
fun determinant(lhs: Vec, rhs: Vec): Float
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
fun divide(lhs: Vec, rhs: Float, output: MutableVec): Unit
Divides the x and y values of the Vec by the Float and stores the result in output.
dotProduct
fun dotProduct(lhs: Vec, rhs: Vec): Float
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.
multiply
fun multiply(lhs: Float, rhs: Vec, output: MutableVec): Unit
Multiplies the x and y values of the Vec by the Float and stores the result in output.
multiply
fun multiply(lhs: Vec, rhs: Float, output: MutableVec): Unit
Multiplies the x and y values of the Vec by the Float and stores the result in output.
signedAngleBetweenInDegrees
fun signedAngleBetweenInDegrees(lhs: Vec, rhs: Vec): @AngleDegreesFloat @FloatRange(from = -180.0, to = 180.0, fromInclusive = false) Float
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.
Public companion properties
Public functions
computeDirectionDegrees
fun computeDirectionDegrees(): @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. 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.
computeMagnitudeSquared
fun computeMagnitudeSquared(): @FloatRange(from = 0.0) Float
The squared length of the Vec.
computeNegation
fun computeNegation(): ImmutableVec
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
fun computeNegation(outVec: MutableVec): MutableVec
Modifies outVec into a vector with the same magnitude, but pointing in the opposite direction. Returns outVec.
computeOrthogonal
fun computeOrthogonal(): ImmutableVec
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
fun computeOrthogonal(outVec: MutableVec): MutableVec
Modifies outVec into a vector with the same magnitude as this one, but rotated by (positive) 90 degrees. Returns outVec.
computeUnitVec
fun computeUnitVec(): ImmutableVec
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
fun computeUnitVec(outVec: MutableVec): MutableVec
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.
isAlmostEqual
fun isAlmostEqual(other: Vec, tolerance: @FloatRange(from = 0.0) Float = 1.0E-4f): Boolean
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
fun isParallelTo(
other: Vec,
toleranceDegrees: @AngleDegreesFloat @FloatRange(from = 0.0) Float
): Boolean
Returns true if the angle formed by this and other is within toleranceDegrees of 0 degrees or 180 degrees.
isPerpendicularTo
fun isPerpendicularTo(
other: Vec,
toleranceDegrees: @AngleDegreesFloat @FloatRange(from = 0.0) Float
): Boolean
Returns true if the angle formed by this and other is within toleranceDegrees of ±90 degrees.