Implement Douglas–Peucker algorithm for point reduction before the curve fitting.

This commit is contained in:
Jürg Lehni 2011-06-05 21:28:49 +01:00
parent aef8dcf2d3
commit 42f5b5e26e

View file

@ -31,6 +31,7 @@ var PathFitter = Base.extend({
prev = point;
}
}
this.points = this.reducePoints(this.points, 1);
this.error = error;
this.iterationError = error * error;
},
@ -46,6 +47,28 @@ var PathFitter = Base.extend({
return this.segments;
},
// DouglasPeucker algorithm
reducePoints: function(points, tolerance) {
var line = new Line(points[0], points[points.length - 1], false);
var maxDistance = 0,
maxIndex = 0;
for (var i = 1; i < points.length - 1; i++) {
var distance = line.getDistance(points[i]);
if(distance > maxDistance) {
maxDistance = distance;
maxIndex = i;
}
}
if (maxDistance >= tolerance) {
var pts = this.reducePoints(
points.slice(0, maxIndex + 1), tolerance);
pts.pop();
return pts.concat(this.reducePoints(
points.slice(maxIndex, points.length), tolerance));
}
return [points[0], points[points.length - 1]];
},
// Fit a Bezier curve to a (sub)set of digitized points
fitCubic: function(first, last, tHat1, tHat2) {
// Use heuristic if region only has two points in it