圆的面积交,板题。
#include <bits/stdc++.h>
#define aint(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
const int maxn = 111;
const long double pi = acos(-1.0);
struct Point {
long double x, y;
Point(long double x = 0, long double y = 0):x(x),y(y) {};
};
const long double eps = 1e-10;
int dcmp(long double x) {
if(fabs(x) < eps) return 0; else return x < 0? -1 : 1;
}
long double Dis(Point a, Point b) {
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
long double AreaOfOverlap(Point c1, long double r1, Point c2, long double r2) {
long double a = Dis(c1, c2), b = r1, c = r2;
long double s1 = pi*r1*r1, s2 = pi*r2*r2;
if(dcmp(a - b - c) >= 0) return 0;
if(dcmp(a + min(b, c) - max(b, c)) <= 0) return min(s1, s2);
else {
long double cta1 = 2*acos((a*a+b*b-c*c)/(2*a*b));
long double cta2 = 2*acos((a*a+c*c-b*b)/(2*a*c));
return cta1/(2*pi)*s1-0.5*sin(cta1)*b*b + cta2/(2*pi)*s2-0.5*sin(cta2)*c*c;
}
}
int main() {
Point p1, p2;
long double r1, r2;
while(cin >> p1.x >> p1.y >> r1) {
cin >> p2.x >> p2.y >> r2;
cout<<fixed<<setprecision(10)<<AreaOfOverlap(p1, r1, p2, r2)<<endl;
}
return 0;
}