UVALive 3890 Most Distant Point from the Sea(凸包最大内接圆)

一个n个点的凸多边形,求多边形中离多边形边界最远的距离。实际上就是求凸包最大内接圆的半径。

利用半平面交求解,每次二分枚举半径d,然后将凸包每条边所代表的半平面沿其垂直单位法向量平移d,看所有平移后的半平面的交集是否为空。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define FF(i, a, b) for(int i=a; i=b; i--)
#define REP(i, n) for(int i=0; i 0; }
//直线交点
Point GetIntersection(Line a, Line b)
{
    Vector u = a.p-b.p;
    double t = Cross(b.v, u) / Cross(a.v, b.v);
    return a.p + a.v*t;
}

const int maxn = 200;
Point p[maxn], poly[maxn];
Line L[maxn];
Vector v[maxn], v2[maxn];
int n;

//半平面交
Point pp[maxn];
Line qq[maxn];
int HalfplaneIntersection(Line* L, int n, Point* poly)
{
    sort(L, L+n);
    int first, last;

    qq[first=last=0] = L[0];
    FF(i, 1, n)
    {
        while(first < last && !onLeft(L[i], pp[last-1])) last--;
        while(first < last && !onLeft(L[i], pp[first])) first++;
        qq[++last] = L[i];
        if(fabs(Cross(qq[last].v, qq[last-1].v)) < eps)
        {
            last--;
            if(onLeft(qq[last], L[i].p)) qq[last] = L[i];
        }
        if(first < last) pp[last-1] = GetIntersection(qq[last-1], qq[last]);
    }
    while(first < last && !onLeft(qq[first], pp[last-1])) last--;
    if(last-first <= 1) return 0;
    pp[last] = GetIntersection(qq[last], qq[first]);

    int m = 0;
    FF(i, first, last+1) poly[m++] = pp[i];
    return m;
}


int main()
{
    while(scanf("%d", &n), n)
    {
        REP(i, n) scanf("%lf%lf", &p[i].x, &p[i].y);
        REP(i, n)
        {
            v[i] = p[(i+1)%n]-p[i];
            v2[i] = Normal(v[i]);
        }
        double l=0, r=20000, mid;
        while(r - l > eps)
        {
            mid = (l+r) / 2.0;
            REP(i, n) L[i] = Line(p[i]+v2[i]*mid, v[i]);
            int m = HalfplaneIntersection(L, n, poly);
            if(!m) r=mid; else l=mid;
        }
        printf("%.6f\n", l);
    }
    return 0;
}


你可能感兴趣的:(计算几何)