Circular Area

题目链接

[POJ 2546] Circular Area

题解

中档题。题意大概是给定两个圆,求出相交的面积。
模板题。

代码

#include <cmath>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
const double pi = acos(-1.0);

struct Point {
    double x, y;
};

struct Circle {
    double r;
    Point center;
}a, b;

inline double squ(double a) {
    return a * a;
}

inline double dis(Point a, Point b) {
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}

int main() {
    while (cin >> a.center.x >> a.center.y >> a.r >> b.center.x >> b.center.y >> b.r) {
        double length = dis(a.center, b.center);
        if (a.r + b.r < length) {
            printf("%.3lf\n", 0.0);
            continue;
        }
        if (fabs(a.r - b.r) >= length) {
            printf("%.3lf\n", pi * squ(min(a.r, b.r)));
            continue;
        }
        double angleA = acos((squ(a.r) + squ(length) - squ(b.r)) / 2 / a.r / length);
        double angleB = acos((squ(b.r) + squ(length) - squ(a.r)) / 2 / b.r / length);
        double area1 = squ(a.r) * angleA;
        double area2 = squ(b.r) * angleB;
        printf("%.3lf\n", area1 + area2 - a.r * length * sin(angleA));
    }
    return 0;
}

标签: math, geometry

添加新评论