Skip to main content

Module: utils/vectors

Functions

applyRotation

โ–ธ applyRotation(base, origin, rotation): vec2[]

Rotates each vector in an array by a rotation angle in radians around an origin point.

Parameters

NameTypeDescription
basevec2[]The vectors to apply the rotation to
originvec2The origin to rotate the vectors around
rotationnumberThe rotation (in radians) to apply to each vector

Returns

vec2[]

The rotated vectors

Defined in

src/utils/vectors.ts:11


applyTranslation

โ–ธ applyTranslation(base, translation): vec2[]

Translates each vector in an array by a given translation vector

Parameters

NameTypeDescription
basevec2[]The vectors to apply the translation to
translationvec2The translation to apply to each vector

Returns

vec2[]

The translated vectors

Defined in

src/utils/vectors.ts:27


cross2D

โ–ธ cross2D(a, b): number

Calculates the cross product of 2 2D vectors and returns the Z value of the resulting 3D vector.

Parameters

NameTypeDescription
avec2The vector to cross
bvec2The vector to cross with a

Returns

number

The Z coordinate of the resultant 3D vector

Defined in

src/utils/vectors.ts:89


cross2DWithScalar

โ–ธ cross2DWithScalar(out, a, scalar): vec2

Calculates the cross product of a with a scalar and stores the value in out

Parameters

NameTypeDescription
outvec2The vector to output to
avec2The vector to cross
scalarnumberThe scalar to cross a with

Returns

vec2

out

Defined in

src/utils/vectors.ts:76


midpoint

โ–ธ midpoint(out, a, b): vec2

Calculate the midpoint between two vectors.

Parameters

NameTypeDescription
outvec2The vector to output the result to
avec2The start vector
bvec2The end vector

Returns

vec2

The resultant vector, out

Defined in

src/utils/vectors.ts:101


tripleProduct

โ–ธ tripleProduct(out, a, b, c): vec2

Calculates the triple product of three 2D vectors.

This is done by:

  • extending each 2D vector into 3D by giving them a Z value of 0.
  • Calculating the cross product of A and B, storing the result as vector Rโ‚. Rโ‚ will be a vector purely in the z axis as the x and y components after the cross product are 0.
  • The cross product of Rโ‚ and C is then calculated, we'll call this Rโ‚‚. Rโ‚‚ will be a vector that is perpendicular to C and pointing in the direction of B.

see This post for an explanation

Parameters

NameTypeDescription
outvec2-
avec2Initial vector
bvec2Vector that the result will be in direction of
cvec2Vector that the result will be perpendicular to

Returns

vec2

Defined in

src/utils/vectors.ts:55


vec2SloppyEquals

โ–ธ vec2SloppyEquals(a, b, xSlop, ySlop): boolean

Determines wether two vectors are roughly equal to each other.

Parameters

NameTypeDescription
avec2The first vector
bvec2The second vector
xSlopnumberThe allowed x slop
ySlopnumberThe allowed y slop

Returns

boolean

Wether the vectors are equal or not

Defined in

src/utils/vectors.ts:116