HDU 5120 Intersection——两圆面积并

题意:求两个圆环的面积并

思路:设两个圆环为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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值