hdu---(Tell me the area)(幾何/三角形面積以及圓面積的一些知識)
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<cstdio>
2 #include<cmath>
3 #include<algorithm>
4 #include<iostream>
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 }