HDU 1798 Tell me the area (计算几何)

Tell me the area

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1801    Accepted Submission(s): 542


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
 

 

Author
wangye
 

 

Source
 

 

Recommend
wangye   |   We have carefully selected several similar problems for you:  1797 1793 1796 1795 1794 
 

 

利用余弦公式计算出角度,然后通过扇形和三角形的关系求出答案即可

#include<cmath>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
double distance(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
    //freopen("in.txt","r",stdin);
    double PI=2*asin(1.0);
    double x1,y1,r1;
    double x2,y2,r2;
    double dis,r,cosa,cosb;
    double a,b,c,d,ans;
    while(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&r1,&x2,&y2,&r2)!=EOF)
    {
        dis=distance(x1,y1,x2,y2);
        if(dis>=r1+r2||r1==0||r2==0)//两圆相离,外切时,相交面积为0
            ans=0;
        else if(dis<=fabs(r1-r2))//两圆内含,内切时,相交面积为较小圆的面积
        {
            r=r1<r2?r1:r2;
            ans=r*r*PI;
        }
        else//两圆相交时
        {
            cosa=acos((r1*r1+dis*dis-r2*r2)/(2*r1*dis));
            cosb=acos((r2*r2+dis*dis-r1*r1)/(2*r2*dis));//余弦公式求出两圆连线和半径的夹角
            a=r1*r1*cosa;
            b=r2*r2*cosb;//扇形面积
            c=r1*r1*cos(cosa)*sin(cosa);
            d=r2*r2*cos(cosb)*sin(cosb);//三角形面积
//            s1=0.5*r1*r1*(cos1-sin(cos1));
//            s2=0.5*r2*r2*(cos2-sin(cos2));
            ans=(a+b)-(c+d);//这里用两个扇形的面积之和减去三角形之和,是因为相交的情况分为两个圆的圆心是否在同一个圆里两种情况
        } 
        printf("%.3lf\n",ans);
    }
    return 0;
}

 

posted @ 2014-08-21 16:48  Cliff Chen  阅读(181)  评论(0编辑  收藏  举报