#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
double x;
double y;
};
node a,b,c,d;
node p1[1005],p2[1005];
bool pd(node a,node b,node c,node d)
{
if(min(a.y,b.y)<=max(c.y,d.y)&&min(d.x,c.x)<=max(a.x,b.x)&&min(c.y,d.y)<=max(a.y,b.y)&&min(a.x,b.x)<=max(c.x,d.x))
{
double u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); //c 以ab为线段,将c点代入
double v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y); //d 以ab为线段,将d点代入
double w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y); // a 以cd为线段将a点代入
double z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
return (u*v<=0.00000001&&w*z<=0.00000001);
//u*v代表以ab为线段,将cd代入如果小于0.00000001则cd两点在ab线段两侧,必相交.
//w*z代表以cd为线段,将ab代入如果小于0.00000001则cd两点在cd线段两侧,必相交.
}
return false;
}
int main()
{
int n,sum=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf %lf %lf %lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(pd(p1[i],p2[i],p1[j],p2[j]))
sum++;
}
}
cout<<sum<<endl;
}
转载于:https://www.cnblogs.com/ACM-Lxw/p/10905668.html