POJ 3525 Most Distant Point from the Sea(凸多边形的最大内切圆@半平面交+二分)

Most Distant Point from the Sea
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4102 Accepted: 1921 Special Judge

Description

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

Source

 
题意:求在一个形状为多边形的岛中的一点到海的最大距离,转换成在多边形内可以画一个最大半径为多少的圆。
 
思路:二分答案半径,然后判断是否存在这样一个区域能放入这个圆心,每次在半平面交的时候把每条边向内侧平移半径长度,然后判断新的多边形是否存在核。

也就是每条边向里面平移r长度,去截凸多边形,最后判断是否有核,我模版用的不熟弄错了一点找了半天错误,郁闷。。。

 

代码:

 

#include<stdio.h>
#include<math.h>
#include<map>
#include<iostream>
#include<string.h>
using namespace std;
const int N = 1505;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point() {}
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
};
//计算多边形面积
double CalcArea(Point p[],int n)
{
    double res = 0;
    for(int i = 0; i < n; i++)
        res += (p[i]^p[(i+1)%n]);
    return fabs(res/2);
}
//通过两点,确定直线方程
void Get_equation(Point p1,Point p2,double &a,double &b,double &c)
{
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x*p1.y - p1.x*p2.y;
}
//求交点
Point Intersection(Point p1,Point p2,double a,double b,double c)
{
    double u = fabs(a*p1.x + b*p1.y + c);
    double v = fabs(a*p2.x + b*p2.y + c);
    Point t;
    t.x = (p1.x*v + p2.x*u)/(u+v);
    t.y = (p1.y*v + p2.y*u)/(u+v);
    return t;
}
Point tp[N],p[N],q[N];
void Cut(double a,double b,double c,Point p[],int &cnt)
{
    int tmp = 0;
    for(int i = 1; i <= cnt; i++)
    {
//当前点在左侧,逆时针的点
        if(a*p[i].x + b*p[i].y + c < eps)tp[++tmp] = p[i];
        else
        {
            if(a*p[i-1].x + b*p[i-1].y + c < -eps)//如果p[i-1]在直线的左侧的话,
            
//则将p[i],p[i-1]形成的直线与已知直线的交点作为核的一个顶点(这样的话,由于精度的问题,核的面积可能会有所减少)
                tp[++tmp] = Intersection(p[i-1],p[i],a,b,c);
            if(a*p[i+1].x + b*p[i+1].y + c < -eps)
                tp[++tmp] = Intersection(p[i],p[i+1],a,b,c);
        }
    }
    for(int i = 1; i <= tmp; i++)
        p[i] = tp[i];
    p[0] = p[tmp];
    p[tmp+1] = p[1];
    cnt = tmp;
}

int solve(Point p[],int n,double r)
{
    p[n+1]=p[1];p[0]=p[n];//这里p[0]=p[n]不能少..不然cut会出错
    int cnt=n;
    for(int i=0;i<=n+1;i++) q[i]=p[i];
    for(int i=1; i<=n; i++)
    {
        double a,b,c;
        Point q1,q2;
        double xx = p[i+1].x-p[i].x;
        double yy = p[i+1].y-p[i].y;
        double dd = sqrt(xx*xx+yy*yy);
        q1.x = p[i].x + r*(-yy)/dd, q1.y = p[i].y + r*(xx)/dd;
        q2.x = p[i+1].x + r*(-yy)/dd, q2.y = p[i+1].y + r*(xx)/dd;
       // printf("q1.x=%lf, q1.y=%lf, q2.x=%lf, q2.y=%lf\n",q1.x,q1.y,q2.x,q2.y);
        Get_equation(q1,q2,a,b,c);
        Cut(a,b,c,q,cnt);
       // printf("r=%lf, i=%d, a=%lf, b=%lf, c=%lf,  cnt=%d\n",r,i,a,b,c,cnt);
    }
    return cnt;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF && n)
    {
        for(int i=1; i<=n; i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        double low=0,high=100000,mid;
        while(fabs(high-low)>eps)
        {
            mid=(low+high)/2.0;
            if(solve(p,n,mid))
                low=mid;
            else
                high=mid;
        }
        printf("%.6lf\n",mid);
    }
}
View Code

 

posted @ 2015-02-17 10:00  Doli  阅读(145)  评论(0编辑  收藏  举报