Vector2d

data class Vector2d<U : Unit<U>>(val x: Measure<U>, val y: Measure<U>)(source)

A 2D vector with components in a specified unit system.

This class represents a 2D vector \(\mathbf{v} = (x, y)\) where both components have the same unit type \(U\). Common use cases include position vectors (with distance units), velocity vectors (with velocity units), and acceleration vectors (with acceleration units).

Operations

Vector arithmetic:

  • Addition: \(\mathbf{v}_1 + \mathbf{v}_2 = (x_1 + x_2, y_1 + y_2)\)

  • Subtraction: \(\mathbf{v}_1 - \mathbf{v}_2 = (x_1 - x_2, y_1 - y_2)\)

  • Negation: \(-\mathbf{v} = (-x, -y)\)

Scalar operations:

  • Multiplication: \(c \cdot \mathbf{v} = (c \cdot x, c \cdot y)\)

  • Division: \(\mathbf{v} / c = (x / c, y / c)\)

Vector products:

  • Dot product: \(\mathbf{v}_1 \cdot \mathbf{v}_2 = x_1 x_2 + y_1 y_2\)

  • Squared norm: \(\|\mathbf{v}\|^2 = x^2 + y^2\)

  • Norm (magnitude): \(\|\mathbf{v}\| = \sqrt{x^2 + y^2}\)

Example Usage

// Position vector
val position = Vector2d(10.0.inches, 5.0.inches)

// Velocity vector
val velocity = Vector2d(2.0.inchesPerSecond, 3.0.inchesPerSecond)

// Vector operations
val sum = position + Vector2d(1.0.inches, 2.0.inches)
val scaled = position * 2.0
val magnitude = position.norm()

Type Parameters

U

the unit type for both components (e.g., DistanceUnit, VelocityUnit)

Constructors

Link copied to clipboard
constructor(x: Measure<U>, y: Measure<U>)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

the x-component of the vector

Link copied to clipboard

the y-component of the vector

Functions

Link copied to clipboard

Returns the angle of this vector as a Rotation2d.

Link copied to clipboard

Returns the angle of this vector as a Rotation2d, assuming this vector is already normalized (unit length).

Link copied to clipboard

Converts this vector to a pair of measurements.

Link copied to clipboard
operator fun div(z: Double): Vector2d<U>

Divides the vector by a scalar.

Link copied to clipboard
infix fun dot(v: Vector2d<U>): Double

Computes the dot product with another vector.

Link copied to clipboard
fun lerp(other: Vector2d<U>, t: Double): Vector2d<U>

Linear interpolation (lerp) toward another vector.

Link copied to clipboard
operator fun minus(v: Vector2d<U>): Vector2d<U>

Subtracts two vectors component-wise.

Link copied to clipboard
fun norm(): Double

Computes the Euclidean norm (magnitude) of the vector.

Link copied to clipboard
operator fun plus(v: Vector2d<U>): Vector2d<U>

Adds two vectors component-wise.

Link copied to clipboard

Computes the squared norm (magnitude squared) of the vector.

Link copied to clipboard
operator fun times(z: Double): Vector2d<U>

Multiplies the vector by a scalar.

Link copied to clipboard
@JvmName(name = "velTimesTime")
operator fun Vector2d<PerUnit<DistanceUnit, TimeUnit>>.times(other: Time): Vector2d<DistanceUnit>
Link copied to clipboard
operator fun unaryMinus(): Vector2d<U>

Negates the vector.