line-simplify-rdp
Advanced tools
Comparing version 0.3.1 to 0.4.0
55
index.js
'use strict'; | ||
/** @module line-simplify-rdp */ | ||
var distanceToLineSegment2 = require('distance-to-line-segment').squaredWithPrecalc; | ||
@@ -39,8 +42,48 @@ | ||
function ptsEqual(p1, p2) { | ||
return p1.x === p2.x && p1.y === p2.y; | ||
} | ||
function simplify(points, threshold) { | ||
/** | ||
* Return a simplified version of the polyline or polygon defined by the given | ||
* points. Basically what happens is that some points are removed from the line, | ||
* but only ones that leave the resulting line within a certain distance of the | ||
* original. | ||
* @alias module:line-simplify-rdp | ||
* @param {array} points - polyline or polygon. Each element of the array must be | ||
* an object with at least x and y numeric properties. Neither the array | ||
* nor any member will be modified. | ||
* @param {number} threshold - maximum distance the simplified line can be from | ||
* the original. Should be grater than or equal to zero. Passing zero means | ||
* only colinear points will be eliminated; greater values lead to more | ||
* aggressive line approximations. | ||
* @param {boolean} closed - if the passed first point and the last points are | ||
* the same, this flag does nothing. Otherwise, if closed is true, the | ||
* points will be treated as a polygon with an implied last segment | ||
* between the last point and the first point. | ||
* @returns {array} the simplified points. (This will always be separately | ||
* allocated object, not the one passed in, but it *will* contain the | ||
* same point objects passed in.) | ||
*/ | ||
function simplify(points, threshold, closed) { | ||
var len = points.length; | ||
if (len <= 2) | ||
return points; | ||
threshold = threshold || 0; | ||
if (len <= 2 || threshold < 0) | ||
return points.slice(0); | ||
if (closed) { | ||
if (ptsEqual(points[0], points[points.length-1])) { | ||
// Treat as unclosed | ||
closed = false; | ||
} | ||
else { | ||
points = points.slice(0); | ||
points.push(points[0]); | ||
len++; | ||
} | ||
} | ||
var keepers = []; | ||
@@ -54,2 +97,6 @@ simplifyInternal(points, 0, len-1, threshold, keepers); | ||
} | ||
if (closed) { | ||
out.pop(); | ||
} | ||
return out; | ||
@@ -56,0 +103,0 @@ } |
{ | ||
"name": "line-simplify-rdp", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Simplify lines using the Ramer–Douglas–Peucker algorithm", | ||
@@ -11,2 +11,3 @@ "repository": "https://github.com/scottglz/line-simplify-rdp", | ||
"validate": "npm ls", | ||
"makedocs": "jsdoc2md index.js | cat readme_src/head.md - readme_src/tail.md > README.md", | ||
"prepublish": "npm run lint && npm run validate && npm run test" | ||
@@ -29,2 +30,3 @@ }, | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^1.3.3", | ||
"jshint": "^2.9.1", | ||
@@ -31,0 +33,0 @@ "tape": "^4.4.0" |
17
test.js
@@ -7,3 +7,3 @@ 'use strict'; | ||
test("stairstep", function(t) { | ||
t.plan(4); | ||
t.plan(5); | ||
var stairstep = [ { x:0, y: 0}]; | ||
@@ -19,3 +19,3 @@ for (var i=1; i < 4; i++) { | ||
for (var i=3; i < 1000; i++) { | ||
for (var i=4; i < 1000; i++) { | ||
stairstep.push({ | ||
@@ -26,7 +26,8 @@ x: Math.floor((i)/2), | ||
} | ||
t.deepEquals(simplify(stairstep, 1 ), [stairstep[0],stairstep[999]], "1000 points"); | ||
t.deepEquals(simplify(stairstep, 1 ), [stairstep[0],stairstep[999]], "1000 points"); | ||
t.deepEquals(simplify(stairstep, 1, true), [stairstep[0],stairstep[999]], "1000 points, closed"); | ||
}); | ||
test("unsimplifiable", function(t) { | ||
t.plan(1); | ||
t.plan(2); | ||
var points = [ | ||
@@ -40,2 +41,3 @@ { x: 15, y: 60 }, | ||
t.deepEquals(simplify(points, 1), points); | ||
t.deepEquals(simplify(points, 1, true), points, "Same results here with closed flag"); | ||
}); | ||
@@ -52,6 +54,7 @@ | ||
test("redundant square", function(t) { | ||
t.plan(1); | ||
var points = expandPts([0,0, 0,1, 0,2, 0,3, 1,3, 2,3, 3,3, 3,2, 3,1, 3,0, 2,0, 1,0, 0,0]); | ||
t.deepEquals(simplify(points, 0), expandPts([0,0, 0,3, 3,3, 3,0, 0,0])); | ||
t.plan(2); | ||
var points = expandPts([0,0, 0,1, 0,2, 0,3, 1,3, 2,3, 3,3, 3,2, 3,1, 3,0, 2,0, 1,0]); | ||
t.deepEquals(simplify(points, 0, true), expandPts([0,0, 0,3, 3,3, 3,0])); | ||
t.deepEquals(simplify(points, 0), expandPts([0,0, 0,3, 3,3, 3,0, 1,0]), "Different results without closed flag"); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
7384
6
128
1
30
3