#include<stdio.h>
struct node
{
double x,y;
}start[101],end[101];
double direction(struct node p1,struct node p2, struct node p)
{
return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x);
}
bool is_on_segment(struct node p1,struct node p2,struct node p)
{
double min,max;
if(p1.x<p2.x)
{
min=p1.x;
max=p2.x;
}
else
{
min=p2.x;
max=p1.x;
}
if(p.x>=min&&p.x<=max)
return 1;
else
return 0;
}
bool intersect_segment(struct node p1,struct node p2,struct node p3,struct node p4)
{
double d1=direction(p1,p2,p3);
double d2=direction(p1,p2,p4);
double d3=direction(p3,p4,p1);
double d4=direction(p3,p4,p2);
if(d1*d2<0&&d3*d4<0)
return 1;
if(d1==0&&is_on_segment(p1,p2,p3))
return 1;
if(d2==0&&is_on_segment(p1,p2,p4))
return 1;
if(d3==0&&is_on_segment(p3,p4,p1))
return 1;
if(d1==0&&is_on_segment(p3,p4,p2))
return 1;
return 0;
}
int main()
{
int i,j,num,n;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&start[i].x,&start[i].y,&end[i].x,&end[i].y);
num=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(intersect_segment(start[i],end[i],start[j],end[j]))
num++;
}
}
printf("%d\n",num);
}
return 0;
}
hdu 1086
最新推荐文章于 2020-02-13 16:12:18 发布