线段相交

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 struct point
 7 {
 8  double x,y;
 9 };
10 point p[4];
11 int kuai()
12 {
13  if(min(p[0].x,p[1].x)<=max(p[2].x,p[3].x)&&
14   max(p[0].x,p[1].x)>=min(p[2].x,p[3].x)&&
15   min(p[0].y,p[1].y)<=max(p[2].y,p[3].y)&&
16   max(p[0].y,p[1].y)>=min(p[2].y,p[3].y))
17   return 1;
18  return 0;
19 }
20 double cha(point p0,point p1,point p2)
21 {
22  return (p2.y-p0.y)*(p1.x-p0.x)-(p2.x-p0.x)*(p1.y-p0.y);
23 }
24 int kua()
25 {
26   if(cha(p[2],p[3],p[1])*cha(p[2],p[3],p[0])<=0&&cha(p[0],p[1],p[2])*cha(p[0],p[1],p[3])<=0)
27     return 1;
28   return 0;
29 }
30 int main()
31 {
32  int i,n;
33  //freopen("in.txt","r",stdin);
34  cin>>n;
35  while(n--)
36  {
37   for(i=0;i<4;i++)
38    cin>>p[i].x>>p[i].y;
39   if(kua())
40     cout<<"Yes"<<endl;
41   else
42     cout<<"No"<<endl;
43  }
44 }
原文地址:https://www.cnblogs.com/a1225234/p/4817672.html