现在的位置: 首页 > 综合 > 正文

杭电1086

2013年08月26日 ⁄ 综合 ⁄ 共 1157字 ⁄ 字号 评论关闭

hdu1086判断两线段相交的模板

# include<stdio.h>
# include<string.h>
struct Node{
 double x,y;
}point1[105],point2[105];
double Cross(Node p1,Node p2,Node p3)
{
 return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double Point(Node p1,Node p2,Node p3)
{
 return (p1.x-p3.x)*(p2.x-p3.x) - (p1.y-p3.y)*(p2.y-p3.y);
}
int Segments_intersect(Node p1_start,Node p1_end,Node p2_start,Node p2_end)
{
 double d1,d2,d3,d4;
    d1=Cross(p1_start,p1_end,p2_start);
    d2=Cross(p1_start,p1_end,p2_end);
    d3=Cross(p2_start,p2_end,p1_start);
    d4=Cross(p2_start,p2_end,p1_end);
    if(d1*d2<0 && d3*d4<0) return 1;
    else if(d1==0 && Point(p1_start,p1_end,p2_start)<=0) return 1;
    else if(d2==0 && Point(p1_start,p1_end,p2_end)<=0) return 1;
    else if(d3==0 && Point(p2_start,p2_end,p1_start)<=0) return 1;
    else if(d4==0 && Point(p2_start,p2_end,p1_end)<=0) return 1;
    else return 0;
}
int main()
{
 int n,i,j,count,ans;
 while(scanf("%d",&n)!=EOF && n)
 {
  for(i=1;i<=n;i++)
   scanf("%lf%lf%lf%lf",&point1[i].x,&point1[i].y,&point2[i].x,&point2[i].y);
  count=0;
  for(i=1;i<n;i++)
  {
   for(j=i+1;j<=n;j++)
   {
    ans=Segments_intersect(point1[i],point2[i],point1[j],point2[j]);
    if(ans==1) count++;
   }
  }
  printf("%d\n",count);
 }
 return 0;
}

抱歉!评论已关闭.