Vector2

class Vector2(var x: Double = 0.0, var y: Double = 0.0) : Iterable<Double>

A 2D vector: an ordered pair of numbers (x, y).

Vectors are mutable and not thread-safe; confine an instance (and any object graph holding it) to a single thread, exactly as in three.js. Most methods mutate this and return it for chaining.

Iterating a vector yields its components (x, y) in order.

Constructors

Link copied to clipboard
constructor(x: Double = 0.0, y: Double = 0.0)

Properties

Link copied to clipboard

Alias for y.

Link copied to clipboard

Alias for x.

Link copied to clipboard
var x: Double

The x value of this vector.

Link copied to clipboard
var y: Double

The y value of this vector.

Functions

Link copied to clipboard

Adds v to this instance.

Link copied to clipboard

Adds the scalar s to all components.

Link copied to clipboard

Adds v scaled by s to this instance.

Link copied to clipboard

Sets this vector to a + b.

Link copied to clipboard
fun angle(): Double

Returns the angle of this vector relative to the positive x-axis, in radians, in [0, 2*PI).

Link copied to clipboard

Returns the angle between this vector and v, in radians.

Link copied to clipboard

Multiplies this vector by the 3x3 matrix m.

Link copied to clipboard
fun ceil(): Vector2

Rounds each component up to the nearest integer.

Link copied to clipboard
fun clamp(min: Vector2, max: Vector2): Vector2

Clamps each component into the range [min, max] (assumed component-wise min <= max).

Link copied to clipboard
fun clampLength(min: Double, max: Double): Vector2

Clamps this vector's length into [min, max].

Link copied to clipboard
fun clampScalar(minVal: Double, maxVal: Double): Vector2

Clamps each component into the scalar range [minVal, maxVal].

Link copied to clipboard
fun clone(): Vector2

Returns a new vector with the same components.

Link copied to clipboard

Copies the components of v into this instance.

Link copied to clipboard

Returns the (scalar) cross product of this vector and v.

Link copied to clipboard

Returns the distance from this vector to v.

Link copied to clipboard

Returns the squared distance from this vector to v.

Link copied to clipboard

Divides this instance component-wise by v.

Link copied to clipboard

Divides all components by scalar.

Link copied to clipboard
fun dot(v: Vector2): Double

Returns the dot product of this vector and v.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Structural equality: true when other is a Vector2 with equal components (component-wise ==, so NaN != NaN).

Link copied to clipboard
fun floor(): Vector2

Rounds each component down to the nearest integer.

Link copied to clipboard
fun fromArray(array: DoubleArray, offset: Int = 0): Vector2

Sets x = array[offset], y = array[offset + 1].

Link copied to clipboard
fun fromBufferAttribute(attribute: AttributeLike, index: Int): Vector2

Sets this vector's components from attribute at index.

Link copied to clipboard
fun getComponent(index: Int): Double

Returns the component at index (0 = x, 1 = y).

Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
open operator override fun iterator(): Iterator<Double>
Link copied to clipboard
fun length(): Double

Returns the Euclidean length of this vector.

Link copied to clipboard

Returns the squared Euclidean length of this vector.

Link copied to clipboard
fun lerp(v: Vector2, alpha: Double): Vector2

Linearly interpolates from this vector toward v by alpha.

Link copied to clipboard
fun lerpVectors(v1: Vector2, v2: Vector2, alpha: Double): Vector2

Sets this vector to the linear interpolation of v1 and v2 by alpha.

Link copied to clipboard

Returns the Manhattan distance from this vector to v.

Link copied to clipboard

Returns the Manhattan length of this vector.

Link copied to clipboard

Sets each component to the maximum of itself and v's corresponding component.

Link copied to clipboard

Sets each component to the minimum of itself and v's corresponding component.

Link copied to clipboard

Multiplies this instance component-wise by v.

Link copied to clipboard

Multiplies all components by scalar.

Link copied to clipboard

Negates each component.

Link copied to clipboard

Normalizes this vector to unit length (leaves a zero vector unchanged).

Link copied to clipboard

Sets each component to a pseudo-random value in [0, 1).

Link copied to clipboard
fun rotateAround(center: Vector2, angle: Double): Vector2

Rotates this vector around center by angle radians.

Link copied to clipboard
fun round(): Vector2

Rounds each component to the nearest integer (halves toward +infinity, as JS Math.round).

Link copied to clipboard

Rounds each component toward zero.

Link copied to clipboard
fun set(x: Double, y: Double): Vector2

Sets the vector components.

Link copied to clipboard
fun setComponent(index: Int, value: Double): Vector2

Sets the component at index (0 = x, 1 = y) to value.

Link copied to clipboard
fun setLength(length: Double): Vector2

Sets this vector's length to length, preserving direction.

Link copied to clipboard
fun setScalar(scalar: Double): Vector2

Sets all vector components to scalar.

Link copied to clipboard

Sets the vector's x component.

Link copied to clipboard

Sets the vector's y component.

Link copied to clipboard

Subtracts v from this instance.

Link copied to clipboard

Subtracts the scalar s from all components.

Link copied to clipboard

Sets this vector to a - b.

Link copied to clipboard
fun toArray(array: MutableList<Double> = mutableListOf(), offset: Int = 0): MutableList<Double>

Writes this vector's components into array at offset, growing the list as needed, and returns it.

Link copied to clipboard
open override fun toString(): String