feat: center & localize

This commit is contained in:
cha0s 2021-03-20 02:00:57 -05:00
parent f7e2856d8a
commit d3a0c2a21d

View File

@ -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),