Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simplify-js

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simplify-js - npm Package Compare versions

Comparing version 1.0.0 to 1.2.0

LICENSE

30

package.json
{
"name": "simplify-js",
"description": "A high-performance JavaScript 2D/3D polyline simplification library",
"homepage": "http://mourner.github.com/simplify-js/",
"keywords": ["math", "geometry", "polyline", "simplification"],
"author": "Vladimir Agafonkin",
"repository" : {"type": "git", "url": "git://github.com/mourner/simplify-js.git"},
"main": "simplify.js",
"version": "1.0.0"
"name": "simplify-js",
"version": "1.2.0",
"description": "A high-performance JavaScript 2D/3D polyline simplification library",
"homepage": "http://mourner.github.com/simplify-js/",
"author": "Vladimir Agafonkin",
"keywords": [
"math",
"geometry",
"polyline",
"simplification"
],
"repository": {
"type": "git",
"url": "git://github.com/mourner/simplify-js.git"
},
"main": "simplify.js",
"devDependencies": {
"mocha": "~1.13.0"
},
"scripts": {
"test": "./node_modules/.bin/mocha"
}
}

@@ -1,145 +0,140 @@

(function (global) {
"use strict";
/*
(c) 2013, Vladimir Agafonkin
Simplify.js, a high-performance JS polyline simplification library
mourner.github.io/simplify-js
*/
(function () { "use strict";
// modify the following 2 functions to suit your point format
// and/or switch to 3D points
// to suit your point format, run search/replace for '.x' and '.y';
// for 3D version, see 3d branch (configurability would draw significant performance overhead)
// square distance between 2 points
// square distance between 2 points
function getSqDist(p1, p2) {
function sqDist(p1, p2) {
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
//var dz = p1.z - p2.z;
return dx * dx + dy * dy;
}
return dx * dx + dy * dy;
//return dx * dx + dy * dy + dz * dz;
}
// square distance from a point to a segment
function getSqSegDist(p, p1, p2) {
var x = p1.x,
y = p1.y,
dx = p2.x - x,
dy = p2.y - y;
// square distance from a point to a closest point on a segment
if (dx !== 0 || dy !== 0) {
function sqSegDist(p, p1, p2) {
var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
var x = p1.x,
y = p1.y,
//z = p1.z,
dx = p2.x - x,
dy = p2.y - y,
//dz = p2.z - z,
t;
if (t > 1) {
x = p2.x;
y = p2.y;
if (dx !== 0 || dy !== 0) {
t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
//t = ((p.x - x) * dx + (p.y - y) * dy + (p.z - z) * dz) / (dx * dx + dy * dy + dz * dz);
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
if (t > 1) {
x = p2.x;
y = p2.y;
//z = p2.z;
} else if (t > 0) {
x += dx * t;
y += dy * t;
//z += dz * t;
}
}
dx = p.x - x;
dy = p.y - y;
dx = p.x - x;
dy = p.y - y;
//dz = p.z - z;
return dx * dx + dy * dy;
}
// rest of the code doesn't care about point format
return dx * dx + dy * dy;
//return dx * dx + dy * dy + dz * dz;
}
// basic distance-based simplification
function simplifyRadialDist(points, sqTolerance) {
// the rest of the code doesn't care for the point format
var prevPoint = points[0],
newPoints = [prevPoint],
point;
for (var i = 1, len = points.length; i < len; i++) {
point = points[i];
// simplification based on radial distance
if (getSqDist(point, prevPoint) > sqTolerance) {
newPoints.push(point);
prevPoint = point;
}
}
function simplifyRadialDist(points, sqTolerance) {
if (prevPoint !== point) {
newPoints.push(point);
}
var newPoints = [points[0]],
len = points.length,
i,
prev;
return newPoints;
}
for (i = 1, prev = 0; i < len; i += 1) {
if (sqDist(points[i], points[prev]) > sqTolerance) {
newPoints.push(points[i]);
prev = i;
}
}
// simplification using optimized Douglas-Peucker algorithm with recursion elimination
function simplifyDouglasPeucker(points, sqTolerance) {
if (prev < len - 1) {
newPoints.push(points[len - 1]);
}
var len = points.length,
MarkerArray = typeof Uint8Array !== 'undefined' ? Uint8Array : Array,
markers = new MarkerArray(len),
first = 0,
last = len - 1,
stack = [],
newPoints = [],
i, maxSqDist, sqDist, index;
return newPoints;
}
markers[first] = markers[last] = 1;
while (last) {
// simplification using optimized Douglas-Peucker algorithm
maxSqDist = 0;
function markPointsDP(points, markers, sqTolerance, first, last) {
for (i = first + 1; i < last; i++) {
sqDist = getSqSegDist(points[i], points[first], points[last]);
var maxSqDist = 0,
i,
sqDist,
index;
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}
for (i = first + 1; i < last; i += 1) {
sqDist = sqSegDist(points[i], points[first], points[last]);
if (maxSqDist > sqTolerance) {
markers[index] = 1;
stack.push(first, index, index, last);
}
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}
last = stack.pop();
first = stack.pop();
}
if (maxSqDist > sqTolerance) {
markers[index] = 1;
for (i = 0; i < len; i++) {
if (markers[i]) {
newPoints.push(points[i]);
}
}
markPointsDP(points, markers, sqTolerance, first, index);
markPointsDP(points, markers, sqTolerance, index, last);
}
}
return newPoints;
}
function simplifyDouglasPeucker(points, sqTolerance) {
// both algorithms combined for awesome performance
function simplify(points, tolerance, highestQuality) {
var len = points.length,
ArrayConstructor = typeof Uint8Array !== 'undefined' ? Uint8Array : Array,
markers = new ArrayConstructor(len),
i,
newPoints = [];
var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
markers[0] = markers[len - 1] = 1;
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
points = simplifyDouglasPeucker(points, sqTolerance);
markPointsDP(points, markers, sqTolerance, 0, len - 1);
return points;
}
for (i = 0; i < len; i += 1) {
if (markers[i]) {
newPoints.push(points[i]);
}
}
// export as AMD module / Node module / browser variable
if (typeof define === 'function' && define.amd) {
define(function() {
return simplify;
});
} else if (typeof module !== 'undefined') {
module.exports = simplify;
} else {
window.simplify = simplify;
}
return newPoints;
}
var root = (typeof exports !== 'undefined' ? exports : global);
root.simplify = function (points, tolerance) {
tolerance = typeof tolerance !== 'undefined' ? tolerance : 1;
var sqTolerance = tolerance * tolerance;
points = simplifyRadialDist(points, sqTolerance);
points = simplifyDouglasPeucker(points, sqTolerance);
return points;
};
}(this));
})();
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc