Educational Codeforces Round 2 D.Area of Two Circles' Intersection(计算几何)

Educational Codeforces Round 2D: http://codeforces.com/contest/600/problem/D
D. Area of Two Circles' Intersection
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two circles. Find the area of their intersection.

Input

The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle.

The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.

Output

Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed10 - 6.

Sample test(s)
input
0 0 4
6 0 4
output
7.25298806364175601379
input
0 0 5
11 0 5
output
0.00000000000000000000

大致题意:求两个元的相交面积


原来做过hdu5120这道题,直接套用这题给的模版,结果一直wa

这组数据(0 1000000000 1 0 0 1000000000)错了,原来是模版有错误的地方

重新找了另一个模版


#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;


typedef long double ld;
typedef long long ll;

ld x[2],y[2],r[2];
const ld pi = acosl(-1);

inline ld sqr(ld x) {
    return x*x;
}

inline ll sqrl(ll x) {
    return x*x;
}

inline ld fcos(ld a, ld b, ld c) {
    return acosl((a*a+b*b-c*c)/(2*a*b));
}

inline ld cut(ld ang, ld r) {
    ld s1 = ang*r*r;
    ld s2 = sinl(ang)*cosl(ang)*r*r;
    return s1 - s2;
}

double solve() {
    if(r[0] > r[1]) {
        swap(r[0],r[1]);
        swap(x[0],x[1]);
        swap(y[0],y[1]);
    }
    ll dx = x[0]-x[1], dy = y[0]-y[1];
    ll ds = sqrl(dx)+sqrl(dy);
    if(ds >= sqrl(r[0]+r[1]))
        return 0;
    ld d = sqrtl(ds);
    if(d+r[0] <= r[1])
        return sqr(r[0])*pi;

    ld ang[2];
    ang[0] = fcos(d,r[0],r[1]);
    ang[1] = fcos(d,r[1],r[0]);

    return cut(ang[0],r[0]) + cut(ang[1],r[1]);
}

int main() {
    while(cin>>x[0]>>y[0]>>r[0]>>x[1]>>y[1]>>r[1]) {
        printf("%.6lf\n",solve());
    }
    return 0;
}




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