I

Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The following figures are some famous examples you may know.


A ring is a 2-D figure bounded by two circles sharing the common center. The radius for these circles are denoted by r and R (r < R). For more details, refer to the gray part in the illustration below.


Matt just designed a new logo consisting of two rings with the same size in the 2-D plane. For his interests, Matt would like to know the area of the intersection of these two rings.

InputThe first line contains only one integer T (T ≤ 10 5), which indicates the number of test cases. For each test case, the first line contains two integers r, R (0 ≤ r < R ≤ 10).

Each of the following two lines contains two integers x i, y i (0 ≤ x i, y i ≤ 20) indicating the coordinates of the center of each ring.OutputFor each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the area of intersection rounded to 6 decimal places.
Sample Input
2
2 3
0 0
0 0
2 3
0 0
5 0
Sample Output
Case #1: 15.707963
Case #2: 2.250778
/*
   题意:求两个圆环相交的面积

   思路:圆环相交面积=外圆1与外圆2相交面积-内圆1与外圆2相交面积-外圆1与内圆2相交面积+内圆1与内圆2相交面积
 */
#include <bits/stdc++.h>

#define MAXK 3
#define pi acos(-1)

using namespace std;

int t;
double r,R;
double x[MAXK],y[MAXK];

double dis(double a1,double b1,double a2,double b2){
    return sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));
}

double cal(double a1,double b1,double R,double a2,double b2,double r){
    double d=dis(a1,b1,a2,b2);
    if(d>=(R+r)){
        return 0.0;
    }else if(d<=fabs(R-r)){
        return min(pi*R*R,pi*r*r);
    }else{
        if(r>R){
            swap(a1,a2);
            swap(b1,b2);
            swap(R,r);
        }
        double ok=sqrt(R*R-r*r);
        double x=(R*R-r*r+d*d)/(2*d);
        x=sqrt(R*R-x*x);
        double s1=acos(1-(4*x*x)/(2*R*R));
        double s2=acos(1-(4*x*x)/(2*r*r));
        if(d>=ok){//相交的很小
            return (R*R*s1+r*r*s2-2*d*x)/2;
        }else{//相交的很大
            return pi*r*r-(r*r*s2-R*R*s1+2*d*x)/2;    
        }
    }

}
int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++){
        printf("Case #%d: ",ca);
        scanf("%lf%lf",&r,&R);
        for(int i=0;i<2;i++){
            scanf("%lf%lf",&x[i],&y[i]);
        }
        printf("%.6f
",cal(x[0],y[0],R,x[1],y[1],R)-
            cal(x[0],y[0],R,x[1],y[1],r)-
            cal(x[0],y[0],r,x[1],y[1],R)+
            cal(x[0],y[0],r,x[1],y[1],r));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7771265.html