diff --git a/modules/math/euclidean.ts b/modules/math/euclidean.ts index f823e6c6..9ed2164e 100644 --- a/modules/math/euclidean.ts +++ b/modules/math/euclidean.ts @@ -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), } -} \ No newline at end of file +} + +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; +} + diff --git a/web/app/math/convex-hull.js b/web/app/math/convex-hull.js deleted file mode 100644 index cba249ff..00000000 --- a/web/app/math/convex-hull.js +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/web/app/sketcher/shapes/bezier-curve.js b/web/app/sketcher/shapes/bezier-curve.js index a1ab06c9..09383353 100644 --- a/web/app/sketcher/shapes/bezier-curve.js +++ b/web/app/sketcher/shapes/bezier-curve.js @@ -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";