UNPKG

jakke-graphics-ts

Version:

My common graphics utils for building my aec apps.

60 lines 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeContexHull2d = computeContexHull2d; function computeContexHull2d(vertices) { // Find anchor let min = vertices[0]; const comparableVertices = []; for (const v of vertices) { if (v.y < min.y || (v.y === min.y && v.x < min.x)) { min = v; } } // Set comparable vertices for (const v of vertices) { if (v !== min) comparableVertices.push(v); } // Get parameterized comparables and sort const parameterizedVertices = vertices.map(v => { const vec = { x: v.x - min.x, y: v.y - min.y, z: 0 }; return { vertex: v, param: Math.atan2(vec.y, vec.x) }; }); const sorted = parameterizedVertices.sort((a, b) => a.param === b.param ? getSquaredDistance(min, a.vertex) - getSquaredDistance(min, b.vertex) : a.param - b.param); // Compares const stack = [min, sorted[0].vertex]; for (let i = 1; i < sorted.length; i++) { const pt = sorted[i].vertex; while (stack.length >= 2 && !isCCW2d(stack[stack.length - 2], stack[stack.length - 1], pt)) { stack.pop(); } stack.push(pt); } return stack; } function getSquaredDistance(v0, v1) { return Math.pow(v1.x - v0.x, 2) + Math.pow(v1.y - v0.y, 2) + Math.pow(v1.z - v0.z, 2); } function isCCW2d(p0, p1, p2) { const v1 = { x: p1.x - p0.x, y: p1.y - p0.y, z: p1.z - p0.z, }; const v2 = { x: p2.x - p1.x, y: p2.y - p1.y, z: p2.z - p1.z, }; return cross2d(v1, v2) > 0; } function cross2d(p0, p1) { return (p0.y * p1.z - p0.z * p1.y) - (p0.x * p1.z - p0.z * p1.x) + (p0.x * p1.y - p0.y * p1.x); } //# sourceMappingURL=convexHullUtils.js.map