Home Reference Source Test

Function

Static Public Summary
public

add(to: number[], from: number[], target: number[]): number[]

Component-wise addition of two arrays/vectors.

public

addAll(vectors: number[][], target: number[]): number[]

Component-wise addition of a set of arrays/vectors.

public

addScaled(to: number[], from: number[], factor: number, target: number[]): number[]

Component-wise addition of one vector with a scaled version of another vector.

public

angle(vector: number[], axis: number): number

Find the axis aligned angle of a 3d vector

public

angle2(vector: number[]): number

Find the axis aligned angle of a 2d vector

public

clamp(arg: number | number[], min: number, max: number, target: number[]): number | number[]

Clamps the argument according to min and max.

public

clampArray(arr: number[], min: number, max: number, target: number[]): number[]

Clamps each value in array according to min and max.

public

clampValue(value: number, min: number, max: number): number

Clamps the value to min or max if value is less than min or greater than max

public

cross(v1: number[], v2: number[], target: number[]): number[]

Find the cross product between two 3d vectors

public

cross2(v1: number[], v2: number[]): number

Find the psudo cross product between two 2d vectors

public

Convert radians to degrees

public

descr(from: number[], to: number[], target: number[]): {vector: number[], sqr: number, distance: number, unit: number[]}

Describes relationships between two points.

public

dir(from: number[], to: number[], target: number[]): number[]

Get a unit vector between two points/coordinates.

public

dist(p1: number[], p2: number[]): number

Calculate the distance between two points/coordinates.

public

dot(v1: number[], v2: number[]): number

Calculate the dot product between two vectors

public

flattenList(arg: number[], flattend: number[], max: number): *

Recursivly flattens a list of arguments to a 1 dimensional array

public

isNullVec(v: number[], epsilon: number): *

Test if a vector is a null vector

public

lerp(a: number, b: number, t: number): number

Linear interpolation between two numbers

public

mat(arr: ...number, cols: number, rowsFirst: boolean): Matrix

Factory function for creating and assigning a matrix from an array.

public

mat2(args: ...number): Matrix

Factory function for creating and assigning a 2x2 matrix.

public

mat3(args: ...number): Matrix

Factory function for creating and assigning a 3x3 matrix.

public

mat4(args: ...number): Matrix

Factory function for creating and assigning a 4x4 matrix.

public

mix(a: number[], b: number[], t: number | number[], target: number[]): number[]

Mix (interpolate) numbers or arrays (similar to glsl implementation).

public

norm(vector: number[], target: number[]): number[]

Normalizes an array/vector

public

Normalise an angle to be between -PI to +PI

public

nvec(dim: number, values: Array<number>): Vector

Create a vector of specific dimension and optionally set its values.

public

orth2(vector: number[], target: number[]): number[]

Get a unit vector that is perpendicular to the input vector.

public

Convert degrees to radians

public

round(v: number | number[], digits: number): number

Rounds a number to the specific number of digits.

public

rowsToColumns(arr: number[], cols: number, target: number[]): *

Transpose a single dimensional array, representing 2d data, from rows first to columns first.

public

scalar(vector: number[]): number

Computes the scalar value (length) of a vector

public

scale(arr: number[], factor: number, target: number[]): number[]

Component-wise scaling of an array or vector

public

seq(from: number | number[], to: number | number[], steps: number, start: number, end: number): *

Generates a list of interpolated values between from and to, where the number of elements returned are controlled by the steps argument.

public

seqI(steps: number): *

Generates a list of interpolated values between 0 and 1, where the number of elements returned are controlled by the steps argument.

public

smoothstep(edge0: number, edge1: number, x: number): *

Implementation of glsl smoothstep function

public

step(edge: number | number[], x: number | number[], target: number[]): number | number[]

Implementation of glsl step function.

public

stepArray(edges: number[], x: number | number[], target: number[]): number[]

As glsl step function for multiple numeric values

public

As glsl step function for single numeric values

public

sub(from: number[], vector: number[], target: number[]): number[]

Component-wise subtraction of two arrays/vectors.

public

subAll(from: number[], vectors: number[][], target: number[]): number[]

Component-wise subtraction of array/vector and all elements in vectors.

public

subScaled(from: number[], vector: number[], factor: number, target: number[]): number[]

Component-wise addition of one vector with a scaled version of another vector.

public

sumsqr(arr: number[]): number

Computes the sum of squares

public

triple(v1: number[], v2: number[], v3: number[]): number

Get the triple product between three 3d vectors

public

vec(from: number[], to: number[], target: number[]): number[]

Create a vector from two points.

public

vec2(args: ...number): Vector

Factory function for creating and assigning a 2d vector.

public

vec3(args: ...number): Vector

