From ad801fa786f26ba79c2226ed2f8f56a6521f2d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 15 Feb 2014 23:37:41 +0100 Subject: [PATCH 1/4] Use parameter tolerances in Path#slipt() Required because CurveLocation objects as returned by Path#getLocationAt() have some imprecision in their #parameter values. Fixes #401. --- src/path/Path.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/path/Path.js b/src/path/Path.js index e4d004d3..82d0ef21 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1068,15 +1068,17 @@ var Path = PathItem.extend(/** @lends Path# */{ index = arg.index; parameter = arg.parameter; } - if (parameter >= 1) { + var tolerance = /*#=*/ Numerical.TOLERANCE; + if (parameter >= 1 - tolerance) { // t == 1 is the same as t == 0 and index ++ index++; parameter--; } var curves = this.getCurves(); + paper.console.log(index, parameter); if (index >= 0 && index < curves.length) { // Only divide curves if we're not on an existing segment already. - if (parameter > 0) { + if (parameter > tolerance) { // Divide the curve with the index at given parameter. // Increase because dividing adds more segments to the path. curves[index++].divide(parameter, true); @@ -1344,13 +1346,13 @@ var Path = PathItem.extend(/** @lends Path# */{ var start = length, curve = curves[i]; length += curve.getLength(); - if (length >= offset) { + if (length > offset) { // Found the segment within which the length lies return curve.getLocationAt(offset - start); } } - // It may be that through impreciseness of getLength, that the end - // of the curves was missed: + // It may be that through imprecision of getLength, that the end of the + // last curve was missed: if (offset <= this.getLength()) return new CurveLocation(curves[curves.length - 1], 1); return null; From a912ed96f6cb422c5a6cebdc0c3261f42f53c4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 19 Feb 2014 14:28:57 +0100 Subject: [PATCH 2/4] Define failing test for issue #400. --- test/tests/Item_Contains.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/tests/Item_Contains.js b/test/tests/Item_Contains.js index d419e4f2..c3f16b35 100644 --- a/test/tests/Item_Contains.js +++ b/test/tests/Item_Contains.js @@ -188,4 +188,17 @@ test('Path#contains() (touching stationary point with changing orientation)', fu }); testPoint(path, new Point(200, 200), true); -}) \ No newline at end of file +}); + +test('Path#contains() (complex shape)', function() { + var path = new Path({ + pathData: 'M301 162L307 154L315 149L325 139.5L332.5 135.5L341 128.5L357.5 117.5L364.5 114.5L368.5 110.5L380 105.5L390.5 102L404 96L410.5 96L415 97.5L421 104L425.5 113.5L428.5 126L429.5 134L429.5 141L429.5 148L425.5 161.5L425.5 169L414 184.5L409.5 191L401 201L395 209L386 214.5L378.5 217L368 220L348 219.5L338 218L323.5 212.5L312 205.5L302.5 197.5L295.5 189L291.5 171.5L294 168L298 165.5L301 162z', + fillColor: 'blue', + strokeColor: 'green', + strokeWidth: 2 + }); + + testPoint(path, new Point(360, 160), true); + testPoint(path, new Point(377, 96), false); +}); + From ea89498e48a82478b65e13ac4e3cedce2ef96b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 19 Feb 2014 14:31:32 +0100 Subject: [PATCH 3/4] Remove debug logging. --- src/path/Path.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/path/Path.js b/src/path/Path.js index 82d0ef21..de1d0c47 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1075,7 +1075,6 @@ var Path = PathItem.extend(/** @lends Path# */{ parameter--; } var curves = this.getCurves(); - paper.console.log(index, parameter); if (index >= 0 && index < curves.length) { // Only divide curves if we're not on an existing segment already. if (parameter > tolerance) { From 641514cbf3fc6f65c6db05b8628500f5388c0372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 19 Feb 2014 14:35:04 +0100 Subject: [PATCH 4/4] Rename Item_Contains tests to PathItem_Contains and add more failing tests for #400. --- test/tests/{Item_Contains.js => PathItem_Contains.js} | 4 +++- test/tests/load.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) rename test/tests/{Item_Contains.js => PathItem_Contains.js} (98%) diff --git a/test/tests/Item_Contains.js b/test/tests/PathItem_Contains.js similarity index 98% rename from test/tests/Item_Contains.js rename to test/tests/PathItem_Contains.js index c3f16b35..b86ad6d0 100644 --- a/test/tests/Item_Contains.js +++ b/test/tests/PathItem_Contains.js @@ -10,7 +10,7 @@ * All rights reserved. */ -module('Item Contains'); +module('PathItem Contains'); function testPoint(item, point, inside) { equals(item.contains(point), inside, 'The point ' + point @@ -200,5 +200,7 @@ test('Path#contains() (complex shape)', function() { testPoint(path, new Point(360, 160), true); testPoint(path, new Point(377, 96), false); + testPoint(path, new Point(410, 218), false); + testPoint(path, new Point(431, 104), false); }); diff --git a/test/tests/load.js b/test/tests/load.js index 986cc42a..682344c7 100644 --- a/test/tests/load.js +++ b/test/tests/load.js @@ -23,7 +23,6 @@ /*#*/ include('Item_Cloning.js'); /*#*/ include('Item_Order.js'); /*#*/ include('Item_Bounds.js'); -/*#*/ include('Item_Contains.js'); /*#*/ include('Layer.js'); /*#*/ include('Group.js'); @@ -39,6 +38,8 @@ /*#*/ include('Path_Length.js'); /*#*/ include('CompoundPath.js'); +/*#*/ include('PathItem_Contains.js'); + /*#*/ include('PlacedSymbol.js'); /*#*/ include('Raster.js');