1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const double PI = acos(-1.0); 5 const double eps = 1e-8; 6 int dblcmp (double k) 7 { 8 if (fabs(k)<eps) return 0; 9 return k>0?1:-1; 10 } 11 struct Point 12 { 13 double x,y; 14 }; 15 double dis (Point a,Point b) 16 { 17 return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 18 } 19 double area_of_overlap (Point c1,double r1,Point c2,double r2)//圆相交模板 20 { 21 double d = dis(c1,c2); 22 if (r1+r2<d+eps) return 0; 23 if (d<fabs(r1-r2)+eps){ 24 double r = min(r1,r2); 25 return PI*r*r; 26 } 27 double x = (d*d+r1*r1-r2*r2)/(2*d); 28 double t1 = acos(x/r1); 29 double t2 = acos((d-x)/r2); 30 return r1*r1*t1+r2*r2*t2-d*r1*sin(t1); 31 } 32 int t; 33 int casee = 0; 34 int main() 35 { 36 //freopen("de.txt","r",stdin); 37 scanf("%d",&t); 38 while (t--){ 39 Point p1,p2; 40 double r1,r2; 41 scanf("%lf%lf",&r1,&r2); 42 scanf("%lf%lf",&p1.x,&p1.y); 43 scanf("%lf%lf",&p2.x,&p2.y); 44 if (dblcmp(r1-r2)>0) swap(r1,r2); 45 double ans = area_of_overlap (p1,r2,p2,r2) -area_of_overlap (p1,r2,p2,r1) 46 -(area_of_overlap (p1,r1,p2,r2) - area_of_overlap(p1,r1,p2,r1) ); 47 /*double ans = area (p1,r2,p2,r2) -area (p1,r2,p2,r1) 48 -(area(p1,r1,p2,r2) - area(p1,r1,p2,r1) );*/ 49 printf("Case #%d: %.6f\n",++casee,ans); 50 } 51 return 0; 52 }
hdu 5120 Intersection (圆环面积相交->圆面积相交)
原文:http://www.cnblogs.com/agenthtb/p/7643492.html