Tell me the area
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1733 Accepted Submission(s): 526
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.
![](https://i-blog.csdnimg.cn/blog_migrate/03bcfdcb07fbaba3f2cc5a0c9005d73f.jpeg)
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
题目链接:题目链接
此题是求两圆相交部分的面积。可分三大情况讨论:
一、两圆相离、外切或至少有一圆半径为0:所求面积为0。
二、两圆内切、内含:所求面积为小圆面积。
三、两圆相交:这种情况分两种小情况:1、两圆心在公共弦的异侧,如图1所示;2、两圆心在公共弦的同侧如图2所示。先看图1,阴影部分可由公共弦AB分成两个弓形,求出两个弓形的面积相加即可,即S(阴影) =S(扇形O1AB)-S(三角形O1AB)+S(扇形O2AB)-S(三角形O2AB)=S(扇形O1AB)+S(扇形O2AB)-S(四边形O1AO2B),即两扇形面积和与四边面积之差。再来看图2,这时所求面积为:S(扇形O1AB)-S(三角形O1AB)+S(扇形O2AB<这里的扇形为圆心角为2*y的扇形>)+S(三角形O2AB)=S(扇形O1AB)+S(扇形O2AB)-S(四边形O1AO2B),同样为两扇形面积和与四边面积之差。因此这两种小情况不必分开讨论。(图中a为圆心距,c为圆O1的半径,z为圆O2的半径,b为角AO1O2的大小,y为角AO2O1大小,A、B为公共弦的两端点,O1、O2为两圆的圆心)
我的代码
#include<stdio.h>
#include<math.h>
int main()
{
double x1,x2,y1,y2,r1,r2;
double pi=2*asin(1.0);
while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2)!=EOF)
{
double rs=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),s,p,q,h1,h2,s1,s2;
if(rs>=r1+r2||!r1||!r2)//情况要考虑完全注
s=0;
else
if(rs<=fabs(r1-r2))
{
if(r1>=r2)
s=pi*r2*r2;
else
s=pi*r1*r1;
}
else
{
p=acos((rs*rs+r1*r1-r2*r2)/(2*rs*r1));
q=acos((rs*rs+r2*r2-r1*r1)/(2*rs*r2));
h1=p*r1*r1;
h2=q*r2*r2;
s1=r1*r1*cos(p)*sin(p);
s2=r2*r2*cos(q)*sin(q);
s=h1+h2-(s1+s2);
}
printf("%.3lf\n",s);
}
}
网上的代码
#include<stdio.h>
#include<math.h>
int main()
{
double q,w,m,n,a,b,c,x,y,z,PI;
PI=2*asin(1.0);
while(~scanf("%lf%lf%lf",&a,&b,&c)){
scanf("%lf%lf%lf",&x,&y,&z);
a=sqrt((a-x)*(a-x)+(b-y)*(b-y));//计算圆心距
//如果两圆相离、外切或至少一圆半径为0时,那么所求面积为0
if(a>=c+z||!c||!z)x=0;
//如果两内切或内含,那么所求面积为小圆面积
else if(a<=fabs(z-c)){
if(z>c)z=c;
x=z*z*PI;
}
//如果两圆相交,面积求解如下
else{
//由余弦定理求出公共弦在圆o1中对应的圆心角的一半
b=acos((a*a+c*c-z*z)/(2*a*c));
//由余弦定理求出公共弦在圆o2中对应的圆心角的一半
y=acos((a*a+z*z-c*c)/(2*a*z));
//计算圆o1中扇形面积
m=b*c*c;
//计算圆o2中扇形面积
n=y*z*z;
//计算圆o1中扇形所对应的三角形面积
q=c*c*sin(b)*cos(b);
//计算圆o2中扇形所对应的三角形面积
w=z*z*sin(y)*cos(y);
//q+w为图中四边形面积,两扇形面积之和与四边形面积之差即为
//所求面积。在图2中y为钝角,计算出的面积w为负值,这时q+w
//表示两三角面积之差,刚好还是四边形面积,因此对于图1和图
//2不必分情况讨论
x=m+n-(q+w);
}
printf("%.3f\n",x);
}
return 0;
}