Skip to content
Open
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
14 changes: 6 additions & 8 deletions src/hull.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ function lexicographicOrder(a, b) {
return a[0] - b[0] || a[1] - b[1];
}

// Computes the upper convex hull per the monotone chain algorithm.
// Computes the upper (sign = 1) or lower (sign = -1) convex hull per the monotone chain algorithm.
// Assumes points.length >= 3, is sorted by x, unique in y.
// Returns an array of indices into points in left-to-right order.
function computeUpperHullIndexes(points) {
function computeHullIndexes(points, sign) {
const n = points.length,
indexes = [0, 1];
let size = 2, i;

for (i = 2; i < n; ++i) {
while (size > 1 && cross(points[indexes[size - 2]], points[indexes[size - 1]], points[i]) <= 0) --size;
while (size > 1 && sign * cross(points[indexes[size - 2]], points[indexes[size - 1]], points[i]) <= 0) --size;
indexes[size++] = i;
}

Expand All @@ -25,15 +25,13 @@ export default function(points) {

var i,
n,
sortedPoints = new Array(n),
flippedPoints = new Array(n);
sortedPoints = new Array(n);

for (i = 0; i < n; ++i) sortedPoints[i] = [+points[i][0], +points[i][1], i];
sortedPoints.sort(lexicographicOrder);
for (i = 0; i < n; ++i) flippedPoints[i] = [sortedPoints[i][0], -sortedPoints[i][1]];

var upperIndexes = computeUpperHullIndexes(sortedPoints),
lowerIndexes = computeUpperHullIndexes(flippedPoints);
var upperIndexes = computeHullIndexes(sortedPoints, 1),
lowerIndexes = computeHullIndexes(sortedPoints, -1);

// Construct the hull polygon, removing possible duplicate endpoints.
var skipLeft = lowerIndexes[0] === upperIndexes[0],
Expand Down
2 changes: 1 addition & 1 deletion src/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function(polygon) {
yb = b[1];
xa -= xb;
ya -= yb;
perimeter += Math.hypot(xa, ya);
perimeter += Math.sqrt(xa * xa + ya * ya);
}

return perimeter;
Expand Down