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.
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 Source |
[Submit] [Go Back] [Status] [Discuss]
题目大意:求凸包中的点到凸包边界的最远距离,即凸多边形的最大内切圆。
题解:二分+半平面交。
每次二分一个长度,然后让凸多边形的每条边向内缩,以缩完后的边界做半平面交,如果不存在公共区域或者公共区域为一个点,则说明长度大了或正好,继续二分即可。
主要是内缩比较难搞,我是把相邻两点构成的向量存储下来,然后按照法向量的方向平移得到新的向量,再进行半平面交的。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 103
#define eps 1e-7
#define inf 1e9
using namespace std;
struct vector {
double x,y;
vector (double X=0,double Y=0){
x=X,y=Y;
}
}a[N],p[N],tmp[N],b[N];
struct data
{
vector a,b;
}line[N];
int m,n;
double pi=acos(-1.0);
typedef vector point;
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 *(vector a,double t){return vector (a.x*t,a.y*t);}
vector operator /(vector a,double t){return vector (a.x/t,a.y/t);}
void init()
{
m=0;
p[m++]=point(inf,inf);
p[m++]=point(-inf,inf);
p[m++]=point(-inf,-inf);
p[m++]=point(inf,-inf);
}
int dcmp(double x)
{
if (fabs(x)<eps) return 0;
return x<0?-1:1;
}
double cross(vector a,vector b)
{
return a.x*b.y-a.y*b.x;
}
point glt(point a,point a1,point b,point b1)
{
vector v=a1-a; vector w=b1-b;
vector u=a-b;
double t=cross(w,u)/cross(v,w);
return a+v*t;
}
void cut(point a,point b)
{
int cnt=0;
memset(tmp,0,sizeof(tmp));
for (int i=0;i<m;i++){
double c=cross(b-a,p[i]-a);
double d=cross(b-a,p[(i+1)%m]-a);
if (dcmp(c)<=0) tmp[cnt++]=p[i];
if (dcmp(c)*dcmp(d)<0)
tmp[cnt++]=glt(a,b,p[i],p[(i+1)%m]);
}
m=cnt;
for (int i=0;i<cnt;i++) p[i]=tmp[i];
}
vector rotate(vector a,double rad)
{
return vector (a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
double len(vector v)
{
return sqrt(v.x*v.x+v.y*v.y);
}
bool pd(double x)
{
for (int i=2;i<=n+1;i++){
vector v=b[i]-b[i-1];
vector u=rotate(v,pi/2)/len(v);
u=u*x;
line[i-1].a=v+u+b[i-1];
line[i-1].b=line[i-1].a-v;
int t=0;
}
init();
for (int i=1;i<=n;i++){
cut(line[i].a,line[i].b);
if (!m) return true;
}
if (m==1) return true;
return false;
}
int main()
{
freopen("a.in","r",stdin);
while (true){
scanf("%d",&n);
if (!n) break;
for (int i=1;i<=n;i++) scanf("%lf%lf",&b[i].x,&b[i].y);
b[n+1]=b[1];
double l=0; double r=10000; double ans=0;
while (r-l>eps){
double mid=(l+r)/2;
if (pd(mid)) ans=mid,r=mid-eps;
else l=mid+eps;
}
printf("%.6lf\n",ans);
}
}