求两圆相交面积

本文提供了一种计算平面上两个圆交叠区域面积的方法。通过输入两个圆的坐标及半径,利用数学公式计算并输出交叠部分的面积,结果保留三位小数。

 

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

π=cos(-1.0) 弧度=角度*π/180

#include <stdio.h>
#include <math.h>
#define P acos(-1.0)
int main()
{
    double x1,y1,R,r,x2,y2,z1,z2,s1,s2,ans,m1,m2,d;
    while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&R,&x2,&y2,&r)!=EOF)
    {
        d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        if(d>=R+r||R==0||r==0)//相离
        {
            printf("0.000\n");
            continue;
        }
        else
        if(d<=fabs(r-R)&&d>=0)//相切
        {
            if(r<R)
                ans=P*r*r;
            else
                ans=P*R*R;
            printf("%.3lf\n",ans);
        }
        else
        {
            z1=acos((R*R+d*d-r*r)/(2*R*d));//余弦定理求第一个扇形一半弧度
            z2=acos((r*r+d*d-R*R)/(2*r*d));
            s1=z1*R*R;//扇形面积
            s2=z2*r*r;
            m1=R*R*sin(z1)*cos(z1);//求三角形面积
            m2=r*r*sin(z2)*cos(z2);
            ans=s1+s2-m1-m2;
            printf("%.3lf\n",ans);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zcy19990813/p/9702835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值