hdu 5120(求两个圆环相交的面积 2014北京现场赛 I题)
两个圆环的内外径相同 给出内外径 和 两个圆心 求两个圆环相交的面积
画下图可以知道 就是两个大圆交-2*小圆与大圆交+2小圆交
Sample Input
2
2 3
0 0
0 0
2 3
0 0
5 0
Sample Output
Case #1: 15.707963
Case #2: 2.250778
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string> 6 # include <cmath> 7 # include <queue> 8 # include <list> 9 # define LL long long 10 using namespace std ; 11 12 const double eps = 1e-8; 13 const double PI = acos(-1.0); 14 15 struct Point 16 { 17 double x,y; 18 Point(){} 19 Point(double _x,double _y) 20 { 21 x = _x;y = _y; 22 } 23 Point operator -(const Point &b)const 24 { 25 return Point(x - b.x,y - b.y); 26 } 27 //叉积 28 double operator ^(const Point &b)const 29 { 30 return x*b.y - y*b.x; 31 } 32 //点积 33 double operator *(const Point &b)const 34 { 35 return x*b.x + y*b.y; 36 } 37 //绕原点旋转角度B(弧度值),后x,y的变化 38 void transXY(double B) 39 { 40 double tx = x,ty = y; 41 x = tx*cos(B) - ty*sin(B); 42 y = tx*sin(B) + ty*cos(B); 43 } 44 }; 45 46 //*两点间距离 47 double dist(Point a,Point b) 48 { 49 return sqrt((a-b)*(a-b)); 50 } 51 52 //两个圆的公共部分面积 53 double Area_of_overlap(Point c1,double r1,Point c2,double r2) 54 { 55 double d = dist(c1,c2); 56 if(r1 + r2 < d + eps)return 0; 57 if(d < fabs(r1 - r2) + eps) 58 { 59 double r = min(r1,r2); 60 return PI*r*r; 61 } 62 double x = (d*d + r1*r1 - r2*r2)/(2*d); 63 double t1 = acos(x / r1); 64 double t2 = acos((d - x)/r2); 65 return r1*r1*t1 + r2*r2*t2 - d*r1*sin(t1); 66 } 67 68 int main() 69 { 70 //freopen("in.txt","r",stdin) ; 71 int T ; 72 scanf("%d" , &T) ; 73 int Case = 0; 74 while(T--) 75 { 76 Case++ ; 77 double r , R ; 78 double x1 , y1 , x2 , y2 ; 79 scanf("%lf%lf%lf%lf%lf%lf" , &r , &R , &x1 , &y1 , &x2 , &y2) ; 80 Point c1(x1 ,y1) ; 81 Point c2(x2 ,y2) ; 82 double ans = 0 ; 83 ans = Area_of_overlap(c1,R,c2,R) + Area_of_overlap(c1,r,c2,r); 84 ans -= Area_of_overlap(c1,r,c2,R) ; 85 ans -= Area_of_overlap(c1,R,c2,r) ; 86 printf("Case #%d: %.6lf\n" , Case , ans) ; 87 88 } 89 return 0; 90 }