两线段是否相交模板

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct point
 4 {
 5     int x,y;
 6 }p1,p2,p3,p4;
 7 int cross(const point &a,const point &b,const point &o)
 8 {
 9     return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
10 }
11 bool work(const point &a,const point &b,const point &c,const point &d)
12 {
13     return ((cross(a,d,c)*cross(d,b,c)>=0)&&(cross(c,b,a)*cross(b,d,a)>=0)
14             &&(max(c.x,d.x)>=min(a.x,b.x))&&(max(a.x,b.x)>=min(c.x,d.x))
15             &&(max(c.y,d.y)>=min(a.y,b.y))&&(max(a.y,b.y)>=min(c.y,d.y)));
16 }
17 int main()
18 {
19     while(scanf("%d%d%d%d%d%d%d%d",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&p4.x,&p4.y)!=EOF)
20     {
21         if(work(p1,p2,p3,p4)) puts("YES");
22         else puts("NO");
23     }
24     return 0;
25 }
View Code
原文地址:https://www.cnblogs.com/CJLHY/p/7899625.html