vectors
A grab bag of vector utility functions for 2D and 3D vectors that operate on plain arrays. Much like cog, each method can be required individually to limit the amount of bloat you get from using the module on the client with browserify.
Installation
npm install vectors
Contributors
Usage
Each method is requireable from vectors/${method}
,
followed by calling the returned generator function
with the number of dimensions you want your vectors to be.
e.g.:
var mag = 2var add = 3var sub = 3
If you want something totally generic, you can assume
in most cases that appending -nd
to your require
will return a less performant but more flexible function:
var mag =var add =var sub =
Most of the methods in this module support vectors of arbitrary dimensions, but the ones that don't will throw an error to let you know.
Each method takes a vec
vector, which if returning a new
vector will almost always do so by modifying it directly:
var spd = +1 0var acc = -1 0var cop =// 1// spd === [0, 0]// 0// 1
add(vec, other[, ...])
Adds the other
vector to vec
:
var add = 2var pos = 0 0var spd = 1 15console // [2, 3]
Or add a scalar to the entire array:
var res =console // [7, 7, 7]
You can disable this by passing scalars: false
to
the generator function for faster results:
var add = 2 scalars: false
copy(vec)
Returns a copy of the vector vec
:
var copy = 2var spd = 5 5var cop ==== 100 100cop === 5 5
cross(vec, other)
Returns the cross product of vectors vec
and other
:
var cross = 2var a = 1 2var b = 8 4=== -12
This method only works in 2 and 3 dimensions.
dist(vec, other)
Returns the distance between vectors vec
and other
:
var dist = 2var pos1 = 2 4var pos2 = 4 4=== 2
div(vec, other[, ...])
Divides the vector vec
by a other
value:
var div = 2var spd = 5 5=== 25 25
Or divide multiple arrays from each other:
var res =console // [3, 3, 3]
You can disable this by passing vectors: false
to
the generator function for faster results:
var sub = 2 vectors: false
dot(vec, other)
Returns the dot product of vectors vec
and other
:
var dot = 2var vecA = 15 5var vecB = 10 8=== 190
heading(vec, other)
Mutliplies the vector vec
by a scalar
value:
var heading = 2var a = 5 0var b = 0 5* 180 / MathPI === 45 // degrees
lerp(vec, start, finish, scalar)
Set vec
to the linear interpolation between vectors start
and finish
:
var lerp = 2var start = 0 0var finish = 100 100=== 75 75
limit(vec, scalar)
Limits the vector vec
to a magnitude of scalar
units.
var limit = 2=== 2 0=== 06 08=== 5 5
mag(vec)
Returns the magnitude of the vector:
var mag = 2var spd = 2 4=== 447213595499958
mult(vec, other[, ...])
Mutliplies the vector vec
by a other
value:
var mult = 2var spd = 5 5=== 10 10
Or multiply multiple arrays:
var res =console // [8, 8, 8]
You can disable this by passing vectors: false
to
the generator function for faster results:
var sub = 2 vectors: false
normalize(vec, scalar)
Normalizes the vector (i.e. scales it to make its distance 1 unit).
var normalize = 2=== 1 0=== 08 06
sub(vec, other[, ...])
Subtracts the other
vector from vec
:
var sub = 2var pos = 0 0var spd = 1 15console // [-2, -3]
Or subtract a scalar from the entire array:
var res =console // [3, 2, 1]
You can disable this by passing scalars: false
to
the generator function for faster results:
var sub = 2 scalars: false