两圆相交面积万能版

struct point{
    double x, y;
};
double dis(point a, point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Area(point c1, double r1, point c2, double r2){
    double tmpa = r1, tmpb = r2;
    if(tmpa < tmpb) swap(tmpa,tmpb);
    double res = 0;
    double dd = dis(c1, c2);
    if(dd > tmpa -tmpb && dd < tmpa + tmpb){
        double cos1 = (tmpa*tmpa+dd*dd-tmpb*tmpb)/(2*tmpa*dd);
        double cos2 = (tmpb*tmpb+dd*dd-tmpa*tmpa)/(2*tmpb*dd);
        double th1 = 2*acos(cos1);
        double th2 = 2*acos(cos2);
        double s1 = 0.5*tmpa*tmpa*sin(th1);
        double s2 = 0.5*tmpb*tmpb*sin(th2);
        double s3 = (th1/2)*tmpa*tmpa;
        double s4 = (th2/2)*tmpb*tmpb;
        res = s3+s4-s1-s2;
    }
    else if(dd <= tmpa - tmpb)
        res = pi*tmpb*tmpb;
    return res;
}

你可能感兴趣的:(c语言,=====数学=====)