首页 > 代码库 > HDU 1798 两圆相交面积

HDU 1798 两圆相交面积

Tell me the area

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1755    Accepted Submission(s): 535


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
 
 
题意:
给x1,y1,r1,x2,y2,r2分别为一个圆的原点坐标(x1,y1)和半径和另外一个圆的原点坐标和半径,求两圆相交面积。
 
思路:
只要上过初中的这道题应该都会把。
先求出两原点之间距离d,若d>=(r1+r2) 两圆相离,相交面积为0。若d<=max(r1,r2)-min(r1,r2),小圆在大圆内部,相交面积为小圆面积。
若两圆相交,运用几何和三角函数知识很容易就求出了。
 
代码:
 1 #include <iostream> 2 #include <string> 3 #include <map> 4 #include <stdio.h> 5 #include <math.h> 6 using namespace std; 7 #define pi acos(-1) 8  9 10 main()11 {12      double x1, y1, x2, y2, r1, r2;13      while(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&r1,&x2,&y2,&r2)==6){14          double d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));15          if(d>=(r1+r2)){16              printf("0.000\n");continue;17          }18         if(d<=(max(r1,r2)-min(r1,r2))){19             printf("%.3lf\n",min(r1,r2)*min(r1,r2)*pi);continue;20         } 21          double a1=acos((r1*r1+d*d-r2*r2)/(2*r1*d));22          double a2=acos((r2*r2+d*d-r1*r1)/(2*r2*d));23          double s1=a1*r1*r1;24          double s2=a2*r2*r2;25          double s=r1*d*sin(a1);26          printf("%.3lf\n",s1+s2-s);27      }28 }