Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsk committed Aug 4, 2013
0 parents commit 8dcabeb
Show file tree
Hide file tree
Showing 19 changed files with 631 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 2d/add.js
@@ -0,0 +1,26 @@
/**
### `add(vec, other)`
Adds the `other` vector to `vec`:
``` javascript
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]
```
**/

module.exports = add

function add(vec, other) {
vec[0] += other[0]
vec[1] += other[1]
return vec
}
22 changes: 22 additions & 0 deletions 2d/copy.js
@@ -0,0 +1,22 @@
/**
### `copy(vec)`
Returns a copy of the vector `vec`:
``` javascript
var copy = require('vectors/2d/copy')
var spd = [5, 5]
var cop = copy(spd)
mult(spd, 100) === [100, 100]
cop === [5, 5]
```
**/

module.exports = copy

function copy(vec) {
return [vec[0], vec[1]]
}
24 changes: 24 additions & 0 deletions 2d/cross.js
@@ -0,0 +1,24 @@
/**
### `cross(vec, other)`
Returns the cross product of vectors `vec` and `other`:
``` javascript
var cross = require('vectors/2d/cross')
var a = [1, 2]
var b = [8, 4]
cross(a, b) === -12
```
**/

module.exports = cross

function cross(vec, other) {
return (
vec[0] * other[1] -
vec[1] * other[0]
)
}
23 changes: 23 additions & 0 deletions 2d/dist.js
@@ -0,0 +1,23 @@
/**
### `dist(vec, other)`
Returns the distance between vectors `vec` and `other`:
``` javascript
var dist = require('vectors/2d/dist')
var pos1 = [2, 4]
var pos2 = [4, 4]
dist(pos1, pos2) === 2
```
**/

module.exports = dist

function dist(vec, other) {
var x = other[0] - vec[0]
var y = other[1] - vec[1]
return Math.sqrt(x*x + y*y)
}
22 changes: 22 additions & 0 deletions 2d/div.js
@@ -0,0 +1,22 @@
/**
### `div(vec, scalar)`
Divides the vector `vec` by a `scalar` value:
``` javascript
var div = require('vectors/2d/div')
var spd = [5, 5]
div(spd, 2) === [2.5, 2.5]
```
**/

module.exports = div

function div(vec, scalar) {
vec[0] /= scalar
vec[1] /= scalar
return vec
}
21 changes: 21 additions & 0 deletions 2d/dot.js
@@ -0,0 +1,21 @@
/**
### `dot(vec, other)`
Returns the dot product of vectors `vec` and `other`:
``` javascript
var dot = require('vectors/2d/dot')
var vecA = [15, 5]
var vecB = [10, 8]
dot(vecA, vecB) === 190
```
**/

module.exports = dot

function dot(vec, other) {
return vec[0] * other[0] + vec[1] * other[1]
}
21 changes: 21 additions & 0 deletions 2d/heading.js
@@ -0,0 +1,21 @@
/**
### `heading(vec, other)`
Mutliplies the vector `vec` by a `scalar` value:
``` javascript
var heading = require('vectors/2d/heading')
var a = [5, 0]
var b = [0, 5]
heading(a, b) * 180 / Math.PI === 45 // degrees
```
**/

module.exports = heading

function heading(vec, other) {
return Math.atan2(vec[1] - other[1], vec[0] - other[0])
}
19 changes: 19 additions & 0 deletions 2d/index.js
@@ -0,0 +1,19 @@
/**
## 2D Vector API
**/

module.exports = {
add: require('./add')
, sub: require('./sub')
, mag: require('./mag')
, div: require('./div')
, dot: require('./dot')
, mult: require('./mult')
, dist: require('./dist')
, lerp: require('./lerp')
, copy: require('./copy')
, cross: require('./cross')
, limit: require('./limit')
, heading: require('./heading')
, normalize: require('./normalize')
}
24 changes: 24 additions & 0 deletions 2d/lerp.js
@@ -0,0 +1,24 @@
/**
### `lerp(vec, start, finish, scalar)`
Set `vec` to the linear interpolation between vectors `start`
and `finish`:
``` javascript
var lerp = require('vectors/2d/lerp')
var start = [0, 0]
var finish = [100, 100]
lerp([], start, finish, 0.75) === [75, 75]
```
**/

module.exports = lerp

function lerp(vec, a, b, scalar) {
vec[0] = a[0] + (b[0] - a[0]) * scalar
vec[1] = a[1] + (b[1] - a[1]) * scalar
return vec
}
29 changes: 29 additions & 0 deletions 2d/limit.js
@@ -0,0 +1,29 @@
/**
### `limit(vec, scalar)`
Limits the vector `vec` to a magintude of `scalar` units.
``` javascript
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]
```
**/

module.exports = limit

function limit(vec, scalar) {
var mag = vec[0]*vec[0] + vec[1]*vec[1]

if (mag > scalar*scalar) {
mag = Math.sqrt(mag)
vec[0] = vec[0] * scalar / mag
vec[1] = vec[1] * scalar / mag
}

return vec
}
20 changes: 20 additions & 0 deletions 2d/mag.js
@@ -0,0 +1,20 @@
/**
### `mag(vec)`
Returns the magnitude of the vector:
``` javascript
var mag = require('vectors/2d/mag')
var spd = [2, 4]
mag(spd) === 4.47213595499958
```
**/

module.exports = mag

function mag(vec) {
return Math.sqrt(vec[0]*vec[0] + vec[1]*vec[1])
}
22 changes: 22 additions & 0 deletions 2d/mult.js
@@ -0,0 +1,22 @@
/**
### `mult(vec, scalar)`
Mutliplies the vector `vec` by a `scalar` value:
``` javascript
var mult = require('vectors/2d/mult')
var spd = [5, 5]
mult(spd, 2) === [10, 10]
```
**/

module.exports = mult

function mult(vec, scalar) {
vec[0] *= scalar
vec[1] *= scalar
return vec
}
26 changes: 26 additions & 0 deletions 2d/normalize.js
@@ -0,0 +1,26 @@
/**
### `normalize(vec, scalar)`
Normalizes the vector (i.e. scales it to make its
distance 1 unit).
``` javascript
var normalize = require('vectors/2d/normalize')
normalize([3, 0]) === [1, 0]
normalize([4, 3]) === [0.8, 0.6]
```
**/

module.exports = normalize

function normalize(vec, scalar) {
var mag = Math.sqrt(vec[0]*vec[0] + vec[1]*vec[1])

vec[0] /= mag
vec[1] /= mag

return vec
}
26 changes: 26 additions & 0 deletions 2d/sub.js
@@ -0,0 +1,26 @@
/**
### `sub(vec, other)`
Subtracts the `other` vector from `vec`:
``` javascript
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]
```
**/

module.exports = sub

function sub(vec, other) {
vec[0] -= other[0]
vec[1] -= other[1]
return vec
}
8 changes: 8 additions & 0 deletions 3d/index.js
@@ -0,0 +1,8 @@
/**
## 3D Vector API
Working on this one...
**/
module.exports = {
// soon...
}
18 changes: 18 additions & 0 deletions LICENSE.md
@@ -0,0 +1,18 @@
This software is released under the MIT license:

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit 8dcabeb

Please sign in to comment.