From 41aca10112a17513e04745182d65bc10520be878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 3 Feb 2016 12:54:54 +0100 Subject: [PATCH] Clean-up changes from #939 --- src/path/PathItem.Boolean.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/path/PathItem.Boolean.js b/src/path/PathItem.Boolean.js index cbf6ae17..c1e40c94 100644 --- a/src/path/PathItem.Boolean.js +++ b/src/path/PathItem.Boolean.js @@ -314,6 +314,7 @@ PathItem.inject(new function() { py = point.y, windLeft = 0, windRight = 0, + isOnCurve = false, length = curves.length, roots = [], abs = Math.abs; @@ -349,17 +350,15 @@ PathItem.inject(new function() { var xBefore = px - epsilon, xAfter = px + epsilon, prevWinding, - prevXEnd, - isOnCurve; + prevXEnd; for (var i = 0; i < length; i++) { var curve = curves[i], winding = curve.winding, values = curve.values, yStart = values[1], yEnd = values[7]; - // The first curve of a loop holds the last curve with - // non-zero winding. - // Retrieve and use it here (See _getMonoCurve()). + // The first curve of a loop holds the last curve with non-zero + // winding. Retrieve and use it here (See _getMonoCurve()). if (curve.last) { // Get the end x coordinate and winding of the last // non-horizontal curve, which will be the previous @@ -374,19 +373,19 @@ PathItem.inject(new function() { // Horizontal curves with winding == 0 will be completely // ignored. if (winding && (py >= yStart && py <= yEnd - || py >= yEnd && py <= yStart)) { + || py >= yEnd && py <= yStart)) { // Calculate the x value for the ray's intersection. var x = py === yStart ? values[0] : py === yEnd ? values[6] : Curve.solveCubic(values, 1, py, roots, 0, 1) === 1 - ? Curve.getPoint(values, roots[0]).x - : null; + ? Curve.getPoint(values, roots[0]).x + : null; if (x != null) { // If the point is between the curve's start point and // the previous curve's end point, it must be on a - // horizontal curve inbetween. + // horizontal curve in between. var isOnHorizontal = - (py === yStart && (px - x) * (px - prevXEnd) < 0); + py === yStart && (px - x) * (px - prevXEnd) < 0; // Test if the point is on a y-monotonic curve if ((x >= xBefore && x <= xAfter) || isOnHorizontal) isOnCurve = true; @@ -396,7 +395,7 @@ PathItem.inject(new function() { // - or the crossing is at the start of the curve and // the winding did not change between curves if (py != yEnd - && (py != yStart || winding === prevWinding)) { + && (py != yStart || winding === prevWinding)) { if (x < xBefore) { windLeft += winding; } else if (x > xAfter) { @@ -411,7 +410,10 @@ PathItem.inject(new function() { } } } - // If the point was on a monocurve, ensure that the winding is odd. + // If the point is on a monotonic curve, ensure that the winding is odd + // by applying bit-wise or `| 1` to the bigger absolute winding number: + // If winding is an even number and the point is on a curve, this will + // add up to the next uneven number, otherwise it remains the same. return Math.max(abs(windLeft), abs(windRight)) | (isOnCurve ? 1 : 0 ); }