class Primitives · Computations

Vectors

Computations on vectors: arithmetic, length, angles, rotation, projection and normalization.

Static methods

addstatic

static add(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Vector | null): Vector

Computes the sum of two vectors.

v1
The first vector.
v2
The second vector.
result optional
Optional output vector instance.

Returns The sum of v1 and v2.

anglestatic

static angle(direction: Vector | VectorLike, clockwise: Bool | BoolLike, result?: Real | null): Real

Computes the angle of the specified direction relative to the system’s zero direction.

direction
The direction to measure.
clockwise
If true, measures the angle clockwise; otherwise counterclockwise. Clockwise refers to the screen (y pointing down): (0, 1) is π/2 clockwise and 3π/2 counterclockwise.
result optional
Optional output angle instance.

Returns The angle of direction in radians.

angleTostatic

static angleTo(direction: Vector | VectorLike, other: Vector | VectorLike, clockwise: Bool | BoolLike, result?: Real | null): Real

Computes the angle from one direction to another.

direction
The starting direction.
other
The target direction.
clockwise
If true, measures the angle clockwise from direction; otherwise counterclockwise.
result optional
Optional output angle instance.

Returns The angle from direction to other in radians.

clampstatic

static clamp(v: Vector | VectorLike, maxLength: Real | NumberLike, result?: Vector | null): Vector

Clamps a vector's magnitude to a maximum length, preserving its direction.

v
The vector to clamp.
maxLength
The maximum allowed magnitude.
result optional
Optional output vector.

Returns v unchanged if its length is at most maxLength; otherwise a vector in the same direction with magnitude maxLength.

copystatic

static copy(other: Vector | VectorLike, result?: Vector | null): Point

Copies the coordinates of a point into a result point.

other
The source point.
result optional
Optional output point instance.

Returns A point with the coordinates of other.

crossProductstatic

static crossProduct(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Real | null): Real

Computes the 2D cross product (scalar magnitude) of two vectors.

v1
The first vector.
v2
The second vector.
result optional
Optional output scalar.

Returns The cross product v1.x * v2.y - v1.y * v2.x.

directionstatic

static direction(start: Point | VectorLike, end: Point | VectorLike, result?: Vector | null): Vector

Computes the direction from a starting point to an ending point.

start
The starting point.
end
The ending point.
result optional
Optional output direction instance.

Returns The direction from start to end.

distanceBetweenstatic

static distanceBetween(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Real | null): Real

Computes the Euclidean distance between two vectors.

v1
The first vector.
v2
The second vector.
result optional
Optional output scalar.

Returns The length of the vector from v1 to v2.

dotProductstatic

static dotProduct(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Real | null): Real

Computes the dot product of two vectors.

v1
The first vector.
v2
The second vector.
result optional
Optional output scalar.

Returns The scalar dot product of v1 and v2.

fromAnglestatic

static fromAngle(angle: Real | NumberLike, result?: Vector | null): Vector

Creates a direction from a specified angle.

angle
The angle to convert to a direction.
result optional
Optional output direction instance.

Returns The direction at angle.

fromAngleAndLengthstatic

static fromAngleAndLength(angle: Real | NumberLike, length: Real | NumberLike, result?: Vector | null): Vector

Creates a vector from a specified angle and length.

angle
The angle of the vector, measured from the positive X axis.
length
The magnitude of the resulting vector.
result optional
Optional output vector instance.

Returns A vector pointing in the direction of angle with magnitude length.

isZerostatic

static isZero(v: Vector | VectorLike, result?: Bool | null): Bool

Determines whether a vector has zero magnitude.

v
The vector to test.
result optional
Optional output boolean.

Returns true if both components of v are zero; otherwise false.

lenstatic

static len(v: Vector | VectorLike, result?: Real | null): Real

Computes the magnitude (length) of a vector.

v
The vector to measure.
result optional
Optional output scalar.

Returns The length of v.

lerpstatic

static lerp(v1: Vector | VectorLike, v2: Vector | VectorLike, t: Real | NumberLike, result?: Vector | null): Vector

Linearly interpolates between two vectors by the specified factor.

v1
The start vector, returned when t is 0.
v2
The end vector, returned when t is 1.
t
The interpolation factor.
result optional
Optional output vector.

Returns The vector interpolated between v1 and v2 at t.

middlestatic

static middle(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Vector | null): Vector

Computes the midpoint between two vectors.

v1
The first vector.
v2
The second vector.
result optional
Optional output vector.

Returns The point halfway between v1 and v2.

negatestatic

