题意:求两个圆环的面积并
思路:设两个圆环为S1 S2,S1 由 两个圆 c1(大) c2(小)组成,S2由两个圆c3(大) c4(小)组成,那么结果就是c1与c3的面积并减去c1与c4的面积并减去c3与c2的面积并加上c2与c4的面积并
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const double PI = acos(-1.0);
double cal(double r1, double x1, double y1, double r2, double x2, double y2) {
double d_2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
double d = sqrt(d_2);
if (1e-6 + d - r1 - r2 > 0) return 0.0;
if (d < fabs(r1 - r2) + 1e-6) {
double r = min(r1, r2);
return PI * r * r;
}
double d2 = (d_2 - r1 * r1 + r2 * r2) / (2.0 * d);
double d1 = d - d2;
double angle1 = 2.0 * acos(d1 / r1);
double angle2 = 2.0 * acos(d2 / r2);
double temp = sqrt(r1 * r1 - d1 * d1);
return angle1 * r1 * r1 / 2.0 - temp * d1 + angle2 * r2 * r2 / 2.0 - temp * d2;
}
int main() {
int T; scanf("%d", &T);
for (int kase = 1; kase <= T; kase++) {
double r, R, x1, y1, x2, y2;
scanf("%lf%lf%lf%lf%lf%lf", &r, &R, &x1, &y1, &x2, &y2);
printf("Case #%d: ", kase);
double ans1 = cal(R, x1, y1, R, x2, y2);
double ans2 = cal(R, x1, y1, r, x2, y2);
double ans3 = cal(r, x1, y1, R, x2, y2);
double ans4 = cal(r, x1, y1, r, x2, y2);
//cout << ans1 << " " << ans2 << " " << ans3 << " " << ans4 << endl;
printf("%.6lf\n", ans1 - ans2 - ans3 + ans4);
}
return 0;
}