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

 

 

主要分三种情况:

1、两圆相离或者是相交 面积都是 0(特殊的是,要考虑 两圆中半径至少有一个是 0 的情况,此时的面积是 0)

 

 

2、两圆内含的情况 面积是半径较小的那个圆的面积(包含两个圆重合的情况)

3、两圆相切的情况 应用了 余弦定理,扇形面积公式

 

    角度制计算

    几何问题(求两圆相交的面积)

, l是弧长,n是扇形圆心角,π是圆周率,R是扇形半径

    弧度制计算

    几何问题(求两圆相交的面积) (L为弧长,R为扇形半径)

    S=1/2*a*r*r (a 是角度)

 

 

     输出结果保留 三位数

我的错误:

几何问题(求两圆相交的面积)

 

 

我一开始想的是 求出来余弦 再求 正弦 利用 开方 但是一直 WA 后来减少了开方的步骤,因为 double 本身就是不精确的,再开方可能更不精确,结果可能不符合

!!!! π 在代码中是 acos(-1)

几何问题(求两圆相交的面积)

代码如下

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    double x,y,r,x1,y1,r1;
    double s,s1,s2,s3,s4,s5,s6;
    double b,b1,b2,c,c1,c2;
    double d,d1,minn;
    while(scanf("%lf %lf %lf",&x,&y,&r)!=EOF){
        scanf("%lf %lf %lf",&x1,&y1,&r1);
        d=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));//两圆心的距离
        if(r>=r1){ //求最小的 半径 以及 半径的差 (为了讨论 两圆内含的情况)
            d1=r-r1;
            minn=r1;
        }
        else{
            d1=r1-r;
            minn=r;
        }
        if(d>=r+r1||r==0||r1==0)//相离或相切 或半径至少一个是 0
            printf("0.000\n");
        else if(d<=d1&&d>=0){
            s=acos(-1)*minn*minn;//内含的 π*r*r
            printf("%0.3lf\n",s);
        }
        else{
            b=(r*r+d*d-r1*r1)/(2*r*d); // 求出来 ∠oAB 的余弦
            c=(r1*r1+d*d-r*r)/(2*r1*d); ∠OBA 的余弦
            b1=2*acos(b); //∠OAB==∠O1AB 所以b1=∠OAO1=2*∠OAB     acos(b) 是∠OAB
            c1=2*acos(c);//同上
            s1=(double)1/2*r*r*sin(b1); //三角形面积公式
            s2=(double)1/2*r1*r1*sin(c1);
            s3=(double)1/2*b1*r*r;// 扇形面积公式  b1 是角度
            s4=(double)1/2*c1*r1*r1;
            s=(s3+s4)-(s1+s2);//阴影面积公式=Soao1o+Sobo1o-Soao1b(两个扇形的面积-四边形的面积)
            printf("%0.3lf\n",s);
        }
    }
}

 

 

 

 

 

 

相关文章: