Intersection
Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 400 Accepted Submission(s): 167
Problem Description
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.
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.
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
题意:让我们求的是两个圆环的面积。
做法:
S相交=S(大圆1交大圆2)+S(小圆1交小圆2)-S(大圆1交小圆2)-S(大圆2交小圆1)
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x0f0f0f0f
using namespace std;
const double PI = acos(-1.0);
struct node
{
double x;
double y;
double r;
}c[5];
double area(int i,double r1,int j,double r2)
{
double d=sqrt((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y-c[j].y)*(c[i].y-c[j].y));//圆心距
if(r1>r2)
{
double temp=r1;
r1=r2;
r2=temp;
}//r1取小
if(r1+r2<=d)
return 0;//相离
else if(r2-r1>=d)
return PI*r1*r1;//内含
else
{
double a1=acos((r1*r1+d*d-r2*r2)/(2.0*r1*d));
double a2=acos((r2*r2+d*d-r1*r1)/(2.0*r2*d));
return (a1*r1*r1+a2*r2*r2-r1*d*sin(a1));
}//相交
}
int main()
{
int r,R,i,j,k,l,m,n,T,x1,y1,x2,y2,cas=1;
double ans,s1d,s2d,s1x,s2x,s1ds2x,s1xs2d,s1ds2d,s1xs2x;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&r,&R);
scanf("%d%d",&x1,&y1);
scanf("%d%d",&x2,&y2);
s1d=s2d=PI*R*R;
s1x=s2x=PI*r*r;
c[0].x=c[1].x=x1*1.0;
c[0].y=c[1].y=y1*1.0;
c[0].r=c[2].r=R*1.0;
c[1].r=c[3].r=r*1.0;
c[2].x=c[3].x=x2*1.0;
c[2].y=c[3].y=y2*1.0;
s1ds2x=area(0,R,3,r);
s1xs2d=area(1,r,2,R);
s1ds2d=area(0,R,2,R);
s1xs2x=area(1,r,3,r);
ans=fabs(s1ds2x+s1xs2d-s1ds2d-s1xs2x);
// printf("大圆1 %.6f\n 大圆2 %.6f\n 小圆1 %.6f\n 小圆2 %.6f\n 大圆1小圆2 %.6f\n 小圆1大圆2 %.6f\n 大圆1大圆2 %.6f\n 小圆1小圆2 %.6f\n",s1d,s2d,s1x,s2x,s1ds2x,s1xs2d,s1ds2d,s1xs2x);
printf("Case #%d: %.6f\n",cas++,ans);
}
}