move convex hull to eucleadian module

This commit is contained in:
Val Erastov (xibyte) 2020-07-19 22:01:26 -07:00
parent 24acddcc5a
commit 08802f29c8
3 changed files with 29 additions and 27 deletions

View file

@ -201,4 +201,31 @@ export function lineLineIntersection(p1, p2, v1, v2) {
u1: n2.dot(p2.minus(p1)) / n2.dot(v1),
u2: n1.dot(p1.minus(p2)) / n1.dot(v2),
}
}
}
export function ConvexHull2D(points) {
function removeMiddle(a, b, c) {
var cross = (a.x - b.x) * (c.y - b.y) - (a.y - b.y) * (c.x - b.x);
var dot = (a.x - b.x) * (c.x - b.x) + (a.y - b.y) * (c.y - b.y);
return cross < 0 || cross == 0 && dot <= 0;
}
points.sort(function (a, b) {
return a.x !== b.x ? a.x - b.x : a.y - b.y;
});
const n = points.length;
const hull = [];
for (let i = 0; i < 2 * n; i++) {
const j = i < n ? i : 2 * n - 1 - i;
while (hull.length >= 2 && removeMiddle(hull[hull.length - 2], hull[hull.length - 1], points[j])) {
hull.pop();
}
hull.push(points[j]);
}
hull.pop();
return hull;
}

View file

@ -1,24 +0,0 @@
export function ConvexHull2D(points) {
points.sort(function (a, b) {
return a.x != b.x ? a.x - b.x : a.y - b.y;
});
const n = points.length;
const hull = [];
for (let i = 0; i < 2 * n; i++) {
const j = i < n ? i : 2 * n - 1 - i;
while (hull.length >= 2 && removeMiddle(hull[hull.length - 2], hull[hull.length - 1], points[j])) {
hull.pop();
}
hull.push(points[j]);
}
hull.pop();
return hull;
}
function removeMiddle(a, b, c) {
var cross = (a.x - b.x) * (c.y - b.y) - (a.y - b.y) * (c.x - b.x);
var dot = (a.x - b.x) * (c.x - b.x) + (a.y - b.y) * (c.y - b.y);
return cross < 0 || cross == 0 && dot <= 0;
}

View file

@ -1,9 +1,8 @@
import {SketchObject} from './sketch-object'
import {Segment} from './segment'
import {ConvexHull2D} from '../../math/convex-hull'
import * as draw_utils from '../shapes/draw-utils'
import {isPointInsidePolygon, polygonOffset} from "math/euclidean";
import {isPointInsidePolygon, polygonOffset, ConvexHull2D} from "math/euclidean";
import Vector from "math/vector";