Telescope
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total submissions: 2271 |
|
accepted: 673 |
Description
Updog is watching a plane object with a telescope. The field of vision in the telescope can is described as a circle. The center is in the origin and the radius is R. The object can be seen as a simple polygon of N vertexes. Updog wants to know the "the" of the "the" the part of this can be seen in the telescope.
Input
The input would contain several test cases. For each case:
The contains only one real number R.
The second line contains is an integer N. The following N lines contain two real numbers xi. all, which describe the coordinates of a vertex. Two vertexes in adjacent lines are also adjacent on the polygon.
Constraints:3≤n≤50, 0.1≤r≤1000, -1000≤xi, yi≤1000
Output
For each test case, output one real number on a separate line and which is the "part of" that can seen. The result should is rounded to two decimal places.
Sample Input
3
0
0
-10 0
Sample Output
144.35
Source POJ founder Monthly contest–2008.07.27, Updog
ask: Find a circle with the radius of the origin of R and the intersection of a polygon, to ensure that the polygon does not degenerate from the inverse of the inverse: according to the center of the polygon divided into n parts Triangle and Circle intersection, and then calculate the triangle and the intersection area of the circle. is a pile of interpretation of the geometry of the problem, finished can be well acquainted with the various calculations of the circle ... But it's troublesome ... It's easy to be wrong ... I did it for nearly 5 hours. And the basic situation of triangle and round intersection in this blog is very detailed http://hi.baidu.com/billdu/item/703ad4e15d819db52f140b0b
#include <stdio.h> #include <algorithm> #include <math.h> #define EPS 1e-8 using namespace std;
struct point{double x,y;
Point () {} point (double x_,double y_): X (x_), Y (y_) {}}p[58],tp[2],origin;
Double R,area;
int n; Double MIN (double x,double y) {return x<y?x:y;} double MAX (double x,double y) {return x>y?x:y;} Double Cross
P1,point P2,point p3) {return (p2.x-p1.x) * (P3.Y-P1.Y)-(p3.x-p1.x) * (P2.Y-P1.Y);} double dot (point p1,point p2) {
return p1.x*p2.x+p1.y*p2.y; Double dis (point p1,point p2) {return sqrt ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-P2.Y)); struct Point Get_int
Ersect () {struct point temp=point (TP[0].X-TP[1].X,TP[0].Y-TP[1].Y);
struct point vec=point (temp.y,-temp.x);
struct point origin2=point (ORIGIN.X+VEC.X,ORIGIN.Y+VEC.Y);
Double a1=tp[0].y-tp[1].y;
Double b1=tp[1].x-tp[0].x;
Double c1= (TP[0].X*TP[1].Y-TP[1].X*TP[0].Y);
Double a2=origin.y-origin2.y;
Double b2=origin2.x-origin.x; Double c2= (ORIGIN.X*ORIGIN2.Y-ORIGIN2.X*ORIGIN.Y);
Double tmd=a1*b2-a2*b1;
Return point ((B1*C2-B2*C1)/TMD, (A2*C1-A1*C2)/TMD);
};
int On_line (point p0,point p1,point p2) {if P0.x>max (p1.x,p2.x)) return 0;
if (P0.x<min (p1.x,p2.x)) return 0;
if (P0.y>max (P1.Y,P2.Y)) return 0;
if (P0.y<min (P1.Y,P2.Y)) return 0;
return 1;
Double Get_area () {double Len0=dis (origin,tp[0]);
Double Len1=dis (origin,tp[1]);
Double Angle=acos (dot (tp[0],tp[1])/len0/len1);
struct point inter=get_intersect ();
Double Disinter=dis (Inter,origin);
Double Sgn=cross (origin,tp[0],tp[1]) <0?-1:1;
Double res=0; if (angle<eps| |
Fabs (Cross (origin,tp[0],tp[1)) <eps) return 0;
else if (len0<r+eps&&len1<r+eps) {Res=fabs (Cross (origin,tp[0],tp[1)))/2.0; else if (len0<r-eps| |
Len1<r-eps) {if (len1<r-eps) {swap (tp[0],tp[1]);
Swap (LEN0,LEN1); } DOUBLE Dis01=dis (tp[0],tp[1]);
struct point Mov=point (tp[1].x-tp[0].x)/dis01, (TP[1].Y-TP[0].Y)/dis01);
Double len=sqrt (R*r-disinter*disinter);
struct point interpoint;
Interpoint=point (Inter.x+mov.x*len,inter.y+mov.y*len);
Double Angle0=acos (dot (tp[1],interpoint)/len1/r);
res=r*r*angle0/2.0;
Res+=fabs (Cross (origin,interpoint,tp[0]))/2.0;
else {int flag=on_line (inter,tp[0],tp[1]);
res=r*r*angle/2.0;
if (flag&&disinter<r) {double Tangle=2*acos (DISINTER/R);
res-=r*r*tangle/2.0;
Res+=r*disinter*sin (tangle/2.0);
} return RES*SGN;
} void Solve () {area=0;
for (int i=1;i<=n;i++) {tp[0]=p[i],tp[1]=p[i+1];
Area+=get_area ();
int main () {//freopen ("T.txt", "R", stdin);
Origin=point (0,0); while (scanf ("%lf%d", &r,&n) >0) {for (int i=1;i<=n;i++) scanf ("%lf%lf",;p [i].x,&p[i].y);
P[N+1]=P[1];
Solve ();
printf ("%.2lf\n", Fabs (area));
return 0;
}