题目:
题意:
给出一个多边形,求里面一个点,其距离离多边形的边界最远,输出距离。
题解:
二分答案,相当于是把所有直线往里平移mid长,看看这个半平面能不能有交点
这个平移嘛。。可以找到一个垂直的方向(rotate),然后按照长度往里平移就好了
代码:
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-9;
const double pi=acos(-1.0);
int dcmp(double x)
{
if (x<=eps && x>=-eps) return 0;
return (x>0)?1:-1;
}
struct po
{
double x,y;
po(double X=0,double Y=0){x=X;y=Y;}
}d[105],p[105];
struct Line
{
po x,v;double ang;
Line(po X=po(0,0),po V=po(0,0)){x=X;v=V;ang=atan2(v.y,v.x);}
}L[105],q[105];int n,head,tail;
po rotate(po x,double v){return po(x.x*cos(v)-x.y*sin(v),x.x*sin(v)+x.y*cos(v));}
po operator +(po x,po y){return po(x.x+y.x,x.y+y.y);}
po operator -(po x,po y){return po(x.x-y.x,x.y-y.y);}
po operator *(po x,double y){return po(x.x*y,x.y*y);}
double cj(po x,po y){return x.x*y.y-x.y*y.x;}
double dot(po x,po y){return x.x*y.x+x.y*y.y;}
double Len(po x){return sqrt(dot(x,x));}
bool Onleft(Line x,po y){return dcmp(cj(x.v,y-x.x))>0;}
po jd(Line a,Line b)
{
po u=a.x-b.x;
double t=cj(b.v,u)/cj(a.v,b.v);
return a.x+a.v*t;
}
int cmp(Line x,Line y){return x.ang<y.ang;}
bool halfp()
{
sort(L+1,L+n+1,cmp);
head=tail=1; q[1]=L[1];
for (int i=2;i<=n;i++)
{
while (head<tail && !Onleft(L[i],p[tail-1])) tail--;
while (head<tail && !Onleft(L[i],p[head])) head++;
q[++tail]=L[i];
if (dcmp(cj(q[tail].v,q[tail-1].v))==0)
{
tail--;
if (Onleft(q[tail],L[i].x)) q[tail]=L[i];
}
if (head<tail) p[tail-1]=jd(q[tail],q[tail-1]);
}
while (head<tail && !Onleft(q[head],p[tail-1])) tail--;
if (tail-head<=1) return 0;return 1;
}
bool check(double mid)
{
for (int i=1;i<=n;i++)
{
po a=d[i],b=d[i%n+1];
po w=rotate(b-a,pi/2);
w=w*(mid/Len(w));
a=a+w; b=b+w;
L[i]=Line(a,b-a);
}
return halfp();
}
int main()
{
while (scanf("%d",&n) && n)
{
double x,y,l=0,r=0;
for (int i=1;i<=n;i++)
{
scanf("%lf%lf",&x,&y);
r=max(r,max(x,y));
d[i]=po(x,y);
}
while (dcmp(r-l))
{
double mid=(l+r)/2;
if (check(mid)) l=mid;else r=mid;
}
printf("%.6lf\n",l);
}
}