POJ 3525 Most Distant Point from the Sea (二分答案+半平面交)

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 ≤ 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


Solution

二分出最远距离,用半平面交检验即可。

假设当前最远距离为d,那么将小岛的边界垂直向内移动d个单位长度后如果包围的土地不为空(单点也行,实际上单点最好),那么d就可能成为最远的距离,否则就不行。那么只要二分答案然后判断半平面的交集是否为空就可以了。


Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#define Eps 1e-10
#define N 111

using namespace std;

int n;

struct Point{
    double x, y;
    Point() {}
    Point(double _x, double _y):x(_x), y(_y) {}
}p[N], is[N], nor[N];

typedef Point Vector;

Vector operator + (Vector A, Vector B){return Vector(A.x + B.x, A.y + B.y);}
Vector operator - (Vector A, Vector B){return Vector(A.x - B.x, A.y - B.y);}
Vector operator * (double A, Vector B){return Vector(A * B.x, A * B.y);}
Vector operator / (Vector A, double B){return Vector(A.x / B, A.y / B);}
double Dot(Vector A, Vector B){return A.x * B.x + A.y * B.y;}
double Det(Vector A, Vector B){return A.x * B.y - A.y * B.x;}
double Length(Vector A){return sqrt(Dot(A, A));}

struct Line{
    Point P;
    Vector v;
    double ang;
    Line() {}
    Line(Point _P, Vector _v):P(_P), v(_v) {ang = atan2(_v.y, _v.x);}
    bool operator < (const Line& l) const{return ang < l.ang;}
}q[N], L[N];

Vector Normal(Vector l){
    double len = Length(l);
    return Vector(-l.y / len, l.x / len);
}//求有向直线的单位法向量

bool Onleft(Point A, Line B){
    return Det(A - B.P, B.v) < Eps;//点在线上也行的
}

Point Cross(Line A, Line B){
    Point p1 = A.P, q1 = A.P + A.v;
    Point p2 = B.P, q2 = B.P + B.v;
    double x = Det(p2 - p1, q2 - p1), y = Det(q2 - q1, p2 - q1);
    return (x * q1 + y * p1) / (x + y);
}

bool HalfplaneI(){

    sort(L+1, L+1+n);
    int head, tail;
    q[head = tail = 0] = L[1];
    for(int i = 2; i <= n; i++){
      while(head < tail && !Onleft(p[tail-1], L[i]))  tail --;
      while(head < tail && !Onleft(p[head], L[i]))  head ++;
      q[++tail] = L[i];
      if(fabs(Det(q[tail].v, q[tail-1].v)) < Eps){
        tail --;
        if(Onleft(L[i]. P, q[tail]))  q[tail] = L[i];
      }
      if(head < tail)  p[tail-1] = Cross(q[tail], q[tail-1]);
    }
    while(head < tail && !Onleft(p[tail-1], q[head]))  tail --;
    return tail - head > 1;//交集不为空
}

int main(){


    while(~ scanf("%d", &n) && n){

      for(int i = 1; i <= n; i++)  scanf("%lf%lf", &is[i].x, &is[i].y);

      is[n+1] = is[1];
      for(int i = 1; i <= n; i++)  nor[i] = Normal(is[i+1] - is[i]);

      double Left = 0.00, Right = 10000.00; 

      while(Left + Eps < Right){
        double mid = (Left + Right) / 2.00;
        for(int i = 1; i <= n; i++)  L[i] = Line(is[i] + mid * nor[i], is[i+1] - is[i]);//移动半平面
        if(HalfplaneI())  Left = mid;
        else  Right = mid;
      }

      printf("%.6lf\n", Left);
    }


    return 0;



} 

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值