vectors

A grab bag of vector utility functions for 2D and 3D vectors that operate on plain arrays

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.

npm install vectors

Each method is requireable from vectors/${dims}d/${method}, e.g.:

var mag2d = require('vectors/2d/mag')
var add2d = require('vectors/2d/add')
var copy2d = require('vectors/2d/copy')

Each method takes a vec vector, which if returning a new vector will almost always do so by modifying it directly:

var spd = [+1, 0]
var acc = [-1, 0]
var cop = copy2d(spd)
 
mag2d(spd)      // 1 
add2d(spd, acc) // spd === [0, 0] 
mag2d(spd)      // 0 
mag2d(cop)      // 1 

Working on this one...

Adds the other vector to vec:

var add = require('vectors/2d/add')
var pos = [0, 0]
var spd = [1, 1.5]
 
add(pos, spd)
add(pos, spd)
 
console.log(pos) // [2, 3] 

Returns a copy of the vector vec:

var copy = require('vectors/2d/copy')
var spd = [5, 5]
 
var cop = copy(spd)
mult(spd, 100) === [100, 100]
cop === [5, 5]

Returns the cross product of vectors vec and other:

var cross = require('vectors/2d/cross')
var a = [1, 2]
var b = [8, 4]
 
cross(a, b) === -12

Returns the distance between vectors vec and other:

var dist = require('vectors/2d/dist')
var pos1 = [2, 4]
var pos2 = [4, 4]
 
dist(pos1, pos2) === 2

Divides the vector vec by a scalar value:

var div = require('vectors/2d/div')
var spd = [5, 5]
 
div(spd, 2) === [2.5, 2.5]

Returns the dot product of vectors vec and other:

var dot = require('vectors/2d/dot')
var vecA = [15, 5]
var vecB = [10, 8]
 
dot(vecA, vecB) === 190

Mutliplies the vector vec by a scalar value:

var heading = require('vectors/2d/heading')
var a = [5, 0]
var b = [0, 5]
 
heading(a, b) * 180 / Math.PI === 45 // degrees 

Set vec to the linear interpolation between vectors start and finish:

var lerp = require('vectors/2d/lerp')
var start = [0, 0]
var finish = [100, 100]
 
lerp([], start, finish, 0.75) === [75, 75]

Limits the vector vec to a magintude of scalar units.

var limit = require('vectors/2d/limit')
 
limit([3, 0], 2)  === [2, 0]
limit([3, 4], 1)  === [0.6, 0.8]
limit([5, 5], 24) === [5, 5]

Returns the magnitude of the vector:

var mag = require('vectors/2d/mag')
var spd = [2, 4]
 
mag(spd) === 4.47213595499958

Mutliplies the vector vec by a scalar value:

var mult = require('vectors/2d/mult')
var spd = [5, 5]
 
mult(spd, 2) === [10, 10]

Normalizes the vector (i.e. scales it to make its distance 1 unit).

var normalize = require('vectors/2d/normalize')
 
normalize([3, 0])  === [1, 0]
normalize([4, 3])  === [0.8, 0.6]

Subtracts the other vector from vec:

var sub = require('vectors/2d/sub')
var pos = [0, 0]
var spd = [1, 1.5]
 
sub(pos, spd)
sub(pos, spd)
 
console.log(pos) // [-2, -3]