Tell me the area(思维题)


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

题目大意:相交圆的重叠面积,主要分为相离,相交和内含三种情况,将其分别讨论就行了。刚开始w,后来才发现是多组输入,希望这能帮到各位大佬。

注意:求qaq的时候要注意计算顺序,否则会造成计算误差,导致w。

#include <iostream>
#include <math.h>
#include <stdio.h>
#define PI acos(-1)
using namespace std;
int main()
{
    double x1,x2,y1,y2,r1,r2;
    while(~scanf("%lf%lf%lf",&x1,&y1,&r1))
    {
        scanf("%lf%lf%lf",&x2,&y2,&r2);
        double k=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        if(k>=r1+r2)
            printf("0.000\n");
        else if(k<=fabs(r1-r2)&&k>=0)
            if(r1>r2)
                printf("%0.3lf\n",PI*r2*r2);
            else
                printf("%0.3lf\n",PI*r1*r1);
        else
        {
            double h,s,l1,l2,qaq,j;
            h=(r1+r2+k)/2;
            s=sqrt(h*(h-r1)*(h-r2)*(h-k));//海伦公式啊!
            s*=2.0;
            l1=acos((k*k+r1*r1-r2*r2)/(2*k*r1));//反三角函数求弧度角
            l2=acos((k*k-r1*r1+r2*r2)/(2*k*r2));
            qaq=PI*r2*r2*(l2/(2*PI))*2+PI*r1*r1*(l1/(2*PI))*2-s;//两个扇形减去未重叠部分三角形面积
            printf("%.3lf\n",qaq);
        }
    }
    return 0;
}















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值