POJ 3525 Most Distant Point from the Sea(半平面交+凸多边形内切圆)

Most Distant Point from the Sea
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5786   Accepted: 2573   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.000000494.23364134.5429480.353553

解析:

这个题目限制了是一个凸多边形,于是多边形所在的范围就等价于各个边所在的直线划分成的半平面的交,如果是凹多边形的话显然就不能这样等价了。

    首先,我们可以把问题转化为求凸多边形的半径最大的内切圆,同时,我们会发现,如果各个边向内收缩r的话,内切圆的半径就会减少r,当缩到半平面交恰好不存在时,内切圆的半径也就为0了,这时向内收缩的距离r自然就是内切圆的最大半径了。

    于是我们只要二分内切圆的半径r作为各条边向内收缩的距离,然后判断这时半平面交是否为空集即可,如果为空则说明向内收缩的过头了,于是就要更新max,否则就更新min。

    值得一提的是,当半平面交恰好不存在时(或者说恰好存在时),半平面交表示的就是半径最大的内切圆的圆心。

半平面交 点击打开链接   O(nlogn) 点击打开链接
O(n^2)做法
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
const int MAXN = 15000+100;
struct Point
{
    double x, y;
}point[MAXN], temp[MAXN], temp_temp[MAXN];
int num_temp, cut_num;
int n;
double a, b, c;
void getline(Point p2, Point p1)//可以轻松证明出该结论。
{
    a = p1.y - p2.y;
    b = p2.x - p1.x;
    c = p2.y*p1.x - p1.y*p2.x;
}
Point intersect(Point x, Point y) //获取直线ax+by+c==0  和点x和y所连直线的交点
{
    double u = fabs(a*x.x + b*x.y + c);
    double v = fabs(a*y.x + b*y.y + c);
    Point ans;
    ans.x = (x.x*v + y.x*u) / (u + v);
    ans.y = (x.y*v + y.y*u) / (u + v);
    return ans;
}
void cut()  //点的顺序是顺时针的话就要变成>=0,>0
{
    cut_num = 0;
    for (int i = 1; i <= num_temp; i++)
    {
        Point p = temp[i];
        Point p1 = temp[i - 1];
        Point p2 = temp[i + 1];
        if (a*p.x + b*p.y + c <= 0)
        {
            temp_temp[++cut_num] = p;
        }
        else    //割平面
        {
            if (a*p1.x + b*p1.y + c < 0)  
            {
                temp_temp[++cut_num] = intersect(temp[i - 1], temp[i]);
            }
            if (a*p2.x + b*p2.y + c < 0)
            {
                temp_temp[++cut_num] = intersect(temp[i], temp[i + 1]);
            }
        }
    }
    for (int i = 1; i <= cut_num; i++)
    {
        temp[i] = temp_temp[i];
    }
    temp[cut_num + 1] = temp_temp[1];
    temp[0] = temp_temp[cut_num];
    num_temp = cut_num;
}
double solve()
{
    double l = 0, r = 1000000;
    double mid;
    while (r - l >= 1e-5)
    {
        mid = (l + r) / 2;
        for (int i = 0; i <= n + 1; i++)
        {
            temp[i] = point[i];
        }
        num_temp = n;
        for (int i = 1; i <= n; i++)
        {
            getline(point[i], point[i + 1]);   //控制哪边的值为正,向量法来规定直线左右平面的+-(沿向量方向左侧为-,右侧为+)
            c += mid * sqrt(a*a + b*b);     
            cut();
        }
        if (num_temp)
            l = mid;
        else
            r = mid;
    }
    return r;
}
int main()
{
    int t;
#ifdef glx
    freopen("in.txt", "r", stdin);
#endif
    while (~scanf("%d", &n)&&n)
    {
        for (int i = 1; i <= n; i++)
        {
            scanf("%lf%lf", &point[i].x, &point[i].y);
        }
        point[0] = point[n];
        point[n + 1] = point[1];
        printf("%.6lf\n", solve());
        //cout << num_temp << endl;
    }
    return 0;
}

O(nlogn)做法
/* ***********************************************
Author        :kuangbin
Created Time  :2013/8/18 15:11:26
File Name     :F:\2013ACM练习\专题学习\计算几何\半平面交\POJ3525.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int MAXN = 15000+10;
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;
    }
};
struct Line
{
    Point s,e;
    double k;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s; e = _e;
        k = atan2(e.y - s.y,e.x - s.x);
    }
    Point operator &(const Line &b)const
    {
        Point res = s;
        double t = ((s - b.s)^(b.s - b.e))/((s - e)^(b.s - b.e));
        res.x += (e.x - s.x)*t;
        res.y += (e.y - s.y)*t;
        return res;
    }
};
//半平面交,直线的左边代表有效区域
bool HPIcmp(Line a,Line b)
{
    if(fabs(a.k - b.k) > eps)return a.k < b.k;
    return ((a.s - b.s)^(b.e - b.s)) < 0;
}
Line Q[MAXN];
void HPI(Line line[], int n, Point res[], int &resn)
{
    int tot = n;
    sort(line,line+n,HPIcmp);
    tot = 1;
    for(int i = 1;i < n;i++)
        if(fabs(line[i].k - line[i-1].k) > eps)
            line[tot++] = line[i];
    int head = 0, tail = 1;
    Q[0] = line[0];
    Q[1] = line[1];
    resn = 0;
    for(int i = 2; i < tot; i++)
    {
        if(fabs((Q[tail].e-Q[tail].s)^(Q[tail-1].e-Q[tail-1].s)) < eps || fabs((Q[head].e-Q[head].s)^(Q[head+1].e-Q[head+1].s)) < eps)
            return;
        while(head < tail && (((Q[tail]&Q[tail-1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
            tail--;
        while(head < tail && (((Q[head]&Q[head+1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
            head++;
        Q[++tail] = line[i];
    }
    while(head < tail && (((Q[tail]&Q[tail-1]) - Q[head].s)^(Q[head].e-Q[head].s)) > eps)
        tail--;
    while(head < tail && (((Q[head]&Q[head-1]) - Q[tail].s)^(Q[tail].e-Q[tail].e)) > eps)
        head++;
    if(tail <= head + 1)return;
    for(int i = head; i < tail; i++)
        res[resn++] = Q[i]&Q[i+1];
    if(head < tail - 1)
        res[resn++] = Q[head]&Q[tail];
}
Point p[MAXN];
Line line[MAXN];
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
void change(Point a,Point b,Point &c,Point &d,double p)//将线段ab往左移动距离p
{
    double len = dist(a,b);
    double dx = (a.y - b.y)*p/len;
    double dy = (b.x - a.x)*p/len;
    c.x = a.x + dx; c.y = a.y + dy;
    d.x = b.x + dx; d.y = b.y + dy;
}
Point pp[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    while(scanf("%d",&n) == 1 && n)
    {
        for(int i = 0;i < n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        double l = 0, r = 100000;
        double ans = 0;
        while(r - l >= eps)
        {
            double mid = (l+r)/2;
            for(int i = 0;i < n;i++)
            {
                Point t1,t2;
                change(p[i],p[(i+1)%n],t1,t2,mid);
                line[i] = Line(t1,t2);
            }
            int resn;
            HPI(line,n,pp,resn);
            if(resn == 0)
                r = mid - eps;
            else
            {
                ans = mid;
                l = mid + eps;
            }
        }
        printf("%.6f\n",ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/80469754
今日推荐