Hard Evidence
Hard Evidence
The young reporter Janne is planning to take a photo of a secret government installation. He needs to obtain evidence of the many serious crimes against good sense that are being committed there, so as to create a scandal and possibly win a Pulitzer. Unfortunately, the base is surrounded by a high fence with high voltage wires running around. Janne does not want to risk being electrocuted, so he wants to take a photo from outside the fence. He can bring a tripod as high as the fence to take a photo, so if he wants he can stand right beside the fence and take his picture. The secret installation is a convex polygon. The fence has a form of a circle. Of course Janne wants to make a photo with maximal possible detail level.
Consider taking a photo of the base from some point P outside the fence. Let Q be the left-most point of the base when viewed from P , and let R be the right-most point of the base when viewed from P . Then the view angle of the base at P is the angle at P in the triangle formed by the three points P , Q and R .
The detail level of the photo depends on the view angle of the base at the point from which the photo is taken. Therefore he wants to find a point to maximize this angle.
Input
The first line of the input file contains two integer numbers: n and r -- the number of vertices of the polygon and the radius of the fence (3n200 , 1r1000 ). The following n lines contain two real numbers each -- the coordinates of the vertices of the polygon listed in counterclockwise order. It is guaranteed that all vertices of the polygon are strictly inside the fence circle, and that the polygon is convex. The center of the fence circle is located at the origin, (0, 0) .
Output
For each test case, write to the output the maximal view angle a for the photo ( 0a < 2 ), on a line by itself. Any answer with either absolute or relative error smaller than 10-6 is acceptable.
Sample Input
4 2 -1.0 -1.0 1.0 -1.0 1.0 1.0 -1.0 1.0
Sample Output
1.5707963268
题意:找到最大的能够看到的角度。
思路:先预处理,即找到相邻的两个点的连线与圆的交点的两个点所分别对应的角度,找到所有可能的情况,然后对这两个点进行三分操作,找到最大的角度。
另外预处理的那部分几何不好,所有从网上借鉴的代码,不过剩下的部分是我写的。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps=1e-8,pi=3.14159265358979;
double x[210],y[210],start[210],end[210],r;
int n;
void QuadraticEquation(double a, double b, double c, double &res1, double &res2)
{ double d = b * b - 4 * a * c;
if (d < 0) return ;
d = sqrt(d);
res1 = (-b + d) / (2 * a);
res2 = (-b - d) / (2 * a);
return ;
}
double solve(double mi,int i,int j)
{ double x1,y1,a,b,c,angle,ans;
if(j>n)
j=1;
x1=r*cos(mi);
y1=r*sin(mi);
a=(x1-x[i])*(x1-x[i])+(y1-y[i])*(y1-y[i]);
b=(x1-x[j])*(x1-x[j])+(y1-y[j])*(y1-y[j]);
c=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
angle=(a+b-c)/(2*sqrt(a*b));
ans=acos(angle);
return ans;
}
int main()
{ int i,j,k,t;
double dx,dy,a,b,c,res1,res2,mi,mi1,mi2,val,val1,val2,angle,ans,px1,px2,py1,py2,left,right,st1,end1,st2,end2;
while(~scanf("%d%lf",&n,&r))
{ ans=0;val=0;angle=0;ans=0;
for(i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
for(i=1;i<=n;i++)
{ j=i+1;
if(j>n)
j=1;
dx = x[j] - x[i], dy = y[j] - y[i];
c = x[i] * x[i] + y[i] * y[i] - r * r;
a = dx * dx + dy * dy, b = 2 * (dx * x[i] + dy * y[i]);
QuadraticEquation(a, b, c, res1, res2);
px1 = x[i] + res1 * dx, py1 = y[i] + res1 * dy;
px2 = x[i] + res2 * dx, py2 = y[i] + res2 * dy;
start[i] = atan2(py2, px2);
end[i] = atan2(py1, px1);
}
for(i=1;i<=n;i++)
{ j=i;
left=start[i];
right=end[j];
if(left>right)
right+=pi*2;
while(true)
{ while(left+eps<right)
{ mi=(right-left)/3;
mi1=left+mi;
mi2=left+mi*2;
val1=solve(mi1,i,j+1);
val2=solve(mi2,i,j+1);
if(val1>val2)
right=mi2;
else
left=mi1;
}
if(val1>angle)
angle=val1;
j++;
if(j>n)
j=1;
st1=start[i];
end1=end[i];
if(st1>end1)
end1+=2*pi;
st2=start[j];
if(st1<=st2 && st2<=end1)
{ left=st2;
right=end1;
continue;
}
st2+=2*pi;
if(st1<=st2 && st2<=end1)
{ left=st2;
right=end1;
continue;
}
break;
}
}
printf("%.10f\n",angle);
}
}