hdu 5120 圓相交面積+組合數學

http://acm.hdu.edu.cn/showproblem.php?pid=5120

圓環的交 = 大圓交-2個大小圓交+小圓交

最討厭這些模板題!幹.

<span style="font-size:14px;">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 1000000000;
const int maxn = 1e6+5;

const double Pi=acos(-1.0);

double Rad2Deg(double rad){
    return (rad*180.0/Pi);
}

double Deg2Rad(double deg){
    return (deg*Pi/180.0);
}

double ArcCos(double val){
    return Rad2Deg(acos(val));
}

double Sin(double deg){
    return sin(Deg2Rad(deg));
}

struct CIRCLE{
    double x;
    double y;
    double r;
    CIRCLE(){}
    CIRCLE(double xx,double yy,double rr){
        x=xx; y=yy; r=rr;
    }
};

struct POINT{
    double x;
    double y;
    POINT():x(0),y(0){};
    POINT(double xx,double yy):x(xx),y(yy){    }
};

double Distance(const POINT& a,const POINT& b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

POINT Center(const CIRCLE& circle){
    return POINT(circle.x,circle.y);
}

double Area(const CIRCLE& circle){
    return Pi*circle.r*circle.r;
}

double CommonArea(const CIRCLE& A,const CIRCLE &B){
    double area=0.0;
    const CIRCLE& M=(A.r>B.r)?A:B;
    const CIRCLE& N=(A.r>B.r)?B:A;
    double D=Distance(Center(M),Center(N));
    if((D<M.r+N.r)&&(D>M.r-N.r)){
        double cosM=(M.r*M.r+D*D-N.r*N.r)/(2.0*M.r*D);
        double cosN=(N.r*N.r+D*D-M.r*M.r)/(2.0*N.r*D);
        double alpha=2.0*ArcCos(cosM);
        double beta=2.0*ArcCos(cosN);

        double TM=0.5*M.r*M.r*Sin(alpha);
        double TN=0.5*N.r*N.r*Sin(beta);
        double FM=(alpha/360.0)*Area(M);
        double FN=(beta/360.0)*Area(N);
        area=FM+FN-TM-TN;
    }else if(D<=M.r-N.r){
        area=Area(N);
    }
    return area;
}

double work()
{
    double r,R;
        double x1,y1;
        double x2,y2;
        //cin>>r>>R>>x1>>y1>>x2>>y2;
        scanf("%lf%lf%lf%lf%lf%lf",&r,&R,&x1,&y1,&x2,&y2);
        CIRCLE cr1(x1,y1,r);
        CIRCLE cr2(x2,y2,r);

        CIRCLE cR1(x1,y1,R);
        CIRCLE cR2(x2,y2,R);
        double ans=CommonArea(cR1,cR2)+CommonArea(cr1,cr2);
        ans-=CommonArea(cr1,cR2);
        ans-=CommonArea(cR1,cr2);
    return ans;
}
int main()
{
    int _,cas = 1;
    RD(_);
    while(_--){
        printf("Case #%d: %.6lf\n",cas++,work());
    }
    return 0;
}
/*

*/
</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章