hdu1798: Tell me the area

hdu1798: http://acm.hdu.edu.cn/showproblem.php?pid=1798
题意:给出两个圆的圆心坐标和半径,求两圆相交面积
code:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
const double pi=acos(double(-1));
double min(double x,double y)
{
    if(x>y)
        return y;
    else
        return x;
}
int main()
{
    double x1,y1,r1,x2,y2,r2,ans;
    while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2)!=EOF)
    {
        double d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        if(d>=r1+r2)
            printf("0.000\n");
        else if(d<=fabs(r1-r2))
        {
            ans=pi*min(r1,r2)*min(r1,r2);
            printf("%.3f\n",ans);
        }
        else
        {
            double k1=acos((r1*r1+d*d-r2*r2)/(2.0*r1*d)),k2=acos((r2*r2+d*d-r1*r1)/(2.0*r2*d));
            ans=k1*r1*r1-r1*r1*sin(k1)*cos(k1)+k2*r2*r2-r2*r2*sin(k2)*cos(k2);
            printf("%.3f\n",ans);
        }
    }
}
/*input:
0 0 2
2 2 1
output:
0.108
*/

posted on 2012-07-25 20:50  acmer-jun  阅读(206)  评论(0编辑  收藏  举报

导航