hdu 1086

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值