forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
82 lines (70 loc) · 2.18 KB
/
D.cpp
File metadata and controls
82 lines (70 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define ll long long
#define SZ(x) ((int) (x).size())
#define sqr(x) ((x) * (x))
#define double long double
using namespace std;
const double PI = acos((double) -1.0);
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
Point operator - (const Point& a) const {
return Point(x-a.x, y-a.y);
}
double len() {
return hypot(x, y);
}
};
struct Circle : Point {
double r;
};
double solve(double a, double b, double c) {
return acos((a*a + b*b - c*c) / 2 / a / b);
}
double cut(double a, double r) {
double s1 = a * r * r / 2;
double s2 = sin(a) * r * r / 2;
return s1 - s2;
}
double commonCircleArea(Circle c1, Circle c2) { //return the common area of two circle
if (c1.r < c2.r) {
swap(c1, c2);
}
double d = (c1 - c2).len();
double a1 = solve(d, c1.r, c2.r);
double a2 = solve(d, c2.r, c1.r);
return cut(a1*2, c1.r) + cut(a2*2, c2.r);
}
int main() {
cout << (fixed) << setprecision(12);
ll x1, y1, r1, x2, y2, r2;
while (cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2) {
// do not intersect
ll dist2 = sqr(x1 - x2) + sqr(y1 - y2);
if (dist2 >= sqr(r1 + r2)) {
cout << 0.0 << endl;
continue;
}
// C1 in C2
double dist = sqrt(dist2);
if (dist + r1 <= r2) {
cout << PI * r1 * r1 << endl;
continue;
}
// C2 in C1
if (dist + r2 <= r1) {
cout << PI * r2 * r2 << endl;
continue;
}
Circle c1; c1.x = x1; c1.y = y1; c1.r = r1;
Circle c2; c2.x = x2; c2.y = y2; c2.r = r2;
cout << commonCircleArea(c1, c2) << endl;
}
}