
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358
【思路】
二分法+半平面交
二分与海边的的距离,由法向量可以得到平移后的各边,半平面交在特定精度判断是否有交集。
【代码】
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const double eps = 1e-; struct Pt {
double x,y;
Pt(double x=,double y=):x(x),y(y) {}
};
typedef Pt vec;
struct Line {
Pt P; vec v;
double ang;
Line () {};
Line (Pt P,vec v):P(P),v(v) { ang=atan2(v.y , v.x); }
bool operator < (const Line& rhs) const{
return ang<rhs.ang;
}
}; vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
vec operator + (vec A,vec B) { return vec(A.x+B.x,A.y+B.y); }
vec operator * (vec A,double p) { return vec(A.x*p,A.y*p); }
double Dot(vec A,vec B) { return A.x*B.x+A.y*B.y; }
double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; }
double Len(vec A) { return sqrt(Dot(A,A)); }
vec Normal(vec A) { double L=Len(A); return vec(-A.y/L,A.x/L); } bool onleft(Line L,Pt P) { return cross(L.v,P-L.P)>; } Pt LineIntersection(Line a,Line b) {
vec u=a.P-b.P;
double t=cross(b.v,u)/cross(a.v,b.v);
return a.P+a.v*t;
}
int HalfplaneIntersection(Line* L,int n,Pt* poly) {
sort(L,L+n);
int first,last;
Pt *p=new Pt[n];
Line *q=new Line[n];
q[first=last=]=L[];
for(int i=;i<n;i++) {
while(first<last && !onleft(L[i],p[last-])) last--;
while(first<last && !onleft(L[i],p[first])) first++;
q[++last]=L[i];
if(fabs(cross(q[last].v,q[last-].v))<eps) {
last--;
if(onleft(q[last],L[i].P)) q[last]=L[i];
}
if(first<last) p[last-]=LineIntersection(q[last-],q[last]);
}
while(first<last && !onleft(q[first],p[last-])) last--;
if(last-first<=) return ;
p[last]=LineIntersection(q[last],q[first]);
int m=;
for(int i=first;i<=last;i++) poly[m++]=p[i];
return m;
} const int N = +;
Pt p[N],poly[N];
Line L[N];
vec v[N] , v2[N];
int n; int main() {
while(scanf("%d",&n)== && n) {
int m,x,y;
for(int i=;i<n;i++) {
scanf("%d%d",&x,&y);
p[i]=Pt(x,y);
}
for(int i=;i<n;i++) {
v[i]=p[(i+)%n]-p[i];
v2[i]=Normal(v[i]);
}
double left= , right=;
while(right-left>eps) {
double mid=left+(right-left)/;
for(int i=;i<n;i++) L[i]=Line(p[i]+v2[i]*mid,v[i]);
m=HalfplaneIntersection(L,n,poly);
if(!m) right=mid; else left=mid;
}
printf("%.6lf\n",left);
}
return ;
}