HDU 1798 Tell me the area

本文介绍了一种计算平面上两个圆之间的公共区域面积的方法。通过解析输入的圆心坐标和半径,利用数学公式计算并输出精确到小数点后三位的交叠面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.hdu.edu.cn/showproblem.php?pid=1798

 

Problem Description
    There are two circles in the plane (shown in the below picture), there is a common area between the two circles. The problem is easy that you just tell me the common area.
 
Input
There are many cases. In each case, there are two lines. Each line has three numbers: the coordinates (X and Y) of the centre of a circle, and the radius of the circle.
 
Output
For each case, you just print the common area which is rounded to three digits after the decimal point. For more details, just look at the sample.
 
Sample Input
0 0 2
2 2 1
 
Sample Output
0.108

 

代码:

#include <bits/stdc++.h>
using namespace std;

int main() {
    double x1, y1, r1, x2, y2, r2, minr, maxr, d, ans;
    while(~scanf("%lf%lf%lf", &x1, &y1, &r1)) {
        scanf("%lf%lf%lf", &x2, &y2, &r2);
        d = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        minr = min(r1, r2);
        maxr = max(r1, r2);
        if(d <= maxr - minr)
            ans = acos(-1.0) * minr * minr;
        else if(d >= r1 + r2)
            ans = 0;
        else {
            double a1, a2;
            double p;
            p = (r1 + r2 + d) / 2;
            a1 = acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d));
            a2 = acos((r2 * r2 + d * d - r1 * r1) / (2 * r2 * d));
            ans = a1 * r1 * r1 + a2 * r2 * r2 - 2 * sqrt(p * (p - r1) * (p - r2) * (p - d));
        }
        printf("%.3f\n", ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/zlrrrr/p/9688159.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值