Factory function for creating and assigning a 3d vector.

public

vec4(args: ...number): Vector

Factory function for creating and assigning a 4d vector.

Static Public

public add(to: number[], from: number[], target: number[]): number[] source

Component-wise addition of two arrays/vectors. If target is NOT specified, the first argument will be mutated. Target will be overwritten and NOT included in the sum.

Params:

NameTypeAttributeDescription
to number[]

left operand

from number[]

right operand

target number[]

optional array/vector to store the result

Return:

number[]

array/vector

public addAll(vectors: number[][], target: number[]): number[] source

Component-wise addition of a set of arrays/vectors. If target is NOT specified, the first element in the set will be mutated. Target (if it has valid values) WILL be included in the sum.

Params:

NameTypeAttributeDescription
vectors number[][]

Array of equally length arrays/vectors (to be added)

target number[]

optional array/vector to add into

Return:

number[]

array/vector

public addScaled(to: number[], from: number[], factor: number, target: number[]): number[] source

Component-wise addition of one vector with a scaled version of another vector. If target is NOT specified, the first argument will be mutated. Target will be overwritten and NOT included in the sum.

Params:

NameTypeAttributeDescription
to number[]

left operand

from number[]

right operand

factor number

scaling factor to apply to from-vector

target number[]

optional array/vector to store the result

Return:

number[]

array/vector

public angle(vector: number[], axis: number): number source

Find the axis aligned angle of a 3d vector

Params:

NameTypeAttributeDescription
vector number[]
axis number

which axis to measure from X=0, Y=1, Z=2 (defaults to 0)

Return:

number

angle in radians

public angle2(vector: number[]): number source

Find the axis aligned angle of a 2d vector

Params:

NameTypeAttributeDescription
vector number[]

Return:

number

angle in radians

public clamp(arg: number | number[], min: number, max: number, target: number[]): number | number[] source

Clamps the argument according to min and max. Arg can be either a numeric value or an array of numeric values.

Params:

NameTypeAttributeDescription
arg number | number[]

value or array of values to clamp

min number

minimum value

max number

maximum value

target number[]

optional array/vector to store the result

Return:

number | number[]

clamped version of arg

public clampArray(arr: number[], min: number, max: number, target: number[]): number[] source

Clamps each value in array according to min and max.

Params:

NameTypeAttributeDescription
arr number[]

values to clamp

min number

minimum value

max number

maximum value

target number[]

optional array/vector to store the result

Return:

number[]

array of clamped values

public clampValue(value: number, min: number, max: number): number source

Clamps the value to min or max if value is less than min or greater than max

Params:

NameTypeAttributeDescription
value number

value to clamp

min number

minimum value

max number

maximum value

Return:

number

clamped value

public cross(v1: number[], v2: number[], target: number[]): number[] source

Find the cross product between two 3d vectors

Params:

NameTypeAttributeDescription
v1 number[]

left hand operand (3d vector)

v2 number[]

right hand operand (3d vector)

target number[]

optional array/vector to store the resulting vector

Return:

number[]

the cross product vector (normal)

public cross2(v1: number[], v2: number[]): number source

Find the psudo cross product between two 2d vectors

Params:

NameTypeAttributeDescription
v1 number[]

left hand operand (2d vector)

v2 number[]

right hand operand (2d vector)

Return:

number

signed area of the parallellogram defined by v1 and v2

public deg(r: number): number source

Convert radians to degrees

Params:

NameTypeAttributeDescription
r number

radians

Return:

number

degrees

public descr(from: number[], to: number[], target: number[]): {vector: number[], sqr: number, distance: number, unit: number[]} source

Describes relationships between two points.

Params:

NameTypeAttributeDescription
from number[]

start coordinates

to number[]

end coordinates

target number[]

optional array/vector to store the result

Return:

{vector: number[], sqr: number, distance: number, unit: number[]}

public dir(from: number[], to: number[], target: number[]): number[] source

Get a unit vector between two points/coordinates. This function does not mutate any arguments, but a target may still be used to control the return type or to avoid creating additional arrays.

Params:

NameTypeAttributeDescription
from number[]

start coordinates

to number[]

end coordinates

target number[]

optional array/vector to store the resulting vector

Return:

number[]

unit vector between from and to

public dist(p1: number[], p2: number[]): number source

Calculate the distance between two points/coordinates.

Params:

NameTypeAttributeDescription
p1 number[]

point/coordinates

p2 number[]

point/coordinates

Return:

number

distance

public dot(v1: number[], v2: number[]): number source

Calculate the dot product between two vectors

Params:

NameTypeAttributeDescription
v1 number[]

left hand operand

v2 number[]

right hand operand

Return:

number

the dot product

