Total Submission(s): 7658 Accepted Submission(s): 3745
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
A test case starting with 0 terminates the input and this test case is not to be processed.
/*
hdoj 1086
就是线段相交的个数
*/
#include<iostream>
#include<stdio.h>
using namespace std;
struct point{
double x,y;
};
point point1[101],point2[101];
double xmult(point a,point b,point c)//大于零代表a,b,c左转
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
bool OnSegment(point a,point b,point c)//a,b,c共线时有效
{
return c.x>=min(a.x,b.x)&&c.x<=max(a.x,b.x)&&c.y>=min(a.y,b.y)&&c.y<=max(a.y,b.y);
}
int cross(point a,point b,point c,point d){//判断ab 与cd是否相交
double d1,d2,d3,d4;
d1=xmult(c,d,a);
d2=xmult(c,d,b);
d3=xmult(a,b,c);
d4=xmult(a,b,d);
if(d1*d2<0&&d3*d4<0) return 1;
else if(d1==0&&OnSegment(c,d,a)) return 1;
else if(d2==0&&OnSegment(c,d,b)) return 1;
else if(d3==0&&OnSegment(a,b,c)) return 1;
else if(d4==0&&OnSegment(a,b,d)) return 1;
return 0;
}
int main()
{
int n,i,j,sum,f;
// freopen("test.txt","r",stdin);
while(scanf("%d",&n)&&n)
{
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&point1[i].x,&point1[i].y,&point2[i].x,&point2[i].y);
sum=0;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
f=cross(point1[i],point2[i],point1[j],point2[j]);//2条线段
if(f==1) sum++;
}
printf("%d\n",sum);
}
return 0;
}