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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| #include <bits/stdc++.h> using namespace std; #define double long double const double eps = 1e-16; const double PI = acos(-1.0); int sign(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; return 1; } struct Point { double x, y; Point(double a = 0, double b = 0) { x = a, y = b; } Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); } Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } Point operator*(const double &a) const { return Point(x * a, y * a); } Point operator/(const double &a) const { return Point(x / a, y / a); } bool operator==(const Point &a) const { return !sign(x - a.x) && !sign(y - a.y); } bool operator<(const Point &a) const { return (fabs(x - a.x) < eps) ? (y < a.y) : (x < a.x); } }; struct Line { Point a, v; Line(Point x = Point(), Point y = Point()) { a = x, v = y; } }; struct Circle { Point o; double r; Circle(Point x = Point(), double y = 0) { o = x, r = y; } }; double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - b.x * a.y; } double get_length(Point a) { return sqrt(dot(a, a)); } Point get_line_intersection(Line m, Line n) { Point u = m.a - n.a; if (sign(cross(m.v, n.v)) == 0) return Point(0, 0); double t = cross(n.v, u) / cross(m.v, n.v); return m.a + m.v * t; } double distance_to_line(Point p, Line m) { return fabs(cross(p - m.a, m.v) / get_length(m.v)); } pair<Point, Point> line_circle_intersection(Line l, Circle c) { Point h = get_line_intersection(l, Line(c.o, Point(-l.v.y, l.v.x))); if (sign(distance_to_line(h, l) - c.r) > 0) return {Point(), Point()}; Point e = l.v / get_length(l.v); double k = sqrt(c.r * c.r - fabs(cross(c.o - l.a, l.v)) * fabs(cross(c.o - l.a, l.v)) / dot(l.v, l.v)); return {h - e * k, h + e * k}; } Circle ccl[1003]; int n; int circle_relation(Circle p, Circle q) { double d = get_length(p.o - q.o), l = fabs(p.r - q.r); if (sign(d - p.r - q.r) > 0) return 5; else if (sign(d - p.r - q.r) == 0) return 4; else if (sign(d - l) > 0) return 3; else if (sign(d - l) == 0) return 2; else return 1; } pair<Point, Point> circle_circle_intersection(Circle a, Circle b) { double d = get_length(a.o - b.o); double d1 = a.r * (a.r * a.r + d * d - b.r * b.r) / (2 * a.r * d); double h1 = sqrt(a.r * a.r - d1 * d1); Point ed = b.o - a.o; Point h = a.o + ed / get_length(ed) * d1; return {h + Point(ed.y, -ed.x) / get_length(ed) * h1, h - Point(ed.y, -ed.x) / get_length(ed) * h1}; } double get_angle(Point a, Point b) { return acos(dot(a, b) / get_length(a) / get_length(b)); } int main() { cin >> n; for (int i = 0; i < n; i++) { scanf("%Lf%Lf%Lf", &ccl[i].o.x, &ccl[i].o.y, &ccl[i].r); } vector<pair<double, double>> lim; double ans = 0; for (int i = 0; i < n; i++) { lim.clear(); double ers = 0; bool zero = 0; for (int j = i + 1; j < n; j++) { int tmp = circle_relation(ccl[i], ccl[j]); if (tmp == 1 && ccl[i].r < ccl[j].r) { zero = 1; break; } else if (tmp == 3) { auto pii = circle_circle_intersection(ccl[i], ccl[j]); Point to = ccl[j].o - ccl[i].o; pair<double, double> deg = { atan2((pii.first - ccl[i].o).y, (pii.first - ccl[i].o).x), atan2((pii.second - ccl[i].o).y, (pii.second - ccl[i].o).x)}; if (deg.first > deg.second) { swap(deg.first, deg.second); } if (sign(fabs(deg.first - atan2(to.y, to.x)) - PI) >= 0 || sign(fabs(deg.second - atan2(to.y, to.x)) - PI) >= 0) { lim.push_back({deg.second, PI}); lim.push_back({-PI, deg.first}); } else { lim.push_back(deg); } } } if (zero) continue; if (lim.empty()) { ers = 0; } else { sort(lim.begin(), lim.end()); double st = lim[0].first, ed = lim[0].second; for (int i = 1; i < lim.size(); i++) { if (sign(lim[i].first - ed) <= 0) { ed = max(lim[i].second, ed); } else { ers += ed - st; st = lim[i].first, ed = lim[i].second; } } ers += ed - st; } ans += (2 * PI - ers) * ccl[i].r;
} printf("%.10Lf", ans); }
|