您的位置:首页 > 其它

【计算几何——直线相交】POJ 1039

2012-03-29 21:22 399 查看
黑书例题

#define eps 1e-9
#define pi acos(-1.0)

struct Point{
double x,y;
};
int dbl_cmp(double d){
if (fabs(d)<eps)
return 0;
return d>0?1:-1;
}
double det(double x1,double y1,double x2,double y2){
return x1*y2-x2*y1;
}
double cross(Point a,Point b,Point c){
return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
int segcross_simple(Point a,Point b,Point c,Point d){
if (dbl_cmp(cross(a,b,c))*dbl_cmp(cross(a,b,d))<=0)
return 1;//相交
return 0;//不相交
}
int segcross(Point a,Point b,Point c,Point d,Point &p){//求交点
double s1,s2;
int d1,d2;
s1=cross(a,b,c);
s2=cross(a,b,d);
d1=dbl_cmp(s1);
d2=dbl_cmp(s2);
//规范相交求交点
if ((d1^d2)==-2){
p.x=(c.x*s2-d.x*s1)/(s2-s1);
return 1;
}
if (d2==0){
p.x=d.x;
return 1;
}
if (d1==0){
p.x=c.x;
return 1;
}
return 0;
}
int main(){
Point up[22],bottom[22],p;
int n,i,j,k;
bool all;
double ans;
while (scanf("%d",&n) && n){
all=false;
ans=MIN;
for (i=1;i<=n;i++){
scanf("%lf%lf",&up[i].x,&up[i].y);
bottom[i].x=up[i].x;
bottom[i].y=up[i].y-1.0;
}
for (i=1;i<=n && !all;i++){
for (j=1;j<=n;j++){
for (k=1;k<=n;k++)
if (segcross_simple(up[i],bottom[j],up[k],bottom[k])==0)
break;
if (k>n){//都没有交点
all=true;
break;
}
if (k>max(i,j)){
segcross(up[i],bottom[j],up[k],up[k-1],p);
ans=max(ans,p.x);
segcross(up[i],bottom[j],bottom[k],bottom[k-1],p);
ans=max(ans,p.x);
}
}
}
if (all)
printf("Through all the pipe.\n");
else
printf("%.2lf\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: