瀏覽代碼

Cleanup, attribution

Sam Jaffe 6 年之前
父節點
當前提交
0493ece762
共有 1 個文件被更改,包括 6 次插入14 次删除
  1. 6 14
      math/src/common.cpp

+ 6 - 14
math/src/common.cpp

@@ -78,28 +78,20 @@ namespace math {
   }
   
   bool intersects(dim2::line const & lhs, dim2::line const & rhs) {
-    // Find the four orientations needed for general and
-    // special cases
+    // https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
+    // Find the four orientations needed for general and special cases
     orientation o1 = orient(lhs.first, lhs.second, rhs.first);
     orientation o2 = orient(lhs.first, lhs.second, rhs.second);
     orientation o3 = orient(rhs.first, rhs.second, lhs.first);
     orientation o4 = orient(rhs.first, rhs.second, lhs.second);
     
-    // General case
+    // General Case: Lines cross through each other
     if (o1 != o2 && o3 != o4)
       return true;
     
-    // Special Cases
-    // p1, q1 and p2 are colinear and p2 lies on segment p1q1
-    if (o1 == colinear && contains(lhs, rhs.second)) { return true; }
-    // p1, q1 and q2 are colinear and q2 lies on segment p1q1
-    if (o2 == colinear && contains(lhs, rhs.second)) { return true; }
-    // p2, q2 and p1 are colinear and p1 lies on segment p2q2
-    if (o3 == colinear && contains(rhs, lhs.first)) { return true; }
-    // p2, q2 and q1 are colinear and q1 lies on segment p2q2
-    if (o4 == colinear && contains(rhs, lhs.second)) { return true; }
-    
-    return false; // Doesn't fall in any of the above cases
+    // Special Cases: one of the points exists on the other line
+    return contains(lhs, rhs.second) || contains(lhs, rhs.second) ||
+        contains(rhs, lhs.first) || contains(rhs, lhs.second);
   }
   
   bool intersects(dim2::line const & lhs, dim2::circle const & rhs) {