Tell me the area HDU 1798

来源:互联网 发布:逍遥模拟器没网络 编辑:程序博客网 时间:2024/05/19 23:13

Tell me the area
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2958 Accepted Submission(s): 946

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

[分析]
数学题。
首先判断相离相交还是包含(是叫包含吗?有点忘记了)。
相离就0.00,包含就输出小圆的面积。
相交先算出角A和角B的角度,算出2个扇形面积,减去两个三角形面积。
最后有注释。

[代码]

#include<cstdio>#include<cmath>#define PI acos(-1)int main(){    double x1, y1, r1;    double x2, y2, r2;    while (scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &r1, &x2, &y2, &r2) != EOF)    {        double dis = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));        if (dis >= r1 + r2)printf("0.000\n");//相离        else if (dis <= fabs(r1 - r2))//包含        {            if (r1 > r2)printf("%.3lf\n", PI*r2*r2);            else printf("%.3lf\n", PI*r1*r1);        }        else//相交        {            double a1 = 2 * acos((dis*dis + r1*r1 - r2*r2) / (2 * dis*r1));//注释1            double a2 = 2 * acos((dis*dis + r2*r2 - r1*r1) / (2 * dis*r2));            double sanjiao = 0.5*r1*r1*sin(a1) + 0.5*r2*r2*sin(a2);//注释2            double ans = 0.5*r1*r1*a1 + 0.5*r2*r2*a2 - sanjiao;//注释3            printf("%.3lf\n", ans);        }    }}

这里写图片描述
[注释1]首先使用余弦定理算出角A
这里写图片描述
不过因为cd不知道长度所以只好改算角cab。最后乘2就是角A的大小了。

[注释2]三角形面积公式1/2*a*b*sin(c);

[注释3]扇形面积公式1/2*r*r*a;