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*大圆和小圆相交的面积。
注意精度!pi的值多取几位
代码如下
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
using namespace std;
const double pi=3.141592654;
double intersect(double x1,double y1,double r1,double x2,double y2,double r2){
double s,temp,p,l,ans;
l=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
if(l>=r1+r2) ans=0;
else if(l<=abs(r1-r2)){
if(r1<=r2) ans=pi*r1*r1;
else ans=pi*r2*r2;
}
else{
p=(l+r1+r2)/2;
s=2*sqrt(p*(p-l)*(p-r1)*(p-r2));
if(r1>r2){
temp=x1;x1=x2;x2=temp;
temp=y1;y1=y2;y2=temp;
temp=r1;r1=r2;r2=temp;
}
ans=acos((r1*r1+l*l-r2*r2)/(2*r1*l))*r1*r1+acos((r2*r2+l*l-r1*r1)/(2*r2*l))*r2*r2-s;
}
return ans;
}
int main()
{
int T;
int n;
int r,R;
int x1,y1,x2,y2;
double ans;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
ans=0;
scanf("%d%d",&r,&R);
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
ans+=intersect(x1,y1,R,x2,y2,R)+intersect(x1,y1,r,x2,y2,r);
ans-=2*intersect(x1,y1,R,x2,y2,r);
printf("Case #%d: %.6lf\n",cas,ans);
}
return 0;
}