1. 程式人生 > >POJ 3525(計算幾何+凸多邊形最大內切圓)

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 ≤ i ≤ n − 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

題目題意:題目給我們一個凸多邊形,讓我們求出最大內切圓的半徑。

題目分析:凸多邊形可以看出凸多邊形的邊(直線)的半平面的交,其實只要這個半平面的交存在,那麼這個內切圓就存在,當半平面的交不存在的時候,內切圓就不存在了。(最後變成一個點)

因為是凸多邊形的原因,我們可以通過判斷凸多邊形的核是否存在來判斷。

我們平移凸多邊形的各個邊,直到核不存在了,平移的距離就是最大內切圓的半徑。

程式碼如下:(半平面交的程式碼是模板程式碼)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

const double eps=1e-8;
const double inf=1e9;
const int MAXN=210;
int m;//儲存多邊形的點數
double r;//儲存內移距離
int cCnt,curCnt;//此時cCnt為最終切割得到的多邊形的頂點數、暫存頂點個數
struct point
{
    double x,y;
};
point points[MAXN],p[MAXN],q[MAXN];//讀入的多邊形的頂點(順時針)、p為存放最終切割得到的多邊形頂點的陣列、暫存核的頂點
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 <= m; ++i)p[i] = points[i];
    p[m+1] = p[1];
    p[0] = p[m];
    cCnt = m;
}
point intersect(point x,point y,double a,double b,double c)
{
    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)q[++curCnt] = p[i];
        else
        {
            if(a*p[i-1].x + b*p[i-1].y + c > 0)
            {
                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 dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
void solve()
{
       initial();
      for(int i = 1; i <= m; ++i){
          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);
      }
}
void GuiZhengHua(){ //規整化方向,逆時針變順時針,順時針變逆時針
    for(int i = 1; i < (m+1)/2; i ++)
      swap(points[i], points[m-i]);
}
int main()
{
    while (scanf("%d",&m)!=EOF){
        if (m==0) break;
        for (int i=1;i<=m;i++)
            scanf("%lf%lf",&points[i].x,&points[i].y);
        GuiZhengHua();
        points[m+1]=points[1];
        double left=0,right=inf,mid;
        while ((right-left)>=eps) {//二分求半徑
            mid=(left+right)/2.0;
           // cout<<1<<endl;
            r=mid;
            solve();
            if (cCnt<=0) right=mid;
            else left=mid;
        }
        printf("%.6f\n",left);
    }
    return 0;
}