Skip to content

aninest-root / Vec2

Vec2

A collection of 2D vector math functions and a few other generic scalar operations.

Vectors are represented as {x: number, y: number} and are meant to be immutable, following a functional programming style.

Type Aliases

Type AliasDescription
Vec2A 2D vector.

Variables

VariableDescription
ZERO_VEC2A 2D vector with x and y set to 0.

Functions

FunctionDescription
addVecAdds two vectors together, returning a new vector.
bezierPerforms a bezier interpolation between two vectors by a time value.
clampClamps a value between a minimum and maximum value.
copyDuplicates the vector.
crossCalculates the cross product of two vectors.
distanceToCalculates the distance between two vectors.
distanceTo2Calculates the squared distance between two vectors.
divScalarDivides a vector v by a scalar s immutably.
divVecPerforms component-wise division of v1 / v2 immutably.
dotCalculates the dot product of two vectors.
lerpPerforms a linear interpolation between two vectors by a time value.
lerpFuncLerps between a and b by t.
magCalculates the magnitude of a vector.
magSquaredSquares the magnitude of a vector.
mapVecCalls a function func on each component of a vector, creating a new vector from the result of each function call.
mulScalarMultiplies a vector v by a scalar s immutably.
mulVecDoes component-wise multiplication of two vectors immutably.
newVec2Vec2 Constructor
normalizeReturnes a normalized version of the vector.
rotateRotates a vector by an angle in radians.
rotateAroundRotates a vector around a pivot point by an angle in radians.
subVecSubtracts v2 from v1 immutably.
vecToIterConverts a vector to an array. Useful for spreading into function arguments.