Line Segment intersection template, line segment Intersection
Whether there is an intersection between P1P2 and Q1Q2. If P1P2 and Q1Q2 must be at the same time, Point P1 and point P2 must be on both sides of the online segment Q1Q2 (similarly, the online segment P1P2 must be on both sides of the Q1 and point Q2 segments ). Use the cross product of a straight line to solve the problem. That is:
(P1-Q1) * (Q2-Q1) * (Q2-Q1) * (P2-Q1)> = 0 and (Q1-P1) * (P2-P1) * (P2-P1) * (Q2-P1)> = 0 (ps: "×" indicates the cross multiplication)
# Include <iostream> # include <cstdio> # include <cstring> using namespace std; const int maxn = 100; struct Node {double x; double y ;}; double Product (Node a, Node B) // Cross Product {return. x * B. y-b.x *. y;} bool Intersect (Node P1, Node P2, Node Q1, Node Q2) {Node a [4], B [4]; a [0]. x = P1.x-Q1.x; a [0]. y = P1.y-Q1.y; B [0]. x = Q2.x-Q1.x; B [0]. y = Q2.y-Q1.y; a [1] = B [0]; B [1]. x = P2.x-Q1.x; B [1]. y = P2.y-Q1.y; a [2]. x = Q1.x-P1.x; a [2]. y = Q1.y-P1.y; B [2]. x = P2.x-P1.x; B [2]. y = P2.y-P1.y; a [3] = B [2]; B [3]. x = Q2.x-P1.x; B [3]. y = Q2.y-P1.y; if (Product (a [0], B [0]) * Product (a [1], B [1])> = 0 & Product (a [2], B [2]) * Product (a [3], B [3])> = 0) return true; // (P1-Q1) x (Q2-Q1) * (Q2-Q1) * (P2-Q1)> = 0 & (Q1-P1) x (P2-P1) * (P2-P1) * (Q2-P1) * ()> = 0 return false;} int main () {// freopen ("in.txt", "r", stdin); int n; Node A1, A2, B1, B2; scanf ("% lf", & A1.x, & A1.y, & A2.x, & A2.y, & B1.x, & B1.y, & B2.x, & B2.y); if (Intersect (A1, A2, B1, B2) printf ("YES \ n"); else printf ("NO \ n "); return 0 ;}