poj 3525 多边形内核,缩进

The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.

In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.

Input

The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows.

n  
x1 y1
  
xn yn

Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.

n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x- and y-coordinates of the n vertices. Line segments (xi, yi)–(xi+1, yi+1) (1 ≤ in − 1) and the line segment (xn, yn)–(x1, y1) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive.

You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one.

The last dataset is followed by a line containing a single zero.

Output

For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10−5). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

Sample Input
4
0 0
10000 0
10000 10000
0 10000
3
0 0
10000 0
7000 1000
6
0 40
100 20
250 40
250 70
100 90
0 70
3
0 0
10000 10000
5000 5001
0
Sample Output
5000.000000
494.233641
34.542948
0.353553


题意:

给出一个凸包,求这个凸包里面某个点到边最远的距离

题解:

可以转化为内接圆的最大半径

此时我们可以将多边形缩小R,缩小后得到的刚好只有一个内核则这个R就是最远距离



#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std;

#define eps 1e-8
const int MAXN=2000;

int n;
int cCnt,curCnt;///最终切割得到的多边形的顶点数、暂存顶点个数

struct point
{
    double x,y;
};
point points[MAXN],p[MAXN],q[MAXN];///初始多边形顶点(顺时针)、最终切割后多边形顶点、暂存顶点

void getline(point x,point y,double &a,double &b,double &c) ///两点x、y确定一条直线a、b、c为其系数
{
    a=y.y-x.y;
    b=x.x-y.x;
    c=y.x*x.y-x.x*y.y;
}
void initial()
{
    for(int i=1; i<=n; i++)
        p[i]=points[i];
    p[n+1]=p[1];
    p[0]=p[n];
    cCnt=n;
}
point intersect(point x,point y,double a,double b,double c)///点x、y所在直线与ax+by+c=0的交点
{
    double u=fabs(a*x.x+b*x.y+c);
    double v=fabs(a*y.x+b*y.y+c);
    point pt;
    pt.x=(x.x*v+y.x*u)/(u+v);
    pt.y=(x.y*v+y.y*u)/(u+v);
    return pt;
}
void cut(double a,double b,double c)
{
    curCnt=0;
    for(int i=1; i<=cCnt; i++)
    {
        if(a*p[i].x+b*p[i].y+c>=0)///点代入线都大于0,说明此点都在这条直线某一边,不用切
            q[++curCnt]=p[i];
        else{
            if(a*p[i-1].x+b*p[i-1].y+c>0)///如果p[i-1]在直线的右侧的话
                q[++curCnt]=intersect(p[i],p[i-1],a,b,c);
            if(a*p[i+1].x+b*p[i+1].y+c>0)
                q[++curCnt]=intersect(p[i],p[i+1],a,b,c);
        }
    }
    for(int i=1; i<=curCnt; i++)
        p[i]=q[i];
    p[curCnt+1]=q[1];
    p[0]=p[curCnt];
    cCnt=curCnt;
}
int deal(double r)
{
    initial();
    for(int i=1;i<=n;i++)///向内缩进距离 r
    {
        point ta,tb,tt;
        tt.x=points[i+1].y-points[i].y;
        tt.y=points[i].x-points[i+1].x;
        double k=r/sqrt(tt.x * tt.x + tt.y * tt.y);
        tt.x=tt.x*k;
        tt.y=tt.y*k;
        ta.x=points[i].x + tt.x;
        ta.y=points[i].y + tt.y;
        tb.x=points[i+1].x + tt.x;
        tb.y=points[i+1].y + tt.y;
        double a,b,c;
        getline(ta,tb,a,b,c);
        cut(a,b,c);
    }
    return curCnt;
}
double solve()
{
    double left=0,right=100000000,mid;
    while(left+eps<right)
    {
        mid=(left+right)/2.0;
        if(deal(mid)) left=mid;
        else right=mid;
    }

    return left;
}
void GuiZhengHua()
{
    ///规整化方向,逆时针变顺时针,顺时针变逆时针
    for(int i=1;i<=n;i++) q[i]=points[n-i+1];
    for(int i=1;i<=n;i++) points[i]=q[i];
}
int main()
{
    freopen("in.txt","r",stdin);
    while(scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
            scanf("%lf%lf",&points[i].x,&points[i].y);
        GuiZhengHua();
        points[n+1]=points[1];
        printf("%.6lf\n",solve());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值