poj2546

题意:给定两个圆,求相交面积。

分析:把两个弓形面积相加,可转化为分别求两个扇形面积,相加后减去两个三角形面积。两个三角形可以合并为一个四边形,而四边形可以根据两个圆心的连线分为另外两个三角形。这两个三角形的三边可以轻松求出,然后利用海伦公式。我们还要考虑圆心角是钝角的情况,但是后来发现也符合这个公式。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
using namespace std;

#define eps 10e-11
#define pi acos(double(-1))

int dblcmp(double a, double b)
{
if (a + eps < b)
return -1;
if (a - eps > b)
return 1;
return 0;
}

int main()
{
//freopen("t.txt", "r", stdin);
double x1, y1, x2, y2, r1, r2;
scanf(
"%lf%lf%lf%lf%lf%lf", &x1, &y1, &r1, &x2, &y2, &r2);
double x = abs(x1 - x2);
double y = abs(y1 - y2);
double l = sqrt(x * x + y * y);
if (r1 < r2)
swap(r1, r2);
if (dblcmp(l, r1 + r2) >= 0)
{
printf(
"0.000\n");
return 0;
}
if (dblcmp(l, abs(r1 - r2)) <= 0)
{
printf(
"%.3f\n", pi * min(r1 * r1, r2 * r2));
return 0;
}
double a = r1;
double b = r2;
double c = l;
double A = acos((c * c + b * b - a * a) / (2 * c * b));
double B = acos((c * c + a * a - b * b) / (2 * c * a));
double s = (a + b + c) / 2;
s
= sqrt(s * (s - a) * (s - b) * (s - c));
double area = a * a * B + b * b * A - 2 * s;
printf(
"%.3f\n", area);
return 0;
}
posted @ 2011-07-19 21:03  金海峰  阅读(236)  评论(0编辑  收藏  举报