From 25e5582a35be6d05aaafd87857c1e0bc4a7d9912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 3 May 2013 21:41:22 -0700 Subject: [PATCH] Define CompoundPath#reverse() and #clockwise, and replace reversePath() with it. --- src/path/CompoundPath.js | 25 +++++++++++++++++++++++++ src/path/Path.js | 9 ++++----- src/path/PathItem.Boolean.js | 21 ++++----------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index f1d00990..9cf5a976 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -89,11 +89,36 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath# return this; }, + /** + * Reverses the orientation of all nested paths. + */ + reverse: function() { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) + children[i].reverse(); + }, + smooth: function() { for (var i = 0, l = this._children.length; i < l; i++) this._children[i].smooth(); }, + /** + * Specifies whether the compound path is oriented clock-wise. + * + * @type Boolean + * @bean + */ + isClockwise: function() { + var child = this.getFirstChild(); + return child && child.isClockwise(); + }, + + setClockwise: function(clockwise) { + if (this.isClockwise() != !!clockwise) + this.reverse(); + }, + /** * The first Segment contained within the path. * diff --git a/src/path/Path.js b/src/path/Path.js index 42c67503..6f8f3b85 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1085,19 +1085,18 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ }, setClockwise: function(clockwise) { + // Only revers the path if its clockwise orientation is not the same + // as what it is now demanded to be. // On-the-fly conversion to boolean: - if (this.isClockwise() != (clockwise = !!clockwise)) { - // Only revers the path if its clockwise orientation is not the same - // as what it is now demanded to be. + if (this.isClockwise() != (clockwise = !!clockwise)) this.reverse(); - } // Reverse only flips _clockwise state if it was already set, so let's // always set this here now. this._clockwise = clockwise; }, /** - * Reverses the segments of the path. + * Reverses the orientation of the path, by reversing all its segments. */ reverse: function() { this._segments.reverse(); diff --git a/src/path/PathItem.Boolean.js b/src/path/PathItem.Boolean.js index 0ae15100..949464b7 100644 --- a/src/path/PathItem.Boolean.js +++ b/src/path/PathItem.Boolean.js @@ -108,21 +108,6 @@ PathItem.inject(new function() { return baseWinding; } - function reversePath(path) { - var baseWinding; - if (path instanceof CompoundPath) { - var children = path.children, i, len; - for (i = 0, len = children.length; i < len; i++) { - children[i].reverse(); - } - baseWinding = children[0].clockwise; - } else { - path.reverse(); - baseWinding = path.clockwise; - } - return baseWinding; - } - function computeBoolean(path1, path2, operator, subtract, _cache) { var _path1, _path2, path1Clockwise, path2Clockwise; var ixs, path1Id, path2Id; @@ -147,8 +132,10 @@ PathItem.inject(new function() { path1Id = _path1.id; path2Id = _path2.id; // Do operator specific calculations before we begin - if (subtract) - path2Clockwise = reversePath(_path2); + if (subtract) { + _path2.reverse(); + path2Clockwise = !path2Clockwise; + } var i, j, len, path, crv; var paths = [];