public flattenList(arg: number[], flattend: number[], max: number): * source

import {flattenList} from '@kjerandp/linear-algebra/src/utils.js'

Recursivly flattens a list of arguments to a 1 dimensional array

Params:

NameTypeAttributeDescription
arg number[]

Arguments to be flattended

flattend number[]

Resulting array of values

max number

Max returned values (default is 0 for all)

Return:

*

public isNullVec(v: number[], epsilon: number): * source

Test if a vector is a null vector

Params:

NameTypeAttributeDescription
v number[]

vector to test

epsilon number

optional epsilon value

Return:

*

public lerp(a: number, b: number, t: number): number source

Linear interpolation between two numbers

Params:

NameTypeAttributeDescription
a number

interpolate from

b number

interpolate to

t number

time 0 = a, 1 = b

Return:

number

the interpolated value

public mat(arr: ...number, cols: number, rowsFirst: boolean): Matrix source

Factory function for creating and assigning a matrix from an array. The array must be 1 dimensional, and will be split into rows and columns based on the specified cols parameter. If array length divided by cols doesn't add up to a whole number, then only floor(args.length / cols) values will be read from the input array.

Initial values are passed in row-first order by default, but can optionally be passed in columns-first by setting the rowFirst argument to false.

Params:

NameTypeAttributeDescription
arr ...number

initial values

cols number

number of columns to split the array into

rowsFirst boolean

read arr in rows-first or column-first order

Return:

Matrix

public mat2(args: ...number): Matrix source

Factory function for creating and assigning a 2x2 matrix. Initial values are passed in row-first order.

Params:

NameTypeAttributeDescription
args ...number

initial values

Return:

Matrix

public mat3(args: ...number): Matrix source

Factory function for creating and assigning a 3x3 matrix. Initial values are passed in row-first order.

Params:

NameTypeAttributeDescription
args ...number

initial values

Return:

Matrix

public mat4(args: ...number): Matrix source

Factory function for creating and assigning a 4x4 matrix. Initial values are passed in row-first order.

Params:

NameTypeAttributeDescription
args ...number

initial values

Return:

Matrix

public mix(a: number[], b: number[], t: number | number[], target: number[]): number[] source

Mix (interpolate) numbers or arrays (similar to glsl implementation). Works on both vectors and matrices, since they are both arrays.

Params:

NameTypeAttributeDescription
a number[]

interpolate from

b number[]

interpolate to

t number | number[]

time 0 = a, 1 = b

target number[]

optional array/vector to store the result

Return:

number[]

interpolated array/vector/matrix

public norm(vector: number[], target: number[]): number[] source

Normalizes an array/vector

Params:

NameTypeAttributeDescription
vector number[]

array/vector to notmalize

target number[]

optional array/vector to store the result

Return:

number[]

normalized array/vector

public nrad(r: number): number source

Normalise an angle to be between -PI to +PI

Params:

NameTypeAttributeDescription
r number

radians

Return:

number

normalised angle

public nvec(dim: number, values: Array<number>): Vector source

Create a vector of specific dimension and optionally set its values. If values contains only one element, the all components will be set to this value. Elements in values exceeding the specified dimension will be neglected.

Params:

NameTypeAttributeDescription
dim number

the number of components

values Array<number>

optional initial values

Return:

Vector

public orth2(vector: number[], target: number[]): number[] source

Get a unit vector that is perpendicular to the input vector. Only for 2d vectors!

Params:

NameTypeAttributeDescription
vector number[]

2d vector

target number[]

optional array/vector to store the resulting vector

Return:

number[]

normalized, perpendicular vector

public rad(d: number): number source

Convert degrees to radians

Params:

NameTypeAttributeDescription
d number

degrees

Return:

number

radians

public round(v: number | number[], digits: number): number source

Rounds a number to the specific number of digits. Works with either a single number or an array of numbers, which means it can be used with vectors and matrices as well.

Params:

NameTypeAttributeDescription
v number | number[]

value to round

digits number

number of digits to round to

Return:

number

rounded value

public rowsToColumns(arr: number[], cols: number, target: number[]): * source

import {rowsToColumns} from '@kjerandp/linear-algebra/src/utils.js'

Transpose a single dimensional array, representing 2d data, from rows first to columns first.

Params:

NameTypeAttributeDescription
arr number[]

Array to transpose

cols number

Number of columns in source array

target number[]

If omitted, result will be returned as a new array

Return:

*

public scalar(vector: number[]): number source

Computes the scalar value (length) of a vector

Params:

NameTypeAttributeDescription
vector number[]

array/vector to compute

Return:

number

public scale(arr: number[], factor: number, target: number[]): number[] source

Component-wise scaling of an array or vector

Params:

NameTypeAttributeDescription
arr number[]

