From 8dcabebb11b92064a52e9f0da2f765e2cbddca83 Mon Sep 17 00:00:00 2001 From: Hugh Kennedy Date: Sun, 4 Aug 2013 15:49:24 +1000 Subject: [PATCH] first commit --- 2d/add.js | 26 ++++++ 2d/copy.js | 22 ++++++ 2d/cross.js | 24 ++++++ 2d/dist.js | 23 ++++++ 2d/div.js | 22 ++++++ 2d/dot.js | 21 +++++ 2d/heading.js | 21 +++++ 2d/index.js | 19 +++++ 2d/lerp.js | 24 ++++++ 2d/limit.js | 29 +++++++ 2d/mag.js | 20 +++++ 2d/mult.js | 22 ++++++ 2d/normalize.js | 26 ++++++ 2d/sub.js | 26 ++++++ 3d/index.js | 8 ++ LICENSE.md | 18 +++++ README.md | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 47 +++++++++++ package.json | 28 +++++++ 19 files changed, 631 insertions(+) create mode 100644 2d/add.js create mode 100644 2d/copy.js create mode 100644 2d/cross.js create mode 100644 2d/dist.js create mode 100644 2d/div.js create mode 100644 2d/dot.js create mode 100644 2d/heading.js create mode 100644 2d/index.js create mode 100644 2d/lerp.js create mode 100644 2d/limit.js create mode 100644 2d/mag.js create mode 100644 2d/mult.js create mode 100644 2d/normalize.js create mode 100644 2d/sub.js create mode 100644 3d/index.js create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json diff --git a/2d/add.js b/2d/add.js new file mode 100644 index 0000000..af6250e --- /dev/null +++ b/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 +} diff --git a/2d/copy.js b/2d/copy.js new file mode 100644 index 0000000..ddf2f44 --- /dev/null +++ b/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]] +} diff --git a/2d/cross.js b/2d/cross.js new file mode 100644 index 0000000..ed2ce9b --- /dev/null +++ b/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] + ) +} diff --git a/2d/dist.js b/2d/dist.js new file mode 100644 index 0000000..3ef6110 --- /dev/null +++ b/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) +} diff --git a/2d/div.js b/2d/div.js new file mode 100644 index 0000000..2b5f1cb --- /dev/null +++ b/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 +} diff --git a/2d/dot.js b/2d/dot.js new file mode 100644 index 0000000..38f941a --- /dev/null +++ b/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] +} diff --git a/2d/heading.js b/2d/heading.js new file mode 100644 index 0000000..78ed8cb --- /dev/null +++ b/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]) +} diff --git a/2d/index.js b/2d/index.js new file mode 100644 index 0000000..299c556 --- /dev/null +++ b/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') +} diff --git a/2d/lerp.js b/2d/lerp.js new file mode 100644 index 0000000..6df4086 --- /dev/null +++ b/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 +} diff --git a/2d/limit.js b/2d/limit.js new file mode 100644 index 0000000..e7676d2 --- /dev/null +++ b/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 +} diff --git a/2d/mag.js b/2d/mag.js new file mode 100644 index 0000000..cebf2ff --- /dev/null +++ b/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]) +} diff --git a/2d/mult.js b/2d/mult.js new file mode 100644 index 0000000..6d91cbc --- /dev/null +++ b/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 +} diff --git a/2d/normalize.js b/2d/normalize.js new file mode 100644 index 0000000..5d09d6c --- /dev/null +++ b/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 +} diff --git a/2d/sub.js b/2d/sub.js new file mode 100644 index 0000000..62a94d6 --- /dev/null +++ b/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 +} diff --git a/3d/index.js b/3d/index.js new file mode 100644 index 0000000..244759f --- /dev/null +++ b/3d/index.js @@ -0,0 +1,8 @@ +/** +## 3D Vector API + +Working on this one... +**/ +module.exports = { + // soon... +} diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..ee27ba4 --- /dev/null +++ b/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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c744c2 --- /dev/null +++ b/README.md @@ -0,0 +1,205 @@ +# vectors + +A grab bag of vector utility functions for 2D and 3D vectors that +operate on plain arrays. Much like [cog](http://ghub.io/cog), +each method can be required individually to limit the amount of +bloat you get from using the module on the client with +[browserify](http://browserify.org). + +## Installation + +``` bash +npm install vectors +``` + +## Usage + +Each method is requireable from `vectors/${dims}d/${method}`, +e.g.: + +``` javascript +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: + +``` javascript +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 +``` + +## 3D Vector API + +Working on this one... + +## 2D Vector API + +### `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] +``` + +### `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] +``` + +### `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 +``` + +### `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 +``` + +### `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] +``` + +### `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 +``` + +### `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 +``` + +### `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] +``` + +### `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] +``` + +### `mag(vec)` + +Returns the magnitude of the vector: + +``` javascript +var mag = require('vectors/2d/mag') +var spd = [2, 4] + +mag(spd) === 4.47213595499958 +``` + +### `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] +``` + +### `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] +``` + +### `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] +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..eb2d2ab --- /dev/null +++ b/index.js @@ -0,0 +1,47 @@ +/** + +# vectors + +A grab bag of vector utility functions for 2D and 3D vectors that +operate on plain arrays. Much like [cog](http://ghub.io/cog), +each method can be required individually to limit the amount of +bloat you get from using the module on the client with +[browserify](http://browserify.org). + +## Installation + +``` bash +npm install vectors +``` + +## Usage + +Each method is requireable from `vectors/${dims}d/${method}`, +e.g.: + +``` javascript +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: + +``` javascript +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 +``` + +**/ + +module.exports = { + vec2: require('./2d'), + vec3: require('./3d') +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f4bd988 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "vector-2d", + "version": "0.0.0", + "description": "A grab bag of vector utility functions for 2D and 3D vectors that operate on plain arrays", + "main": "index.js", + "scripts": { + "prepublish": "npm run readme", + "readme": "sourcecat | emu > README.md" + }, + "repository": { + "type": "git", + "url": "git://github.com/hughsk/vectors.git" + }, + "author": "Hugh Kennedy ", + "license": "MIT", + "dependencies": {}, + "devDependencies": { + "emu": "0.0.2", + "sourcecat": "~0.1.0" + }, + "readmeFilename": "README.md", + "keywords": [ + "vectors", + "math", + "utility", + "browserify" + ] +}