Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace some troublesome dependencies #17

Merged
merged 4 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

var RBush = require('rbush');
var convexHull = require('monotone-convex-hull-2d');
var Queue = require('tinyqueue');
var pointInPolygon = require('point-in-polygon');
var orient = require('robust-orientation')[3];
const orient = require('robust-predicates/umd/orient2d.min.js').orient2d;

module.exports = concaveman;
module.exports.default = concaveman;
Expand Down Expand Up @@ -170,11 +169,15 @@ function noIntersections(a, b, segTree) {
return true;
}

function cross(p1, p2, p3) {
return orient(p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]);
}

// check if the edges (p1,q1) and (p2,q2) intersect
function intersects(p1, q1, p2, q2) {
return p1 !== q2 && q1 !== p2 &&
orient(p1, q1, p2) > 0 !== orient(p1, q1, q2) > 0 &&
orient(p2, q2, p1) > 0 !== orient(p2, q2, q1) > 0;
cross(p1, q1, p2) > 0 !== cross(p1, q1, q2) > 0 &&
cross(p2, q2, p1) > 0 !== cross(p2, q2, q1) > 0;
}

// update the bounding box of a node's edge
Expand Down Expand Up @@ -212,12 +215,7 @@ function fastConvexHull(points) {
}

// get convex hull around the filtered points
var indices = convexHull(filtered);

// return the hull as array of points (rather than indices)
var hull = [];
for (i = 0; i < indices.length; i++) hull.push(filtered[indices[i]]);
return hull;
return convexHull(filtered);
}

// create a new node in a doubly linked list
Expand Down Expand Up @@ -350,3 +348,31 @@ function sqSegSegDist(x0, y0, x1, y1, x2, y2, x3, y3) {

return dx * dx + dy * dy;
}

function compareByX(a, b) {
return a[0] === b[0] ? a[1] - b[1] : a[0] - b[0];
}

function convexHull(points) {
points.sort(compareByX);

var lower = [];
for (var i = 0; i < points.length; i++) {
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], points[i]) <= 0) {
lower.pop();
}
lower.push(points[i]);
}

var upper = [];
for (var ii = points.length - 1; ii >= 0; ii--) {
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], points[ii]) <= 0) {
upper.pop();
}
upper.push(points[ii]);
}

upper.pop();
lower.pop();
return lower.concat(upper);
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"description": "Fast 2D concave hull algorithm in JavaScript (generates an outline of a point set)",
"main": "index.js",
"dependencies": {
"monotone-convex-hull-2d": "^1.0.1",
"point-in-polygon": "^1.0.1",
"rbush": "^3.0.0",
"robust-orientation": "^1.1.3",
"robust-predicates": "^2.0.4",
"tinyqueue": "^2.0.3"
},
"devDependencies": {
Expand Down