1. 程式人生 > >幾何問題(求兩圓相交的面積)

幾何問題(求兩圓相交的面積)

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);
        }
    }
}