From 9d4066da1cd7c649ef944e209e933c1f850ca2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 28 Dec 2015 01:05:54 +0100 Subject: [PATCH] Implement unit tests for getWinding() and zero-winding curves. Closes #819 --- test/tests/HitResult.js | 18 ++++----- test/tests/PathItem_Contains.js | 66 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/test/tests/HitResult.js b/test/tests/HitResult.js index 80c1d160..989992c6 100644 --- a/test/tests/HitResult.js +++ b/test/tests/HitResult.js @@ -12,7 +12,7 @@ module('HitResult'); -test('hit-testing options', function() { +test('Hit-testing options', function() { var defaultOptions = { type: null, tolerance: paper.settings.hitTolerance, @@ -296,7 +296,7 @@ test('hit-testing stroke on segment point of a path', function() { equals(error == null, true, description); }); -test('Hit testing a point that is extremely close to a curve', function() { +test('hit-testing a point that is extremely close to a curve', function() { var path = new Path.Rectangle([0, 0], [100, 100]); // A point whose x value is extremely close to 0: var point = new Point(2.842 / Math.pow(10, 14), 0); @@ -492,7 +492,7 @@ test('hitting path with a text item in the project', function() { }); -test('Check hit-testing of items that come after a transformed group.', function() { +test('hit-testing of items that come after a transformed group.', function() { paper.project.currentStyle.fillColor = 'black'; var point1 = new Point(100, 100); var point2 = new Point(140, 100); @@ -556,7 +556,7 @@ test('Check hit-testing of items that come after a transformed group.', function }, path1, 'After moving group before path1, hit-testing path1 for point1 should give us path1.'); }); -test('Check hit-testing of placed symbols.', function() { +test('hit-testing of placed symbols.', function() { var point = new Point(100, 100); var path = new Path.Circle([0, 0], 20); @@ -570,7 +570,7 @@ test('Check hit-testing of placed symbols.', function() { }); -test('Hit testing the corner of a rectangle with miter stroke.', function() { +test('hit-testing the corner of a rectangle with miter stroke.', function() { var rect = new Path.Rectangle({ rectangle: [100, 100, 300, 200], fillColor: '#f00', @@ -583,7 +583,7 @@ test('Hit testing the corner of a rectangle with miter stroke.', function() { }, true); }); -test('Hit testing invisible items.', function() { +test('hit-testing invisible items.', function() { var point = new Point(0, 0); var circle1 = new Path.Circle({ center: point.subtract([25, 0]), @@ -607,7 +607,7 @@ test('Hit testing invisible items.', function() { }, true); }); -test('Hit testing guides.', function() { +test('hit-testing guides.', function() { var point = new Point(0, 0); var circle1 = new Path.Circle({ center: point.subtract([25, 0]), @@ -641,7 +641,7 @@ test('Hit testing guides.', function() { }, true); }); -test('Hit testing fill with tolerance', function() { +test('hit-testing fill with tolerance', function() { var path = new Path.Rectangle({ from: [50, 50], to: [200, 200], @@ -658,7 +658,7 @@ test('Hit testing fill with tolerance', function() { }, true); }); -test('Hit testing compound-paths', function() { +test('hit-testing compound-paths', function() { var center = new Point(100, 100); var path1 = new Path.Circle({ center: center, diff --git a/test/tests/PathItem_Contains.js b/test/tests/PathItem_Contains.js index 89f5afd9..deae04a9 100644 --- a/test/tests/PathItem_Contains.js +++ b/test/tests/PathItem_Contains.js @@ -214,3 +214,69 @@ test('Path#contains() (complex shape)', function() { testPoint(path, new Point(431, 104), false); }); + +test('Path#contains() (straight curves with zero-winding)', function() { + var pathPoints = [ + [140, 100], + [140, 10], + [100, 10], + [100, 20], + [120, 20], + [120, 40], + [100, 40], + [100, 60], + [200, 60], + [120, 60], + [120, 100], + [300, 100], + [50, 100] + ]; + + var path1 = new Path({ + segments: pathPoints, + closed: true, + fillRule: 'evenodd' + }); + + var hitPoints = [ + [[30,10], false], + [[110,10], true], + [[30,20], false], + [[110,20], true], + [[130,20], true], + [[170,20], false], + [[110,50], true], + [[30,60], false], + [[110,60], true], + [[130,60], true], + [[150,60], false], + [[230,60], false], + [[10,100], false], + [[60,100], false], + [[130,100], true], + [[170,100], false], + [[370,100], false] + ]; + + for (var i = 0; i < hitPoints.length; i++) { + var entry = hitPoints[i]; + testPoint(path1, new Point(entry[0]), entry[1]); + } + + // Now test the x-y-reversed shape + + for (var i = 0; i < pathPoints.length; i++) { + pathPoints[i].reverse(); + } + + var path1 = new Path({ + segments: pathPoints, + closed: true, + fillRule: 'evenodd' + }); + + for (var i = 0; i < hitPoints.length; i++) { + var entry = hitPoints[i]; + testPoint(path1, new Point(entry[0].reverse()), entry[1]); + } +})