#include<cstdio>
#include<iostream>
#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)
using namespace std;
struct point
{
double x,y;
};
struct segment
{
point a,b;
};
segment s[1000];
double Direction(point i,point j,point k)
{
double tempx1=k.x-i.x;
double tempy1=k.y-i.y;
double tempx2=j.x-i.x;
double tempy2=j.y-i.y;
return (tempx1*tempy2-tempx2*tempy1);
}
bool On(point i,point j,point k)
{
if((min(i.x,j.x)<=k.x && k.x<=max(i.x,j.x)) && (min(i.y,j.y)<=k.y && k.y<=max(i.y,j.y)))
return true;
else
return false;
}
bool intersect(point a,point b,point c,point d)
{
double d1,d2,d3,d4;
d1=Direction(c,d,a);
d2=Direction(c,d,b);
d3=Direction(a,b,c);
d4=Direction(a,b,d);
if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0)))
return true;
else if(d1==0 && On(c,d,a))
return true;
else if(d2==0 && On(c,d,b))
return true;
else if(d3==0 && On(a,b,c))
return true;
else if(d4==0 && On(a,b,d))
return true;
else
return false;
}
int main()
{
int i,n,j;
double x1,y1,x2,y2;
while(~scanf("%d",&n) &&n)
{
int cnt=0;
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
s[i].a.x=x1;
s[i].a.y=y1;
s[i].b.x=x2;
s[i].b.y=y2;
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(intersect(s[i].a,s[i].b,s[j].a,s[j].b))
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
转载于:https://www.cnblogs.com/ray007great/archive/2013/05/07/3064957.html
相关资源:判断两线段是否相交,相交求交点