feat: vertice operations
This commit is contained in:
parent
af373189a6
commit
31efc515c3
|
@ -7,3 +7,6 @@ export {Rectangle};
|
|||
|
||||
import * as Vector from './vector';
|
||||
export {Vector};
|
||||
|
||||
import * as Vertice from './vertice';
|
||||
export {Vertice};
|
||||
|
|
|
@ -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
21
packages/math/vertice.js
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user