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.
Input
The 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.
Output
For 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
用容斥定律推出公式,两个圆环相交的面积 = 两个大圆相交的面积 - 2 * 大圆和小圆相交的面积 + 两个小圆相交的面积
#include <bits/stdc++.h>
#define PI 3.14159265358979323846
using namespace std;
struct Round
{
int x,y;
int rr;
}yuan[5];
double dis(Round a,Round b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double solve(Round a,Round b)
{
double d=dis(a,b);
if (d>= a.rr+b.rr)
return 0;
if (d<=abs(a.rr-b.rr))
{
double r=a.rr<b.rr?a.rr:b.rr;
return PI*r*r;
}
double ang1=acos((a.rr*a.rr+d*d-b.rr*b.rr)/2./a.rr/d);
double ang2=acos((b.rr*b.rr+d*d-a.rr*a.rr)/2./b.rr/d);
double ret=ang1*a.rr*a.rr+ang2*b.rr*b.rr-d*a.rr*sin(ang1);
return ret;
}
int main()
{
int t;
int r,R;
int x1,y1,x2,y2;
double sum1,sum2,sum3,sum4;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%d%d",&r,&R);
scanf("%d%d",&x1,&y1);
scanf("%d%d",&x2,&y2);
yuan[1].x=x1;yuan[1].y=y1;yuan[1].rr=r;//小
yuan[2].x=x1;yuan[2].y=y1;yuan[2].rr=R;//大
yuan[3].x=x2;yuan[3].y=y2;yuan[3].rr=r;//小
yuan[4].x=x2;yuan[4].y=y2;yuan[4].rr=R;//大
sum1=solve(yuan[2],yuan[4]);
sum2=solve(yuan[1],yuan[4]);
sum3=solve(yuan[1],yuan[3]);
sum4=sum1-2*sum2+sum3;
printf("Case #%d: %.6lf\n",i,sum4);
}
return 0;
}