HDOJ1798 求两圆公共面积

Tell me the area

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


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.
HDOJ1798 求两圆公共面积_第1张图片
 

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)
 

Recommend
wangye   |   We have carefully selected several similar problems for you:   1797  1793  1796  1795  1794 

首先判断圆心距,如果分内含,外离,相交这三种情况。
相交的话用余弦定理求出圆心角,计算出扇形面积,并且根据几何关系求解扇形对应的三角形面积。然后用两个扇形的面积相加减去两个三角形的面积就得出结果了。

#include 
#include 
#include 
#include 
using namespace std;

const double eps = 1e-8;
double X1,Y1,r1,X2,Y2,r2,d;
double a1,a2,s1,s2,t1,t2,PI;

double Dist(double x1, double y1 , double x2, double y2) {
    return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}

int main(){
    while (cin >> X1 >> Y1 >> r1 >> X2 >> Y2 >> r2) {
          d = Dist(X1,Y1,X2,Y2);
          PI = 2*asin(1.0);
          if (d>=(r1+r2) || !r1 || !r2) {printf("0.000\n"); continue;}
          if (d+r1<=r2) {printf("%.3lf\n",PI*r1*r1); continue;}
          if (d+r2<=r1) {printf("%.3lf\n",PI*r2*r2); continue;}

          a1 = acos((d*d + r1*r1 - r2*r2)/(2*d*r1)); //余弦定理求圆心角
          a2 = acos((d*d + r2*r2 - r1*r1)/(2*d*r2));
          s1 = a1*r1*r1;  //扇形面积
          s2 = a2*r2*r2;
          t1 = r1*r1*cos(a1)*sin(a1); //扇形对应的三角形面积
          t2 = r2*r2*cos(a2)*sin(a2);
          printf("%.3lf\n",s1+s2-t1-t2);
    }
    return 0;
}


你可能感兴趣的:(计算几何)