POJ 3525 Most Distant Point from the Sea (banpingmianjiao)

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 (xiyi)–(xi+1yi+1) (1 ≤ i ≤ n − 1) and the line segment (xn,yn)–(x1y1) 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,当缩到半平面交恰好不存在时,内切圆的半径也就为0了,这时向内收缩的距离r自然就是内切圆的最大半径了。

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

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

#include <iostream> #include <iomanip> #include <math.h> #include <string.h> #include <algorithm> using namespace std; #define eps 1e-8 struct Point {     double x,y;     Point(){}     Point(double x,double y):x(x),y(y){} }; struct Line {     Point P;     Point v;     double ang;     Line(){}     Line(Point P,Point v):P(P),v(v){ang=atan2(v.y,v.x);}     bool operator < (const Line&L)const     {         return ang<L.ang;     } }; Point operator + (Point A,Point B) {     return Point(A.x+B.x,A.y+B.y); } Point operator - (Point A,Point B) {     return Point(A.x-B.x,A.y-B.y); } Point operator * (Point A,double p) {     return Point(A.x*p,A.y*p); } double Dot(Point A,Point B) {     return A.x*B.x+A.y*B.y; } double Length(Point A) {     return sqrt(Dot(A,A)); } Point Normal(Point A) {     double L=Length(A);     return Point(-A.y/L,A.x/L); } double Cross(Point A,Point B) {     return A.x*B.y-A.y*B.x; } bool OnLeft(Line L,Point p)//点p在有向直线L的左边(线上不算) {     return Cross(L.v,p-L.P)>0; } Point GetIntersection(Line a,Line b)//两直线交点,假定直线唯一存在 {     Point u=a.P-b.P;     double t=Cross(b.v,u)/Cross(a.v,b.v);     return a.P+a.v*t; } int HalfplaneIntersection(Line *L,int n,Point *poly) {     sort(L,L+n);//按极角排序     int first,last;//双端队列的第一个元素和最后一个元素     Point *p=new Point[n];//p[i]为q[i]和q[i+1]的交点     Line *q=new Line[n];//双端队列     q[first=last=0]=L[0];//双端队列初始化为只有一个半平面L[0]     for(int i=1;i<n;i++)     {         while(first<last&&!OnLeft(L[i],p[last-1]))last--;         while(first<last&&!OnLeft(L[i],p[first]))first++;         q[++last]=L[i];         if(fabs(Cross(q[last].v,q[last-1].v))<eps)//两向量平行且同向,取内测的一个         {             last--;             if(OnLeft(q[last],L[i].P))q[last]=L[i];         }         if(first<last)p[last-1]=GetIntersection(q[last-1],q[last]);     }     while(first<last&&!OnLeft(q[first],p[last-1]))last--;//删除无用平面     if(last-first<=1)return 0;//空集     p[last]=GetIntersection(q[last],q[first]);//计算首尾两个半平面的交点     int m=0;//从deque复制到输出     for(int i=first;i<=last;i++)poly[m++]=p[i];     return m; } int main() {     int n;     while(cin>>n&&n!=0)     {         Point p[210],poly[210];         Line L[210];         Point V[210],V2[210];         for(int i=0;i<n;i++)             cin>>p[i].x>>p[i].y;         for(int i=0;i<n;i++)         {             V[i]=p[(i+1)%n]-p[i];             V2[i]=Normal(V[i]);         }         double left=0,right=20000;         while(right-left>1e-6)         {             double mid=(left+right)/2;             for(int i=0;i<n;i++)                 L[i]=Line(p[i]+V2[i]*mid,V[i]);             int m=HalfplaneIntersection(L,n,poly);             if(!m)right=mid;             else left=mid;         }         cout<<setiosflags(ios::fixed)<<setprecision(6);         cout<<left<<endl;     }     return 0; }
 
   
 
   
 
   
 
   
 
  

你可能感兴趣的:(POJ 3525 Most Distant Point from the Sea (banpingmianjiao))