Created
September 23, 2016 20:59
-
-
Save chris--young/99dc396002cf3b901ecef21112432fe9 to your computer and use it in GitHub Desktop.
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
const _3d = {}; | |
_3d.vector = (x = 0, y = 0, z = 0) => [x, y, z]; | |
_3d.matrix = (v0, v1, v2) => _3d.vector(v0, v1, v2); | |
_3d.identity = _3d.matrix([1, 0, 0], [0, 1, 0], [0, 0, 1]); | |
_3d.scale = (v, s) => _3d.vector(v[0] * s, v[1] * s, v[2] * s); | |
_3d.sum = (v0, v1, v2) => _3d.vector(v0[0] + v1[0] + v2[0], v0[1] + v1[1] + v2[1], v0[2] + v1[2] + v2[2]); | |
_3d.transform = (v, m) => _3d.sum(_3d.scale(m[0], v[0]), _3d.scale(m[1], v[1]), _3d.scale(m[2], v[2])); | |
_3d.multiply = (m0, m1) => _3d.matrix(_3d.transform(m1[0], m0), _3d.transform(m1[1], m0), _3d.transform(m1[2], m0)); | |
// TODO: curry _3d.multiply | |
const utils = { | |
scale: (x, y) => _3d.matrix([x, 0, 0], [0, y, 0], [0, 0, 1]), | |
shear: (x, y) => _3d.matrix([1, x, 0], [y, 1, 0], [0, 0, 1]), | |
rotate: r => _3d.matrix([Math.cos(r), -Math.sin(r), 0], [Math.sin(r), Math.cos(r), 0], [0, 0, 1]), | |
translate: (x, y) => _3d.matrix([1, 0, x], [0, 1, y], [0, 0, 1]), | |
flatten: m => m.reduce((a, b) => a.concat(b)), | |
replicate: (n, v) => new Array(n).map(() => v) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment