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

黑书例题

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





















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值