题意:给定两个圆环,求两个圆环的面积交。
析:很容易知道,圆环面积交就是,大圆与大圆面积交 - 大圆和小圆面积交 - 小圆和大圆面积交 + 小圆和小圆面积交。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 10 + 5; const int mod = 1000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r > 0 && r <= n && c > 0 && c <= m; } int cmp(double x){ if(fabs(x) < eps) return 0; return x > 0 ? 1 :-1; } struct Circle { double x, y; double r; }; Circle big1, big2, small1, small2; double dis(const Circle &a, const Circle &b){ return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double solve(const Circle &a, const Circle &b){ double d = dis(a,b); if(d >= a.r + b.r) return 0; if(d <= fabs(a.r - b.r)){ double r = a.r < b.r ? a.r : b.r; return PI * r * r; } double ang1 = acos((a.r * a.r + d * d - b.r * b.r)/2. / a.r / d); double ang2 = acos((b.r * b.r + d * d - a.r * a.r)/2. / b.r / d); double ret = ang1 * a.r * a.r + ang2 * b.r * b.r - d * a.r * sin(ang1); return ret; } int main(){ int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ scanf("%lf %lf", &small1.r, &big1.r); small2.r = small1.r; big2.r = big1.r; scanf("%lf %lf", &small1.x, &small1.y); scanf("%lf %lf", &small2.x, &small2.y); big1.x = small1.x; big1.y = small1.y; big2.x = small2.x; big2.y = small2.y; double ans = solve(big1, big2) - solve(big1, small2) - solve(big2, small1) + solve(small1, small2); printf("Case #%d: %.6f\n", kase, ans); } return 0; }