diff --git a/packages/math/index.js b/packages/math/index.js index 4d28a9a..e3938cc 100644 --- a/packages/math/index.js +++ b/packages/math/index.js @@ -7,3 +7,6 @@ export {Rectangle}; import * as Vector from './vector'; export {Vector}; + +import * as Vertice from './vertice'; +export {Vertice}; diff --git a/packages/math/vertice.coffee b/packages/math/vertice.coffee deleted file mode 100644 index 6a9027d..0000000 --- a/packages/math/vertice.coffee +++ /dev/null @@ -1,18 +0,0 @@ -# Vertice operations. - -# **Vertice** is a utility class to help with vertice operations. A vertice -# is implemented as a 2-element array. Element 0 is *x* and element 1 is *y*. - -# Translate a vertice from an origin point using rotation and scale. -export translate = (v, origin, rotation = 0, scale = 1) -> - - difference = [v[0] - origin[0], v[1] - origin[1]] - magnitude = scale * Math.sqrt( - difference[0] * difference[0] + difference[1] * difference[1] - ) - rotation += Math.atan2 difference[1], difference[0] - - return [ - origin[0] + Math.cos(rotation) * magnitude - origin[1] + Math.sin(rotation) * magnitude - ] diff --git a/packages/math/vertice.js b/packages/math/vertice.js new file mode 100644 index 0000000..19f1559 --- /dev/null +++ b/packages/math/vertice.js @@ -0,0 +1,21 @@ +import * as Vector from './vector'; + +// Vertice operations. + +// **Vertice** is a utility class to help with vertice operations. A vertice +// is implemented as a 2-element array. Element 0 is *x* and element 1 is *y*. + +// Translate a vertice from an origin point using rotation and scale. +export function translate (vertice, origin, rotation = 0, scale = 1) { + // Rotate. + const difference = Vector.sub(vertice, origin); + rotation += Vector.angle(difference); + const magnitude = Vector.magnitude(vertice, origin); + vertice = Vector.add( + origin, + Vector.scale([Math.cos(rotation), Math.sin(rotation)], magnitude) + ); + // Scale. + vertice = Vector.scale(vertice, scale); + return vertice; +}