diff --git a/packages/math/src/vertice.js b/packages/math/src/vertice.js index 3302019..ab16d95 100644 --- a/packages/math/src/vertice.js +++ b/packages/math/src/vertice.js @@ -89,6 +89,17 @@ export function bresenham([x1, y1], [x2, y2]) { return points; } +export function center(vs) { + let cx = 0; + let cy = 0; + for (let i = 0; i < vs.length; ++i) { + const [x, y] = vs[i]; + cx += x; + cy += y; + } + return Vector.scale([cx, cy], 1 / vs.length); +} + export function convexHull(vertices) { const scanner = new GrahamScan(); for (let i = 0; i < vertices.length; i++) { @@ -99,6 +110,15 @@ export function convexHull(vertices) { .map(({x, y}) => [x, y]); } +export function localize(vs) { + const c = center(vs); + const localize = []; + for (let i = 0; i < vs.length; i++) { + localize[i] = Vector.sub(vs[i], c); + } + return [localize, c]; +} + const c = [ Vector.fromDirection(0), Vector.fromDirection(1),