[POJ3525]Most Distant Point from the Sea(二分+半平面交)

题目:

我是超链接

题意:

给出一个多边形,求里面一个点,其距离离多边形的边界最远,输出距离。

题解:

二分答案,相当于是把所有直线往里平移mid长,看看这个半平面能不能有交点
这个平移嘛。。可以找到一个垂直的方向(rotate),然后按照长度往里平移就好了

代码:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-9;
const double pi=acos(-1.0);
int dcmp(double x)
{
    if (x<=eps && x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct po
{
    double x,y;
    po(double X=0,double Y=0){x=X;y=Y;}
}d[105],p[105];
struct Line
{
    po x,v;double ang;
    Line(po X=po(0,0),po V=po(0,0)){x=X;v=V;ang=atan2(v.y,v.x);}
}L[105],q[105];int n,head,tail;
po rotate(po x,double v){return po(x.x*cos(v)-x.y*sin(v),x.x*sin(v)+x.y*cos(v));}
po operator +(po x,po y){return po(x.x+y.x,x.y+y.y);}
po operator -(po x,po y){return po(x.x-y.x,x.y-y.y);}
po operator *(po x,double y){return po(x.x*y,x.y*y);}
double cj(po x,po y){return x.x*y.y-x.y*y.x;}
double dot(po x,po y){return x.x*y.x+x.y*y.y;}
double Len(po x){return sqrt(dot(x,x));}
bool Onleft(Line x,po y){return dcmp(cj(x.v,y-x.x))>0;}
po jd(Line a,Line b)
{
    po u=a.x-b.x; 
    double t=cj(b.v,u)/cj(a.v,b.v);
    return a.x+a.v*t;
}
int cmp(Line x,Line y){return x.ang<y.ang;}
bool halfp()
{
    sort(L+1,L+n+1,cmp);
    head=tail=1; q[1]=L[1];
    for (int i=2;i<=n;i++)
    {
        while (head<tail && !Onleft(L[i],p[tail-1])) tail--;
        while (head<tail && !Onleft(L[i],p[head])) head++;
        q[++tail]=L[i];
        if (dcmp(cj(q[tail].v,q[tail-1].v))==0)
        {
            tail--;
            if (Onleft(q[tail],L[i].x)) q[tail]=L[i];
        }
        if (head<tail) p[tail-1]=jd(q[tail],q[tail-1]);
    }
    while (head<tail && !Onleft(q[head],p[tail-1])) tail--;
    if (tail-head<=1) return 0;return 1; 
}
bool check(double mid)
{
    for (int i=1;i<=n;i++) 
    {
        po a=d[i],b=d[i%n+1];
        po w=rotate(b-a,pi/2);
        w=w*(mid/Len(w));
        a=a+w; b=b+w;
        L[i]=Line(a,b-a);
    }
    return halfp();
}
int main()
{
    while (scanf("%d",&n) && n)
    {
        double x,y,l=0,r=0;
        for (int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x,&y);
            r=max(r,max(x,y));
            d[i]=po(x,y);
        }
        while (dcmp(r-l))
        {
            double mid=(l+r)/2;
            if (check(mid)) l=mid;else r=mid;
        }
        printf("%.6lf\n",l);
    }   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值