参考:http://blog.csdn.net/FXXKI/article/details/45674979
看完别人写的发现思路还是挺简单的
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 110;
const double INF = 1e8;
const double eps = 1e-8;
struct Point
{
double x,y;
Point() {}
Point(double _x,double _y)
{
x = _x;
y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
Point ori[MAXN];
Point np[MAXN];
Point tp[MAXN];
int n,m;
Point Intersection(Point p1,Point p2,double a,double b,double c)
{
double u = fabs(a*p1.x + b*p1.y + c);
double v = fabs(a*p2.x + b*p2.y + c);
Point t;
t.x = (p1.x*v + p2.x*u)/(u+v);
t.y = (p1.y*v + p2.y*u)/(u+v);
return t;
}
void Get_equation(Point p1,Point p2,double &a,double &b,double &c)
{
a = p2.y - p1.y;
b = p1.x - p2.x;
c = p2.x*p1.y - p1.x*p2.y;
}
void Cut(double a,double b,double c)
{
int tmp = 0;
for(int i = 1; i <= m; i++)
{
if(a*np[i].x + b*np[i].y + c < eps)
tp[++tmp] = np[i];
else
{
if(a*np[i-1].x + b*np[i-1].y + c < -eps)
tp[++tmp] = Intersection(np[i-1],np[i],a,b,c);
if(a*np[i+1].x + b*np[i+1].y + c < -eps)
tp[++tmp] = Intersection(np[i],np[i+1],a,b,c);
}
}
for(int i = 1; i <= tmp; i++)
np[i] = tp[i];
np[0] = np[tmp];
np[tmp+1] = np[1];
m = tmp;
}
bool C(double d)
{
double a,b,c;
for(int i = 0; i <= n+1; ++i)
np[i] = ori[i];
m = n;
for(int i = 1; i <= n; ++i)
{
Get_equation(ori[i],ori[i+1],a,b,c);
c += d*sqrt(a*a+b*b);
Cut(a,b,c);
}
return m > 0;
}
double solve()
{
double lb = 0,ub = INF;
while(fabs(ub-lb) > 1e-5)
{
double mid = (ub+lb)/2;
if(C(mid)) lb = mid;
else ub = mid;
}
return ub;
}
int main()
{
while(scanf("%d",&n) && n)
{
for(int i = 1; i <= n; ++i)
scanf("%lf %lf",&ori[i].x,&ori[i].y);
ori[0] = ori[n];
ori[n+1] = ori[1];
double res = solve();
printf("%f\n",res);
}
return 0;
}