From dd576a51925b5bbc26b7121ba072cb4c7261cbef Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 8 Feb 2016 12:07:49 +0000 Subject: [PATCH] Fixed a mathematical edge-case in line-intersection calculations --- .../geometry/juce_PathStrokeType.cpp | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/modules/juce_graphics/geometry/juce_PathStrokeType.cpp b/modules/juce_graphics/geometry/juce_PathStrokeType.cpp index 96cbc60d8b..1923f153da 100644 --- a/modules/juce_graphics/geometry/juce_PathStrokeType.cpp +++ b/modules/juce_graphics/geometry/juce_PathStrokeType.cpp @@ -99,7 +99,8 @@ namespace PathStrokeHelpers return along >= 0 && along <= 1.0f; } - else if (dy2 == 0 && dy1 != 0) + + if (dy2 == 0 && dy1 != 0) { const float along = (y3 - y1) / dy1; intersectionX = x1 + along * dx1; @@ -112,7 +113,8 @@ namespace PathStrokeHelpers return along >= 0 && along <= 1.0f; } - else if (dx1 == 0 && dx2 != 0) + + if (dx1 == 0 && dx2 != 0) { const float along = (x1 - x3) / dx2; intersectionX = x1; @@ -126,7 +128,8 @@ namespace PathStrokeHelpers return along >= 0 && along <= 1.0f; } - else if (dx2 == 0 && dx1 != 0) + + if (dx2 == 0 && dx1 != 0) { const float along = (x3 - x1) / dx1; intersectionX = x3; @@ -147,33 +150,31 @@ namespace PathStrokeHelpers distanceBeyondLine1EndSquared = 0.0f; return false; } - else + + const float along1 = ((y1 - y3) * dx2 - (x1 - x3) * dy2) / divisor; + + intersectionX = x1 + along1 * dx1; + intersectionY = y1 + along1 * dy1; + + if (along1 >= 0 && along1 <= 1.0f) { - const float along1 = ((y1 - y3) * dx2 - (x1 - x3) * dy2) / divisor; + const float along2 = ((y1 - y3) * dx1 - (x1 - x3) * dy1) / divisor; - intersectionX = x1 + along1 * dx1; - intersectionY = y1 + along1 * dy1; - - if (along1 >= 0 && along1 <= 1.0f) + if (along2 >= 0 && along2 <= 1.0f) { - const float along2 = ((y1 - y3) * dx1 - (x1 - x3) * dy1); - - if (along2 >= 0 && along2 <= divisor) - { - distanceBeyondLine1EndSquared = 0.0f; - return true; - } + distanceBeyondLine1EndSquared = 0.0f; + return true; } - - distanceBeyondLine1EndSquared = along1 - 1.0f; - distanceBeyondLine1EndSquared *= distanceBeyondLine1EndSquared; - distanceBeyondLine1EndSquared *= (dx1 * dx1 + dy1 * dy1); - - if (along1 < 1.0f) - distanceBeyondLine1EndSquared = -distanceBeyondLine1EndSquared; - - return false; } + + distanceBeyondLine1EndSquared = along1 - 1.0f; + distanceBeyondLine1EndSquared *= distanceBeyondLine1EndSquared; + distanceBeyondLine1EndSquared *= (dx1 * dx1 + dy1 * dy1); + + if (along1 < 1.0f) + distanceBeyondLine1EndSquared = -distanceBeyondLine1EndSquared; + + return false; } intersectionX = x2;