feat: vertice operations

This commit is contained in:
cha0s 2019-03-22 11:25:06 -05:00
parent af373189a6
commit 31efc515c3
3 changed files with 24 additions and 18 deletions

View File

@ -7,3 +7,6 @@ export {Rectangle};
import * as Vector from './vector';
export {Vector};
import * as Vertice from './vertice';
export {Vertice};

View File

@ -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
]

21
packages/math/vertice.js Normal file
View File

@ -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;
}