array/vector to scale

factor number

scaling factor

target number[]

optional array/vector to store the result

Return:

number[]

scaled array/vector

public seq(from: number | number[], to: number | number[], steps: number, start: number, end: number): * source

Generates a list of interpolated values between from and to, where the number of elements returned are controlled by the steps argument.

Params:

NameTypeAttributeDescription
from number | number[]

value to interpolate from

to number | number[]

value to interpolate to

steps number

interpolation steps

start number

start time of interpolation [0-1]

end number

end time of interpolation [0-1]

Return:

*

public seqI(steps: number): * source

Generates a list of interpolated values between 0 and 1, where the number of elements returned are controlled by the steps argument.

Params:

NameTypeAttributeDescription
steps number

interpolation steps

Return:

*

public smoothstep(edge0: number, edge1: number, x: number): * source

Implementation of glsl smoothstep function

Params:

NameTypeAttributeDescription
edge0 number
edge1 number
x number

threshold

Return:

*

public step(edge: number | number[], x: number | number[], target: number[]): number | number[] source

Implementation of glsl step function. Returns 0 if an edge is less than the threshold x, otherwise 1

Params:

NameTypeAttributeDescription
edge number | number[]

number/array to test

x number | number[]

threshold(s)

target number[]

optional array/vector to store the result (if edge is array)

Return:

number | number[]

public stepArray(edges: number[], x: number | number[], target: number[]): number[] source

As glsl step function for multiple numeric values

Params:

NameTypeAttributeDescription
edges number[]

values to test

x number | number[]

threshold values

target number[]

optional array/vector to store the result

Return:

number[]

results for each value in edges

public stepValue(edge: number, x: number): number source

As glsl step function for single numeric values

Params:

NameTypeAttributeDescription
edge number

value to test

x number

threshold value

Return:

number

returns 0 or 1

public sub(from: number[], vector: number[], target: number[]): number[] source

Component-wise subtraction of two arrays/vectors. If target is NOT specified, the first argument will be mutated. Target will be overwritten and NOT included in the sum.

Params:

NameTypeAttributeDescription
from number[]

left operand

vector number[]

right operand

target number[]

optional array/vector to store the result

Return:

number[]

array/vector

public subAll(from: number[], vectors: number[][], target: number[]): number[] source

Component-wise subtraction of array/vector and all elements in vectors. If target is NOT specified, the first argument will be mutated. Target will be overwritten and NOT included in the sum.

Params:

NameTypeAttributeDescription
from number[]

left operand

vectors number[][]

right operand

target number[]

optional array/vector to store the result

Return:

number[]

array/vector

public subScaled(from: number[], vector: number[], factor: number, target: number[]): number[] source

Component-wise addition of one vector with a scaled version of another vector. If target is NOT specified, the first argument will be mutated. Target will be overwritten and NOT included in the sum.

Params:

NameTypeAttributeDescription
from number[]

left operand

vector number[]

right operand

factor number

scaling factor to apply to vector that will be subtracted

target number[]

optional array/vector to store the result

Return:

number[]

array/vector

public sumsqr(arr: number[]): number source

Computes the sum of squares

Params:

NameTypeAttributeDescription
arr number[]

array/vector to compute

Return:

number

public triple(v1: number[], v2: number[], v3: number[]): number source

Get the triple product between three 3d vectors

Params:

NameTypeAttributeDescription
v1 number[]

left hand operand for the dot product (3d vector)

v2 number[]

left hand operand for the cross product (3d vector)

v3 number[]

right hand operand for the cross product (3d vector)

Return:

number

triple product

public vec(from: number[], to: number[], target: number[]): number[] source

Create a vector from two points. This function will NOT mutate any arguments unless 'target' is the same as 'from'.

Params:

NameTypeAttributeDescription
from number[]

start coordinates

to number[]

end coordinates

target number[]

optional array/vector to store the resulting vector

Return:

number[]

vector

public vec2(args: ...number): Vector source

Factory function for creating and assigning a 2d vector. If a single argument is passed, then all components will be assigned with its value. Missing initial values will be filled with zeros.

Params:

NameTypeAttributeDescription
args ...number

initial values

Return:

Vector

public vec3(args: ...number): Vector source

Factory function for creating and assigning a 3d vector. If a single argument is passed, then all components will be assigned with its value. Missing initial values will be filled with zeros.

Params:

NameTypeAttributeDescription
args ...number

initial values

Return:

Vector

public vec4(args: ...number): Vector source

Factory function for creating and assigning a 4d vector. If a single argument is passed, then all components will be assigned with its value. Missing initial values will be filled with zeros.

Params:

NameTypeAttributeDescription
args ...number

initial values

Return:

Vector