According to this property, it is possible to determine whether the point P2 is on the left or the right side of the line, which is an important property to determine if the two segments intersect.
This is a case where the two segments intersect, and the end of one segment is on the other.
This is the principle of judging whether the two segments intersect.
1#include <iostream>2#include <algorithm>3#include <cmath>4 using namespacestd;5 6 structPoint {7 Doublex, y;8 };9 Ten BOOLSegmentsintersect (Point P1, point P2, point p3,point p4) { One DoubleD1 =direction (P1, P2, p3); A DoubleD2 =direction (P1, P2, p4); - DoubleD3 =direction (P3, P4, p1); - DoubleD4 =direction (P3, P4, p2); the - if(D1 >0&& D2 <0|| D1 <0|| D2>0|| D3>0&& D4 <0|| D3 <0&& d4>0) - return true; - if(Fabs (D1) <= 1e-9&& onsegment (P1, p2, p3))return true; + if(Fabs (D2) <= 1e-9&& onsegment (P1, P2, p4))return true; - if(Fabs (D3) <= 1e-9&& onsegment (P3, P4, p1))return true; + if(Fabs (D4) <= 1e-9&& onsegment (P3, P4, p2))return true; A return false; at } - - //This is the judgment P3 is on which side of the segment p1p2 - Doubledirection (Point P1, point P2, point p3) { - return(p2.x-p1.x) * (P3.Y-P2.Y)-(p3.x-p2.x) * (P2.Y-p1.y); - } in - //This is to determine whether the point P3 is within a rectangle with p1p2 diagonal to BOOLonsegment (Point P1, point P2, point p3) { + if(p3.x >= min (p1.x, p2.x) && p3.x <= Max (p1.x, p2.x) && -P3.y >= min (p1.y, p2.y) && p3.y <=Max (P1.Y, p2.y)) the return true; * return false; $}
Here's a more detailed look at the geometry of the introduction to algorithms
This method is somewhat understood.
Determine if two segments intersect