From a2a8939d5d0f6b08c7355284124d9466d63d4763 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Mon, 20 Jun 2011 19:17:07 +0200 Subject: [PATCH] Path# rename #pointsToCurves to #simplify and #curvesToPoints to #flatten. --- .../Scripts/CurveTimeParametrization.html | 6 ++--- .../{PointsToCurves.html => Simplify.html} | 2 +- src/path/Path.js | 23 +++++++++++-------- test/tests/Path.js | 8 +++---- test/tests/Path_Curves.js | 4 ++-- 5 files changed, 24 insertions(+), 19 deletions(-) rename examples/Tools/{PointsToCurves.html => Simplify.html} (98%) diff --git a/examples/Scripts/CurveTimeParametrization.html b/examples/Scripts/CurveTimeParametrization.html index 1adce5c8..d6472974 100644 --- a/examples/Scripts/CurveTimeParametrization.html +++ b/examples/Scripts/CurveTimeParametrization.html @@ -22,12 +22,12 @@ var length = curve.length; var step = 10; var iteratively = false; - var curvesToPoints = true; + var flatten = true; var num = Math.floor(length / step); var prev = 0; - if (curvesToPoints) { + if (flatten) { var clone = path.clone(); - clone.curvesToPoints(step); + clone.flatten(step); for (var i = 0; i < clone.segments.length; i++) { var point = clone.segments[i].point; var circle = new Path.Circle(point, step / 2); diff --git a/examples/Tools/PointsToCurves.html b/examples/Tools/Simplify.html similarity index 98% rename from examples/Tools/PointsToCurves.html rename to examples/Tools/Simplify.html index 033eb5d9..055b54ab 100644 --- a/examples/Tools/PointsToCurves.html +++ b/examples/Tools/Simplify.html @@ -42,7 +42,7 @@ var segmentCount = path.segments.length; // When the mouse is released, simplify it: - path.pointsToCurves(10); + path.simplify(10); // Select the path, so we can see its segments: path.fullySelected = true; diff --git a/src/path/Path.js b/src/path/Path.js index c6eb6526..ac6abc54 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -649,12 +649,14 @@ var Path = this.Path = PathItem.extend({ }, /** - * Converts the curves in the path to straight lines. + * Converts the curves in a path to straight lines with an even distribution + * of points. The distance between the produced segments is as close as + * possible to the value specified by the {@code maxDistance} parameter. * * @param {Number} maxDistance the maximum distance between the points * * @example {@paperscript} - * // Straightening the curves of a circle: + * // Flattening a circle shaped path: * * // Create a circle shaped path at { x: 80, y: 50 } * // with a radius of 35: @@ -668,12 +670,12 @@ var Path = this.Path = PathItem.extend({ * copy.position.x += 150; * * // Convert its curves to points, with a max distance of 20: - * copy.curvesToPoints(20); + * copy.flatten(20); * * // Select the copy, so we can inspect its segments: * copy.selected = true; */ - curvesToPoints: function(maxDistance) { + flatten: function(maxDistance) { var flattener = new PathFlattener(this), pos = 0, // Adapt step = maxDistance so the points distribute evenly. @@ -690,11 +692,15 @@ var Path = this.Path = PathItem.extend({ }, /** + * Smooths a path by simplifying it. The {@link Path#segments} array is + * analyzed and replaced by a more optimal set of segments, reducing memory + * usage and speeding up drawing. + * * @param {Number} [tolerance=2.5] * * @example {@paperscript height=300} * // Click and drag below to draw to draw a line, when you release the - * // mouse, the is made smooth using path.pointsToCurves(): + * // mouse, the is made smooth using path.simplify(): * * var path; * function onMouseDown(event) { @@ -719,13 +725,12 @@ var Path = this.Path = PathItem.extend({ * } * * function onMouseUp(event) { - * // When the mouse is released, simplify the path using - * // the pointsToCurves function: - * path.pointsToCurves(); + * // When the mouse is released, simplify the path: + * path.simplify(); * path.selected = true; * } */ - pointsToCurves: function(tolerance) { + simplify: function(tolerance) { var fitter = new PathFitter(this, tolerance || 2.5); this.setSegments(fitter.fit()); }, diff --git a/test/tests/Path.js b/test/tests/Path.js index 9dd17ba9..624753d0 100644 --- a/test/tests/Path.js +++ b/test/tests/Path.js @@ -148,25 +148,25 @@ test('After removing all segments of a selected path, it should still be selecte }); -test('After simplifying a path using #pointToCurves(), the path should stay fullySelected', function() { +test('After simplifying a path using #simplify(), the path should stay fullySelected', function() { var path = new Path(); for (var i = 0; i < 30; i++) { path.add(i * 10, 10); }; path.fullySelected = true; - path.pointsToCurves(); + path.simplify(); equals(function() { return path.fullySelected; }, true); }); -test('After simplifying a path using #pointToCurves(), the path should stay selected', function() { +test('After simplifying a path using #simplify(), the path should stay selected', function() { var path = new Path(); for (var i = 0; i < 30; i++) { path.add(i * 10, (i % 2 ? 20 : 40)); }; path.selected = true; - path.pointsToCurves(); + path.simplify(); equals(function() { return path.selected; }, true); diff --git a/test/tests/Path_Curves.js b/test/tests/Path_Curves.js index 8c5e3f72..b5a96f8c 100644 --- a/test/tests/Path_Curves.js +++ b/test/tests/Path_Curves.js @@ -24,11 +24,11 @@ test('path.curves Synchronisation', function() { equals(path.curves.toString(), "{ point1: { x: 0, y: 100 }, point2: { x: 100, y: 100 } },{ point1: { x: 100, y: 100 }, point2: { x: 0, y: 100 } }", "path.curves: path.add(new Point(100, 100));\npath.removeSegments(1, 2);"); }); -test('path.curvesToPoints(maxDistance)', function() { +test('path.flatten(maxDistance)', function() { var path = new Path.Circle(new Size(80, 50), 35); // Convert its curves to points, with a max distance of 20: - path.curvesToPoints(20); + path.flatten(20); equals(function() { return path.lastSegment.point.equals(path.firstSegment.point);