求一个凸包内找最大的内切圆的半径,二分圆的半径,每次把凸包的各条边向内推进r,然后再判断推进后是否还是一个凸包。
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define mm(a,b) memset(a,b,sizeof(a))
#define maxn 205
using namespace std;
const int inf=0x7ffffff;
const double PI=acos(-1.0);
const double eps=1e-7;
const double e=2.7182818284590452354;
struct point
{
double x,y;
point() {}
point(double x,double y): x(x),y(y) {}
}p[maxn],q[maxn],pnt[maxn];
int cnt,curcnt,n;
void initial()
{
for(int i=1;i<=n;i++)
p[i]=pnt[i];
p[n+1]=p[1];
p[0]=p[n];
cnt=n;
}
//两点确定直线
void getline(point x,point y,double &a,double &b,double &c)
{
a=y.y-x.y;
b=x.x-y.x;
c=y.x*x.y-x.x*y.y;
}
//求交点
point intersect(point x,point y,double a,double b,double c)
{
double u=fabs(a*x.x+b*x.y+c);
double v=fabs(a*y.x+b*y.y+c);
return point((v*x.x+u*y.x)/(u+v),(v*x.y+u*y.y)/(u+v));
}
//直线切割
void cut(double a,double b,double c)
{
curcnt=0;
for(int i=1;i<=cnt;i++)
{
if(a*p[i].x+b*p[i].y+c>=0)//当前点在直线右侧
q[++curcnt]=p[i];
else
{
if(a*p[i-1].x+b*p[i-1].y+c>0)//前一个点在直线右侧
q[++curcnt]=intersect(p[i],p[i-1],a,b,c);
if(a*p[i+1].x+b*p[i+1].y+c>0)//同理
q[++curcnt]=intersect(p[i],p[i+1],a,b,c);
}
}
for(int i=1;i<=curcnt;i++)
p[i]=q[i];
p[curcnt+1]=p[1];
p[0]=p[curcnt];
cnt=curcnt;
}
bool solve(double r)
{
initial();
//向内推进r
for(int i=1;i<=n;i++)
{
point ta,tb,tt;
tt.x=pnt[i+1].y-pnt[i].y;
tt.y=pnt[i].x-pnt[i+1].x;
double k=r/sqrt(tt.x*tt.x+tt.y*tt.y);
tt.x=tt.x*k;
tt.y=tt.y*k;
ta.x=pnt[i].x+tt.x;
ta.y=pnt[i].y+tt.y;
tb.x=pnt[i+1].x+tt.x;
tb.y=pnt[i+1].y+tt.y;
double a,b,c;
getline(ta,tb,a,b,c);
cut(a,b,c);
}
return cnt>0?1:0;
}
int main()
{
int t;
while(~scanf("%d",&n),n)
{
for(int i=1;i<=n;i++)
scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
for(int i=1;i<(n+1)/2;i++)
swap(pnt[i],pnt[n-i]);
pnt[n+1]=pnt[1];
pnt[0]=pnt[n];
double right=1e9;
double left=0,mid;
while(left+eps<right)
{
mid=(right+left)/2;
if(solve(mid))
left=mid;
else right=mid;
}
printf("%lf\n",right);
}
return 0;
}