hdu1086 You can Solve a Geometry Problem too

#include <stdio.h>
//(abXac)(abXad)<0 && (cdXca)(cdXcb)<0
struct segment
{
	double x1,y1,x2,y2;
}seg[102];

int Judge(int i,int j)
{
	double a,b,c,d,x,y,a1,b1;
	a=seg[i].x1-seg[j].x1;
	b=seg[i].y1-seg[j].y1;
	a1=seg[i].x2-seg[j].x1;
	b1=seg[i].y2-seg[j].y1;
	c=seg[j].x2-seg[j].x1;
	d=seg[j].y2-seg[j].y1;
	x=a*d-b*c;
	y=a1*d-b1*c;
	if(x*y<=0)
		return 1;
	else
		return 0;
}

int main()
{
	int n,i,j,count;
	while(scanf("%d",&n)&&n)
	{
		count=0;
		for (i=1;i<=n;i++)
		{
			scanf("%lf %lf %lf %lf",&seg[i].x1,&seg[i].y1,&seg[i].x2,&seg[i].y2);
		}
		for (i=1;i<n;i++)
		{
			for(j=i+1;j<=n;j++)
				if(Judge(i,j)&&Judge(j,i))
					count++;
		}
		printf("%d\n",count);
	}
	return 0;
}

你可能感兴趣的:(hdu1086 You can Solve a Geometry Problem too)