Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8dcabeb
Showing
19 changed files
with
631 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
## 3D Vector API | ||
Working on this one... | ||
**/ | ||
module.exports = { | ||
// soon... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Oops, something went wrong.