static negate(v: Vector | VectorLike, result?: Vector | null): Vector

Returns the negation of a vector.

v
The vector to negate.
result optional
Optional output vector.

Returns A vector pointing in the opposite direction with the same magnitude as v.

normalizestatic

static normalize(vector: Vector | VectorLike, result?: Vector | null): Vector

Normalizes a vector to unit length. The zero vector has no direction and stays the zero vector.

vector
The vector to normalize.
result optional
Optional output vector.

Returns A vector of length 1 in the direction of vector, or the zero vector.

perpendicularstatic

static perpendicular(v: Vector | VectorLike, result?: Vector | null): Vector

Returns a vector perpendicular to the given vector (rotated 90 degrees counterclockwise).

v
The input vector.
result optional
Optional output vector.

Returns A vector perpendicular to v with the same magnitude.

projectOntostatic

static projectOnto(v: Vector | VectorLike, onto: Vector | VectorLike, result?: Vector | null): Vector

Projects a vector onto another vector.

v
The vector to project.
onto
The vector to project onto.
result optional
Optional output vector.

Returns The component of v in the direction of onto. Returns the zero vector if onto has zero magnitude.

reflectOverstatic

static reflectOver(v: Vector | VectorLike, axis: Vector | VectorLike, result?: Vector | null): Vector

Reflects a vector over an axis vector.

v
The vector to reflect.
axis
The axis to reflect over.
result optional
Optional output vector.

Returns v reflected across axis. Returns v unchanged if axis has zero magnitude.

rotate90static

static rotate90(direction: Vector | VectorLike, clockwise: Bool | BoolLike, result?: Vector | null): Vector

Rotates a direction by 90 degrees.

direction
The direction to rotate.
clockwise
If true, rotates clockwise; otherwise counterclockwise.
result optional
Optional output direction.

Returns The direction rotated by 90 degrees.

rotate90Clockwisestatic

static rotate90Clockwise(direction: Vector | VectorLike, result?: Vector | null): Vector

Rotates a direction 90 degrees clockwise.

direction
The direction to rotate.
result optional
Optional output direction.

Returns The direction rotated 90 degrees clockwise.

rotate90CounterClockwisestatic

static rotate90CounterClockwise(direction: Vector | VectorLike, result?: Vector | null): Vector

Rotates a direction 90 degrees counterclockwise.

direction
The direction to rotate.
result optional
Optional output direction.

Returns The direction rotated 90 degrees counterclockwise.

rotateAtstatic

static rotateAt(vector: Vector | VectorLike, angle: Real | NumberLike, anchor: Vector | VectorLike, result?: Vector | null): Vector

Rotates a vector by a specified angle around an anchor point.

vector
The vector to rotate.
angle
The angle of rotation.
anchor
The point to rotate around.
result optional
Optional output vector.

Returns vector rotated by angle around anchor.

rotateBystatic

static rotateBy(vector: Vector | VectorLike, angle: Real | NumberLike, result?: Vector | null): Vector

Rotates a vector by a specified angle around the origin.

vector
The vector to rotate.
angle
The angle of rotation.
result optional
Optional output vector.

Returns vector rotated by angle around the origin.

scalestatic

static scale(v: Vector | VectorLike, factor: Real | NumberLike, result?: Vector | null): Vector

Scales a vector by a scalar factor.

v
The vector to scale.
factor
The scale factor.
result optional
Optional output vector.

Returns v multiplied by factor.

signedAngleTostatic

static signedAngleTo(direction: Vector | VectorLike, other: Vector | VectorLike, result?: Real | null): Real

The signed angle to turn one direction into another, in (−π, π]: positive clockwise on the screen (y pointing down), negative counterclockwise. E.g. how far and which way a handle was turned.

Unlike angleTo (0 to 2π in a given turning direction) it takes the shorter way, so a small turn either way stays small. The sign matches Arcs.arcAt and rotateBy: Vectors.rotateBy(direction, angle) points the same way as other. A direction of length 0 gives 0.

direction
The direction to turn from.
other
The direction to turn to.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The angle in radians, in (−π, π].

squaredLengthstatic

static squaredLength(v: Vector | VectorLike, result?: Real | null): Real

Computes the squared length of a vector.

v
The vector to measure.
result optional
Optional output scalar.

Returns The squared length of v.

substatic

static sub(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Vector | null): Vector

Computes the difference between two vectors (v1 - v2).

v1
The first vector.
v2
The vector to subtract.
result optional
Optional output vector.

Returns The difference v1 - v2.