# hdu 5120 Intersection

A ring is a 2-D figure bounded by two circles sharing the common center. The radius for these circles are denoted by r and R (r < R). For more details, refer to the gray part in the illustration below.

Matt just designed a new logo consisting of two rings with the same size in the 2-D plane. For his interests, Matt would like to know the area of the intersection of these two rings.

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<cmath>
6 #include<algorithm>
7 #define inf 0x7fffffff
8 #define exp 1e-10
9 #define PI 3.141592654
10 using namespace std;
11 typedef long long ll;
12 struct Point
13 {
14     double x,y;
15     Point (double x=0,double y=0):x(x),y(y){}
16 };
17 double dist(Point a,Point b)
18 {
19     double x=(a.x-b.x)*(a.x-b.x);
20     double y=(a.y-b.y)*(a.y-b.y);
21     return sqrt(x+y);
22 }
23 double Area_of_overlap(Point c1,double r1,Point c2,double r2)
24 {
25     double d=dist(c1,c2);
26     if (r1+r2<d+exp) return 0;
27     if (d<fabs(r1-r2)+exp)
28     {
29         double r=min(r1,r2);
30         return PI*r*r;
31     }
32     double x=(d*d+r1*r1-r2*r2)/(2*d);
33     double t1=acos(x/r1);
34     double t2=acos((d-x)/r2);
35     return r1*r1*t1+r2*r2*t2-d*r1*sin(t1);
36 }
37 int main()
38 {
39     int t,ncase=1;
40     double r,R;
41     Point a,b;
42     scanf("%d",&t);
43     while (t--)
44     {
45         scanf("%lf%lf",&r,&R);
46         scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
47         double bb_area=Area_of_overlap(a,R,b,R);
48         double bs_area=Area_of_overlap(a,R,b,r);
49         double ss_area=Area_of_overlap(a,r,b,r);
50         printf("Case #%d: %.6lf\n",ncase++,bb_area-2.0*bs_area+ss_area);
51     }
52     return 0;
53 }

posted @ 2014-12-07 20:52  huangxf  阅读(729)  评论(0编辑  收藏  举报