题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5120
题目:
题意:
求两个圆环相交的面积。
思路:
两个大圆面积交-2×大圆与小圆面积交+两小圆面积交。
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <deque> 4 #include <ctime> 5 #include <stack> 6 #include <cmath> 7 #include <queue> 8 #include <string> 9 #include <cstdio> 10 #include <vector> 11 #include <iomanip> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 17 typedef long long LL; 18 typedef pair<LL, LL> pll; 19 typedef pair<LL, int> pli; 20 typedef pair<int, int> pii; 21 typedef unsigned long long uLL; 22 23 #define lson rt<<1 24 #define rson rt<<1|1 25 #define name2str(name)(#name) 26 #define bug printf("**********\n"); 27 #define IO ios::sync_with_stdio(false); 28 #define debug(x) cout<<#x<<"=["<<x<<"]"<<endl; 29 #define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin); 30 31 const double eps = 1e-8; 32 const int maxn = 1e5 + 7; 33 const int inf = 0x3f3f3f3f; 34 const double pi = acos(-1.0); 35 const LL INF = 0x3f3f3f3f3f3f3f3fLL; 36 37 int t, r, R; 38 int x1, x2, yy, y2; 39 40 double cirinter(int x1,int y1,int r1,int x2,int y2,int r2)//圆交面积公式 41 { 42 double d,s,t,a1,a2,s1,s2,s3; 43 if(r1<r2) swap(r1, r2); 44 d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); //两圆心距离 45 if(d>=r1+r2)return 0; //两圆相离 46 else if(d<=(r1-r2)) //两圆内含 47 s=pi*r2*r2; 48 else { //两圆相交 49 a1=acos((r1*r1+d*d-r2*r2)/(2*r1*d));//大圆中扇形圆心角的一半 50 a2=acos((r2*r2+d*d-r1*r1)/(2*r2*d));//小圆中扇形圆心角的一半 51 s1=a1*r1*r1;//大圆中的那个扇形面积 52 s2=a2*r2*r2;//小圆中的那个扇形面积 53 s3=r1*sin(a1)*d;//两圆心与两交点组成的四边形面积 54 s=s1+s2-s3;//圆交面积 55 } 56 return s; 57 } 58 59 int main() { 60 #ifndef ONLINE_JUDGE 61 FIN; 62 #endif 63 scanf("%d", &t); 64 int icase = 0; 65 while(t--) { 66 scanf("%d%d", &r, &R); 67 scanf("%d%d%d%d", &x1, &yy, &x2, &y2); 68 double ans = cirinter(x1, yy, R, x2, y2, R) - 2 * cirinter(x1, yy, R, x2, y2, r) + cirinter(x1, yy, r, x2, y2, r); 69 printf("Case #%d: %.6f\n", ++icase, ans); 70 } 71 return 0; 72 }