• android
    abstract class Vec
Known direct subclasses
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:

  1. A two-dimensional offset, i.e. the difference between two points

  2. 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

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 Vec objects and stores the result in output, which may be lhs or rhs.

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 Vec by the Float and stores the result in output, which may be lhs.

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 Vec by the Float and stores the result in output, which may be rhs.

android
Unit
multiply(lhs: Vec, rhs: Float, output: MutableVec)

Multiplies the x and y values of the Vec by the Float and stores the result in output, which may be lhs.

android
@AngleDegreesFloat @FloatRange(from = -180.0, to = 180.0, fromInclusive = false) Float

Returns the signed angle between the given vectors.

android
Unit
subtract(lhs: Vec, rhs: Vec, output: MutableVec)

Subtracts the x and y values of rhs from the x and y values of lhs and stores the result in output, which may be lhs or rhs.

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 Vec.

android
@FloatRange(from = 0.0) Float

The squared length of the Vec.

android
ImmutableVec

Returns a newly allocated vector with the same magnitude, but pointing in the opposite direction.

android
MutableVec

Modifies outVec, which may be this, into a vector with the same magnitude, but pointing in the opposite direction.

android
ImmutableVec

Returns a newly allocated vector with the same magnitude as this one, but rotated by (positive) 90 degrees.

android
MutableVec

Modifies outVec, which may be this, into a vector with the same magnitude as this one, but rotated by (positive) 90 degrees.

android
ImmutableVec

Returns a newly allocated vector with the same direction as this one, but with a magnitude of 1.

android
MutableVec

Modifies outVec, which may be this, into a vector with the same direction as this one, but with a magnitude of 1.

android
Boolean
isAlmostEqual(other: Vec, tolerance: @FloatRange(from = 0.0) Float)

Compares this Vec with other, and returns true if the difference between x from this and x from other is less than tolerance, and likewise for y.

android
Boolean
isParallelTo(
    other: Vec,
    toleranceDegrees: @AngleDegreesFloat @FloatRange(from = 0.0) Float
)

Returns true if the angle formed by this and other is within toleranceDegrees of 0 degrees or 180 degrees.

android
Boolean
isPerpendicularTo(
    other: Vec,
    toleranceDegrees: @AngleDegreesFloat @FloatRange(from = 0.0) Float
)

Returns true if the angle formed by this and other is within toleranceDegrees of ±90 degrees.

android

Public properties

abstract Float

The Vec's offset in the x-direction

android
abstract Float

The Vec's offset in the y-direction

android

Extension functions

PointF

Writes the values from this Vec to out.

android
PointF

Constructs a PointF with the same coordinates as the Vec

android
Offset

Returns an Offset with the values from this Vec.

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, which may be lhs or rhs.

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, which may be lhs.

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, which may be rhs.

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, which may be lhs.

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.

subtract

fun subtract(lhs: Vec, rhs: Vec, output: MutableVec): Unit

Subtracts the x and y values of rhs from the x and y values of lhs and stores the result in output, which may be lhs or rhs.

Public companion properties

ORIGIN

val ORIGINImmutableVec

The origin of the coordinate system, i.e. (0, 0).

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.

computeMagnitude

fun computeMagnitude(): @FloatRange(from = 0.0) Float

The length of the Vec.

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, which may be this, 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, which may be this, 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, which may be this, 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.populateFromDirectionInDegreesAndMagnitude 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 from this and x from other 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.

Public properties

x

abstract val xFloat

The Vec's offset in the x-direction

y

abstract val yFloat

The Vec's offset in the y-direction

Extension functions

populatePointF

fun Vec.populatePointF(out: PointF): PointF

Writes the values from this Vec to out.

Returns the modified PointF instance to allow chaining calls.

Returns
PointF

out

toPointF

fun Vec.toPointF(): PointF

Constructs a PointF with the same coordinates as the Vec

toOffset

fun Vec.toOffset(): Offset

Returns an Offset with the values from this Vec.