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): VectorComputes the sum of two vectors.
v1- The first vector.
v2- The second vector.
resultoptional- Optional output vector instance.
Returns The sum of v1 and v2.
anglestatic
static angle(direction: Vector | VectorLike, clockwise: Bool | BoolLike, result?: Real | null): RealComputes 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. resultoptional- 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): RealComputes the angle from one direction to another.
direction- The starting direction.
other- The target direction.
clockwise- If
true, measures the angle clockwise fromdirection; otherwise counterclockwise. resultoptional- 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): VectorClamps a vector's magnitude to a maximum length, preserving its direction.
v- The vector to clamp.
maxLength- The maximum allowed magnitude.
resultoptional- 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): PointCopies the coordinates of a point into a result point.
other- The source point.
resultoptional- Optional output point instance.
Returns A point with the coordinates of other.
crossProductstatic
static crossProduct(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Real | null): RealComputes the 2D cross product (scalar magnitude) of two vectors.
v1- The first vector.
v2- The second vector.
resultoptional- 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): VectorComputes the direction from a starting point to an ending point.
start- The starting point.
end- The ending point.
resultoptional- Optional output direction instance.
Returns The direction from start to end.
distanceBetweenstatic
static distanceBetween(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Real | null): RealComputes the Euclidean distance between two vectors.
v1- The first vector.
v2- The second vector.
resultoptional- 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): RealComputes the dot product of two vectors.
v1- The first vector.
v2- The second vector.
resultoptional- Optional output scalar.
Returns The scalar dot product of v1 and v2.
fromAnglestatic
static fromAngle(angle: Real | NumberLike, result?: Vector | null): VectorCreates a direction from a specified angle.
angle- The angle to convert to a direction.
resultoptional- Optional output direction instance.
Returns The direction at angle.
fromAngleAndLengthstatic
static fromAngleAndLength(angle: Real | NumberLike, length: Real | NumberLike, result?: Vector | null): VectorCreates 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.
resultoptional- 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): BoolDetermines whether a vector has zero magnitude.
v- The vector to test.
resultoptional- Optional output boolean.
Returns true if both components of v are zero; otherwise false.
lenstatic
static len(v: Vector | VectorLike, result?: Real | null): RealComputes the magnitude (length) of a vector.
v- The vector to measure.
resultoptional- Optional output scalar.
Returns The length of v.
lerpstatic
static lerp(v1: Vector | VectorLike, v2: Vector | VectorLike, t: Real | NumberLike, result?: Vector | null): VectorLinearly interpolates between two vectors by the specified factor.
v1- The start vector, returned when
tis 0. v2- The end vector, returned when
tis 1. t- The interpolation factor.
resultoptional- 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): VectorComputes the midpoint between two vectors.
v1- The first vector.
v2- The second vector.
resultoptional- Optional output vector.
Returns The point halfway between v1 and v2.
negatestatic
static negate(v: Vector | VectorLike, result?: Vector | null): VectorReturns the negation of a vector.
v- The vector to negate.
resultoptional- 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): VectorNormalizes a vector to unit length. The zero vector has no direction and stays the zero vector.
vector- The vector to normalize.
resultoptional- 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): VectorReturns a vector perpendicular to the given vector (rotated 90 degrees counterclockwise).
v- The input vector.
resultoptional- 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): VectorProjects a vector onto another vector.
v- The vector to project.
onto- The vector to project onto.
resultoptional- 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): VectorReflects a vector over an axis vector.
v- The vector to reflect.
axis- The axis to reflect over.
resultoptional- 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): VectorRotates a direction by 90 degrees.
direction- The direction to rotate.
clockwise- If
true, rotates clockwise; otherwise counterclockwise. resultoptional- Optional output direction.
Returns The direction rotated by 90 degrees.
rotate90Clockwisestatic
static rotate90Clockwise(direction: Vector | VectorLike, result?: Vector | null): VectorRotates a direction 90 degrees clockwise.
direction- The direction to rotate.
resultoptional- Optional output direction.
Returns The direction rotated 90 degrees clockwise.
rotate90CounterClockwisestatic
static rotate90CounterClockwise(direction: Vector | VectorLike, result?: Vector | null): VectorRotates a direction 90 degrees counterclockwise.
direction- The direction to rotate.
resultoptional- 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): VectorRotates 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.
resultoptional- Optional output vector.
Returns vector rotated by angle around anchor.
rotateBystatic
static rotateBy(vector: Vector | VectorLike, angle: Real | NumberLike, result?: Vector | null): VectorRotates a vector by a specified angle around the origin.
vector- The vector to rotate.
angle- The angle of rotation.
resultoptional- Optional output vector.
Returns vector rotated by angle around the origin.
scalestatic
static scale(v: Vector | VectorLike, factor: Real | NumberLike, result?: Vector | null): VectorScales a vector by a scalar factor.
v- The vector to scale.
factor- The scale factor.
resultoptional- Optional output vector.
Returns v multiplied by factor.
signedAngleTostatic
static signedAngleTo(direction: Vector | VectorLike, other: Vector | VectorLike, result?: Real | null): RealThe 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.
resultoptional- Optional
Realto write the result into; it is returned. Without it, a newRealis returned.
Returns The angle in radians, in (−π, π].
squaredLengthstatic
static squaredLength(v: Vector | VectorLike, result?: Real | null): RealComputes the squared length of a vector.
v- The vector to measure.
resultoptional- Optional output scalar.
Returns The squared length of v.
substatic
static sub(v1: Vector | VectorLike, v2: Vector | VectorLike, result?: Vector | null): VectorComputes the difference between two vectors (v1 - v2).
v1- The first vector.
v2- The vector to subtract.
resultoptional- Optional output vector.
Returns The difference v1 - v2.
