Tell me the area(計算ジオメトリ--2円の交差面積を求める)
Problem Link:http://acm.hdu.edu.cn/showproblem.php?pid=1798
Tell me the area
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2828 Accepted Submission(s): 903
Problem Description
There are two circles in the plane (shown in the below picture), there is a common area between the two circles. The problem is easy that you just tell me the common area.
Input
There are many cases. In each case, there are two lines. Each line has three numbers: the coordinates (X and Y) of the centre of a circle, and the radius of the circle.
Output
For each case, you just print the common area which is rounded to three digits after the decimal point. For more details, just look at the sample.
Sample Input
Tell me the area
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2828 Accepted Submission(s): 903
Problem Description
There are two circles in the plane (shown in the below picture), there is a common area between the two circles. The problem is easy that you just tell me the common area.
Input
There are many cases. In each case, there are two lines. Each line has three numbers: the coordinates (X and Y) of the centre of a circle, and the radius of the circle.
Output
For each case, you just print the common area which is rounded to three digits after the decimal point. For more details, just look at the sample.
Sample Input
0 0 2 2 2 1
Sample Output
0.108
Author
wangye
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(4)
AC code:
#include
#include
#include
#include
#define PI acos(-1)
using namespace std;
int main()
{
int T;
double r1,r2,x1,x2,y1,y2,dr,r,A1,A2,p,s,s1,s2,ans;
while(scanf("%lf%lf%lf",&x1,&y1,&r1)!=EOF)
{
scanf("%lf%lf%lf",&x2,&y2,&r2);
dr=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
r=min(r1,r2);
if(dr>=r1+r2)
printf("0.000
");
else if(dr<=fabs(r1-r2) && dr>=0)
{
ans=PI*r*r;
printf("%0.3lf
",ans);
}
else
{
A1=acos((r1*r1+dr*dr-r2*r2)/(2*r1*dr));
A2=acos((r2*r2+dr*dr-r1*r1)/(2*r2*dr));
p=(r1+r2+dr)/2;
s=sqrt(p*(p-r1)*(p-r2)*(p-dr));
s1=s-PI*r2*r2*(A2/(2*PI));
s2=s-PI*r1*r1*(A1/(2*PI));
ans=2*(s-s1-s2);
printf("%.3lf
",ans);
}
}
return 0;
}