Clean up code from #597 a bit.

This commit is contained in:
Jürg Lehni 2014-12-26 06:42:46 +01:00
parent 71e2af9d70
commit 28538d8a43

View file

@ -22,24 +22,24 @@
*/ */
var PathFitter = Base.extend({ var PathFitter = Base.extend({
initialize: function(path, error) { initialize: function(path, error) {
this.points = []; var points = this.points = [],
var segments = path._segments, segments = path._segments,
prev; prev;
// Copy over points from path and filter out adjacent duplicates. // Copy over points from path and filter out adjacent duplicates.
for (var i = 0, l = segments.length; i < l; i++) { for (var i = 0, l = segments.length; i < l; i++) {
var point = segments[i].point.clone(); var point = segments[i].point.clone();
if (!prev || !prev.equals(point)) { if (!prev || !prev.equals(point)) {
this.points.push(point); points.push(point);
prev = point; prev = point;
} }
} }
// we need to duplicate first and last segments when simplifying a // We need to duplicate the first and last segment when simplifying a
// closed path // closed path.
if ( path._closed ) { if (path._closed) {
this._closed = true; this.closed = true;
this.points.unshift( segments[l - 1].point.clone() ); points.unshift(points[points.length - 1]);
this.points.push( segments[0].point.clone() ); points.push(points[1]); // The point previously at index 0 is now 1.
} }
this.error = error; this.error = error;
@ -47,8 +47,9 @@ var PathFitter = Base.extend({
fit: function() { fit: function() {
var points = this.points, var points = this.points,
length = points.length; length = points.length,
this.segments = length > 0 ? [new Segment(points[0])] : []; segments = this.segments = length > 0
? [new Segment(points[0])] : [];
if (length > 1) if (length > 1)
this.fitCubic(0, length - 1, this.fitCubic(0, length - 1,
// Left Tangent // Left Tangent
@ -56,13 +57,13 @@ var PathFitter = Base.extend({
// Right Tangent // Right Tangent
points[length - 2].subtract(points[length - 1]).normalize()); points[length - 2].subtract(points[length - 1]).normalize());
// remove the duplicated segments // Remove the duplicated segments for closed paths again.
if ( this._closed ) { if (this.closed) {
this.segments.shift(); segments.shift();
this.segments.pop(); segments.pop();
} }
return this.segments; return segments;
}, },
// Fit a Bezier curve to a (sub)set of digitized points // Fit a Bezier curve to a (sub)set of digitized points