poj 3525Most Distant Point from the Sea【二分+半平面交】
相当于多边形内最大圆,二分半径r,然后把每条边内收r,求是否有半平面交(即是否合法)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=205;
const double eps=1e-6;
int n;
struct dian
{
double x,y;
dian(double X=0,double Y=0)
{
x=X,y=Y;
}
dian operator + (const dian &a)
{
return dian(x+a.x,y+a.y);
}
dian operator - (const dian &a)
{
return dian(x-a.x,y-a.y);
}
dian operator * (const double &a) const
{
return dian(x*a,y*a);
}
dian operator / (const double &a) const
{
return dian(x/a,y/a);
}
}p[N];
struct bian
{
dian s,v;
bian(dian S=dian(),dian V=dian())
{
s=S,v=V;
}
}b[N],l[N],s[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
double cj(dian a,dian b)
{
return a.x*b.y-a.y*b.x;
}
double mj(dian a,dian b,dian c)
{
return cj(b-a,c-a)/2;
}
dian jd(bian x,bian y)
{
return x.s+x.v*(cj(x.s-y.s,y.v)/cj(y.v,x.v));
}
bool px(bian a,bian b)
{
return cj(a.v,b.v)==0;
}
bool bn(bian a,bian b)
{
int ar=cj(a.v,b.v);
return ar>0||(ar==0&&cj(a.v,b.s-a.s)>0);
}
bool dn(dian x,bian y)
{
return cj(y.v,x-y.s)<=0;
}
bool cmp(const bian &x,const bian &y)
{
if(x.v.y==0&&y.v.y==0)
return x.v.x<y.v.x;
if((x.v.y<=0)==(y.v.y<=0))
return bn(x,y);
return x.v.y<y.v.y;
}
double dis2(bian a)
{
return sqrt(a.v.x*a.v.x+a.v.y*a.v.y);
}
bian yi(bian a,double r)
{
return bian(dian(a.s.x-a.v.y*r/dis2(a),a.s.y+a.v.x*r/dis2(a)),a.v);
}
bool ok(double r)
{
for(int i=1;i<=n;i++)
l[i]=yi(b[i],r);
sort(l+1,l+1+n,cmp);
int top=0;
for(int i=1;i<=n;i++)
if(i==1||!px(l[i],l[i-1]))
l[++top]=l[i];
n=top;
int ll=1,rr=2;
s[1]=l[1],s[2]=l[2];
for(int i=3;i<=n;i++)
{
while(ll<rr&&dn(jd(s[rr],s[rr-1]),l[i]))
rr--;
while(ll<rr&&dn(jd(s[ll],s[ll+1]),l[i]))
ll++;
s[++rr]=l[i];
}
while(ll<rr&&dn(jd(s[rr],s[rr-1]),s[ll]))
rr--;
return rr-ll>1;
}
int main()
{
while(scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
p[i].x=read(),p[i].y=read();
p[n+1]=p[1];
for(int i=1;i<=n;i++)
b[i]=bian(p[i],p[i+1]-p[i]);
double l=0,r=10005,ans=0;
while(r-l>=eps)
{
double mid=(l+r)/2;//printf("%.6f\n",mid);
if(ok(mid))
l=mid,ans=mid;
else
r=mid;
}
printf("%.6f\n",ans);
}
return 0;
}