From 5211e86e3a3cdd7abcf1a2148fe785e5f60ea4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 28 Apr 2011 15:12:58 +0100 Subject: [PATCH 1/5] Rename Point#isParallel() to #isColinear(), reimplement it using #cross() and add #isOrthogonal() as well, using #dot(). --- src/basic/Point.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/basic/Point.js b/src/basic/Point.js index b554c195..d79042ab 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -353,15 +353,25 @@ var Point = this.Point = Base.extend({ }, /** - * Checks if the vector represented by this point is parallel (collinear) to + * Checks if the vector represented by this point is colinear (parallel) to * another vector. * * @param point the vector to check against * @return {@true if it is parallel} */ - isParallel: function(point) { - // TODO: Tolerance seems rather high! - return Math.abs(this.x / point.x - this.y / point.y) < 0.00001; + isColinear: function(point) { + return this.cross(point) < Numerical.TOLERANCE; + }, + + /** + * Checks if the vector represented by this point is orthogonal + * (perpendicular) to another vector. + * + * @param point the vector to check against + * @return {@true if it is orthogonal} + */ + isOrthogonal: function(point) { + return this.dot(point) < Numerical.TOLERANCE; }, /** From ea510c2403133b865abb3258f645ffda9ec6ef6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 28 Apr 2011 15:42:16 +0100 Subject: [PATCH 2/5] Separate Path#remove(), #remove(index) & #remove(from, to) into #removeSegment(index) and #removeSegments(from, to). --- src/path/Path.js | 27 ++++++++++----------------- test/tests/Path.js | 6 +++--- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/path/Path.js b/src/path/Path.js index d8cd911e..6fdeb3ab 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -107,24 +107,17 @@ var Path = this.Path = PathItem.extend({ segment = Segment.read(arguments, 1); return segment ? this._add(segment, index) : null; }, + + // TODO: Port back to Sg + removeSegment: function(index) { + var segment = this._segments[index] + return segment && segment.remove() ? segment : null; + }, - remove: function() { - if (!arguments.length) { - // remove() - this.base(); - } else if (arguments.length == 1) { - if (arguments[0].point) { - // remove(segment) - arguments[0].remove(); - } else { - // remove(index) - this._segments[arguments[0]].remove(); - } - } else { - // remove(fromIndex, toIndex) - for(var i = arguments[1], l = arguments[0]; i >= l; i--) - this._segments[i].remove(); - } + // TODO: Port back to Sg + removeSegments: function(from, to) { + for(var i = to; i >= from; i--) + this.removeSegment(i); }, isSelected: function() { diff --git a/test/tests/Path.js b/test/tests/Path.js index 7a2f2de7..8a308ca5 100644 --- a/test/tests/Path.js +++ b/test/tests/Path.js @@ -63,13 +63,13 @@ test('path.remove()', function() { path.add(20, 0); path.add(30, 0); - path.remove(0); + path.removeSegment(0); equals(path.segments.length, 3); - path.remove(path.segments[0]); + path.removeSegment(0); equals(path.segments.length, 2); - path.remove(0, 1); + path.removeSegments(0, 1); equals(path.segments.length, 0); path.remove(); From 121ec5a93f2837605b1b2a5bcdf3827e5195c38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 28 Apr 2011 19:02:34 +0100 Subject: [PATCH 3/5] Allow #removeSegments() to be called without arguments, in which case all segments are removed. --- src/path/Path.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/path/Path.js b/src/path/Path.js index 6fdeb3ab..fb2b3304 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -116,8 +116,10 @@ var Path = this.Path = PathItem.extend({ // TODO: Port back to Sg removeSegments: function(from, to) { - for(var i = to; i >= from; i--) - this.removeSegment(i); + var i = Base.pick(to, this._segments.length), + from = from || 0; + while (i >= from) + this.removeSegment(i--); }, isSelected: function() { From d098c6e8b28e28cb6282c5e08f843cf49a7e7453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 28 Apr 2011 19:03:05 +0100 Subject: [PATCH 4/5] Fix index issue in #removeSegments(). --- src/path/Path.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/path/Path.js b/src/path/Path.js index fb2b3304..f90a3d76 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -116,7 +116,7 @@ var Path = this.Path = PathItem.extend({ // TODO: Port back to Sg removeSegments: function(from, to) { - var i = Base.pick(to, this._segments.length), + var i = Base.pick(to, this._segments.length - 1), from = from || 0; while (i >= from) this.removeSegment(i--); From 6d84dc3351edf484c0937dc0fa06ec5875bdffa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 28 Apr 2011 19:04:00 +0100 Subject: [PATCH 5/5] Segment#isSelected() only checks for selection state of point. We need to check _selectionState instead to mirror behaviour in #_setSelected(). --- src/path/Segment.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/path/Segment.js b/src/path/Segment.js index 326307d8..7de08259 100644 --- a/src/path/Segment.js +++ b/src/path/Segment.js @@ -208,8 +208,10 @@ var Segment = this.Segment = Base.extend({ remove: function() { if (this._path) { this._path._segments.splice(this.getIndex(), 1); - if (this.isSelected()) + if (this._selectionState) { this._path._selectedSegmentCount--; + this._selectionState = 0; + } return true; } return false;