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)
?
对于平面内,任意两个圆,存在这些关系: 内含和内切,以及相交,外切和外离。
(1)对于内切,我们只需要求出面积最小圆的面积,
(2)对于外切及外离,得到的面积必然为0.0;
?(3)对于相交,那么我们需要求出这些
? ? ? ? ? : 知道两点坐标: 求出dist两点之间的距离;
? ? ? ? ? ? ? 知道三边,可以求出三边对应的角度: a^2+b^2-2*a*b*cos(g)=dist^2;
? ? ? ? 对于四边形的面积: sm=s3(三角形的面积)*2;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? s3=sqrt(p*(p-r1)*(p-r2)*(p-d));
? ? 然后求出对应两个扇形的面积:s1,s2 ?依据: s=1/2*g*r*r;
? ? ?最后: ?s=s1+s2-sm;
? ? 代码:
?1 #include
?2 #include
?4 #include
?5 using namespace std;
?6?
?7 struct circle
?8 {
?9 ? double x,y,r;
10 };
11 double dist(circle a,circle b)
12 {
13 ? return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
14 }
15 int main()
16 {
17 ? ?circle a,b;
18 ? ?double d,p,area,sb,sa;
19 ? while(scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&a.r,&b.x,&b.y,&b.r)!=EOF)
20 ? {
21 ? ? d=dist(a,b);
22 ? ? double rr=min(a.r,b.r);
23 ? ? if(d<=abs(a.r-b.r)) ? //内含或者内切
24 ? ? ? ?area=acos(-1.0)*rr*rr;
25 ? ? else
26 ? ? ? ?if(d>=a.r+b.r)
27 ? ? ? ? ? area=0.0;
28 ? ? else{
29 ? ? ? ? ?p=(a.r+b.r+d)/2.0;
30 ? ? ? sa=acos((a.r*a.r+d*d-b.r*b.r)/(2.0*a.r*d));
31 ? ? ? sb=acos((b.r*b.r+d*d-a.r*a.r)/(2.0*b.r*d));
32 ? ? ? ? area=sa*a.r*a.r+sb*b.r*b.r-2*sqrt(p*(p-a.r)*(p-b.r)*(p-d));
33 ? ? }
34 ? ? printf("%.3lf\n",area);
35 ? }
36 ?return 0;
37 }