Tell me the area HDU 1798

本文介绍了一道关于计算平面上两个圆的公共区域面积的数学问题。通过判断两圆的位置关系(相离、相交、包含),利用数学方法计算公共区域面积。包括使用余弦定理确定角度、三角形面积公式及扇形面积公式。

Tell me the area
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2958 Accepted Submission(s): 946

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

[分析]
数学题。
首先判断相离相交还是包含(是叫包含吗?有点忘记了)。
相离就0.00,包含就输出小圆的面积。
相交先算出角A和角B的角度,算出2个扇形面积,减去两个三角形面积。
最后有注释。

[代码]

#include<cstdio>
#include<cmath>
#define PI acos(-1)
int main()
{
    double x1, y1, r1;
    double x2, y2, r2;
    while (scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &r1, &x2, &y2, &r2) != EOF)
    {
        double dis = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
        if (dis >= r1 + r2)printf("0.000\n");//相离
        else if (dis <= fabs(r1 - r2))//包含
        {
            if (r1 > r2)printf("%.3lf\n", PI*r2*r2);
            else printf("%.3lf\n", PI*r1*r1);
        }
        else//相交
        {
            double a1 = 2 * acos((dis*dis + r1*r1 - r2*r2) / (2 * dis*r1));//注释1
            double a2 = 2 * acos((dis*dis + r2*r2 - r1*r1) / (2 * dis*r2));
            double sanjiao = 0.5*r1*r1*sin(a1) + 0.5*r2*r2*sin(a2);//注释2
            double ans = 0.5*r1*r1*a1 + 0.5*r2*r2*a2 - sanjiao;//注释3
            printf("%.3lf\n", ans);
        }
    }
}

这里写图片描述
[注释1]首先使用余弦定理算出角A
这里写图片描述
不过因为cd不知道长度所以只好改算角cab。最后乘2就是角A的大小了。

[注释2]三角形面积公式1/2*a*b*sin(c);

[注释3]扇形面积公式1/2*r*r*a;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值