From 3e6268ec5c31ef1cf975b3e80ae214ced1edce0a Mon Sep 17 00:00:00 2001 From: Dany Pignoux Date: Wed, 28 Apr 2021 09:49:28 +0200 Subject: [PATCH] Fix collision for parallel line/line https://github.com/jeffThompson/CollisionDetection/issues/14 --- CodeExamples/LineLine/LineLine.pde | 31 ++++++++-------- CodeExamples/LineLine/web-export/LineLine.pde | 33 +++++++++-------- Website/line-line.php | 37 ++++++++++--------- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/CodeExamples/LineLine/LineLine.pde b/CodeExamples/LineLine/LineLine.pde index 2263c42..a12a928 100644 --- a/CodeExamples/LineLine/LineLine.pde +++ b/CodeExamples/LineLine/LineLine.pde @@ -56,25 +56,26 @@ void draw() { boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { // Check if first line points collides with the line 2 if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) { - return true; - } + // calculate the direction of the lines + float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - // calculate the distance to intersection point - float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + // if uA and uB are between 0-1, lines are colliding + if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { - // if uA and uB are between 0-1, lines are colliding - if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { - - // optionally, draw a circle where the lines meet - float intersectionX = x1 + (uA * (x2-x1)); - float intersectionY = y1 + (uA * (y2-y1)); - fill(255,0,0); - noStroke(); - ellipse(intersectionX,intersectionY, 20,20); - + // optionally, draw a circle where the lines meet + float intersectionX = x1 + (uA * (x2-x1)); + float intersectionY = y1 + (uA * (y2-y1)); + fill(255,0,0); + noStroke(); + ellipse(intersectionX,intersectionY, 20,20); + + return true; + } + } else { return true; } + return false; } diff --git a/CodeExamples/LineLine/web-export/LineLine.pde b/CodeExamples/LineLine/web-export/LineLine.pde index ecaeadf..2ec5f94 100644 --- a/CodeExamples/LineLine/web-export/LineLine.pde +++ b/CodeExamples/LineLine/web-export/LineLine.pde @@ -55,25 +55,26 @@ void draw() { boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { // Check if first line points collides with the line 2 if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) { - return true; - } + // calculate the direction of the lines + float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + + // if uA and uB are between 0-1, lines are colliding + if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { - // calculate the direction of the lines - float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - - // if uA and uB are between 0-1, lines are colliding - if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { - - // optionally, draw a circle where the lines meet - float intersectionX = x1 + (uA * (x2-x1)); - float intersectionY = y1 + (uA * (y2-y1)); - fill(255,0,0); - noStroke(); - ellipse(intersectionX,intersectionY, 20,20); - + // optionally, draw a circle where the lines meet + float intersectionX = x1 + (uA * (x2-x1)); + float intersectionY = y1 + (uA * (y2-y1)); + fill(255,0,0); + noStroke(); + ellipse(intersectionX,intersectionY, 20,20); + + return true; + } + } else { return true; } + return false; } diff --git a/Website/line-line.php b/Website/line-line.php index 039e722..ba6d313 100644 --- a/Website/line-line.php +++ b/Website/line-line.php @@ -21,7 +21,7 @@

To fix parallel line problem described here:

if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) {
-    return true;
+    // ... Test above code
 }
 
@@ -75,26 +75,27 @@ boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { // Check if first line points collides with the line 2 if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) { + // calculate the direction of the lines + float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + + // if uA and uB are between 0-1, lines are colliding + if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { + + // optionally, draw a circle where the lines meet + float intersectionX = x1 + (uA * (x2-x1)); + float intersectionY = y1 + (uA * (y2-y1)); + fill(255,0,0); + noStroke(); + ellipse(intersectionX,intersectionY, 20,20); + + return true; + } + } else { return true; } - // calculate the distance to intersection point - float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - - // if uA and uB are between 0-1, lines are colliding - if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { - - // optionally, draw a circle where the lines meet - float intersectionX = x1 + (uA * (x2-x1)); - float intersectionY = y1 + (uA * (y2-y1)); - fill(255,0,0); - noStroke(); - ellipse(intersectionX,intersectionY, 20,20); - - return true; - } - return false; + return true; }