Tell me the area
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1361 Accepted Submission(s): 426
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
AC代码:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
double x1,y1,r1,x2,y2,r2,s,PI;
PI=2*asin(1.0); //PI不能写成3.1415926
while(cin>>x1>>y1>>r1)
{
cin>>x2>>y2>>r2;
double d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
if(d>=(r1+r2)||!r1||!r2) //外离
s=0;
else if(d<=abs(r2-r1)) //内含
{
if(r2<r1)
r1=r2;
s=r1*r1*PI;
}
else //相交
{
double a1=acos((r1*r1+d*d-r2*r2)/(2.0*r1*d)); //a1,a2分别为两扇形圆心角的一半
double a2=acos((r2*r2+d*d-r1*r1)/(2.0*r2*d));
s=a1*r1*r1+a2*r2*r2-r1*r1*sin(a1)*cos(a1)-r2*r2*sin(a2)*cos(a2); //相交面积等于两扇形面积之和减四边形
}
printf("%.3lf\n",s);
}
return 0;
}

本文介绍了一道关于计算平面中两个圆交叠部分面积的编程题,通过数学方法求解两圆相交时的公共区域面积,并给出了使用C++实现的完整代码示例。

316

被折叠的 条评论
为什么被折叠?



