hdu1798几何公式

主要就是推公式了,我推了老半天,囧啊。。。还要注意一点就是内切的情况,开始没考虑,WA了一次。

/*

 * hdu1798/win.cpp

 * Created on: 2012-7-9

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;



const double pi = acos(-1);



inline double mydistance(double x1, double y1, double x2, double y2) {

    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

}



int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    double x1, y1, r1, x2, y2, r2;

    while(scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &r1, &x2, &y2, &r2) == 6) {

        double d = mydistance(x1, y1, x2, y2);

        double ans;

        if(d >= r1 + r2) {//相离或外切

            ans = 0;

        }else  if(d <= fabs(r1 - r2)) {

                if(r1 <= r2) {

                    ans = pi * r1 * r1;

                }else {

                    ans = pi * r2 * r2;

                }

        }else {

            double x = (d * d + r2 * r2 - r1 * r1) / d / 2;

            double p1 = r1 * r1 * acos((d - x) / r1);

            double p2 = r2 * r2 * acos(x / r2);

            double p3 = d * sqrt(r2 * r2 - x * x);

            ans = p1 + p2 - p3;

        }

        printf("%.3f\n", ans);

    }

    return 0;

}

你可能感兴趣的:(HDU)