diff OrthancStone/Sources/Toolbox/GeometryToolbox.cpp @ 2156:340bde744884

added DebugDrawing2D and GeometryToolbox::IntersectLineAndSegment()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Sep 2024 22:14:36 +0200
parents 16c01cc201e7
children
line wrap: on
line diff
--- a/OrthancStone/Sources/Toolbox/GeometryToolbox.cpp	Fri Sep 27 15:59:17 2024 +0200
+++ b/OrthancStone/Sources/Toolbox/GeometryToolbox.cpp	Fri Sep 27 22:14:36 2024 +0200
@@ -565,5 +565,78 @@
         return false;
       }
     }
+
+
+    static bool SolveLineIntersectionSystem(double& x,
+                                            double& y,
+                                            double& s,
+                                            double& t,
+                                            double x1, double y1,
+                                            double x2, double y2,
+                                            double x3, double y3,
+                                            double x4, double y4)
+    {
+      // https://en.wikipedia.org/wiki/Intersection_(geometry)#Two_line_segments
+      const double a1 = x2 - x1;
+      const double b1 = -x4 + x3;
+      const double c1 = x3 - x1;
+      const double a2 = y2 - y1;
+      const double b2 = -y4 + y3;
+      const double c2 = y3 - y1;
+
+      const double denominator = a1 * b2 - a2 * b1;
+      if (LinearAlgebra::IsCloseToZero(denominator))
+      {
+        return false;
+      }
+      else
+      {
+        // This is Cramer's rule
+        s = (c1 * b2 - c2 * b1) / denominator;
+        t = (a1 * c2 - a2 * c1) / denominator;
+        x = x1 + s * (x2 - x1);
+        y = y1 + s * (y2 - y1);
+        return true;
+      }
+    }
+
+
+    bool IntersectTwoLines(double& x,
+                           double& y,
+                           double ax1,
+                           double ay1,
+                           double ax2,
+                           double ay2,
+                           double bx1,
+                           double by1,
+                           double bx2,
+                           double by2)
+    {
+      double s, t;
+      return SolveLineIntersectionSystem(x, y, s, t, ax1, ay1, ax2, ay2, bx1, by1, bx2, by2);
+    }
+
+
+    bool IntersectLineAndSegment(double& x,
+                                 double& y,
+                                 double lineX1,
+                                 double lineY1,
+                                 double lineX2,
+                                 double lineY2,
+                                 double segmentX1,
+                                 double segmentY1,
+                                 double segmentX2,
+                                 double segmentY2)
+    {
+      double s, t;
+      if (SolveLineIntersectionSystem(x, y, s, t, lineX1, lineY1, lineX2, lineY2, segmentX1, segmentY1, segmentX2, segmentY2))
+      {
+        return (t >= 0 && t <= 1);
+      }
+      else
+      {
+        return false;
+      }
+    }
   }
 }