#include <bits/stdc++.h> using namespace std; // Half-plane intersection was taken from // https://github.com/bicsi/kactl/tree/master/content/geometry using K = long double; #define double __float128 using Point = complex<double>; struct Angle { int x, y; int id; Angle() {} Angle(int x, int y) : x(x), y(y) {} Angle(int x, int y, int id): x(x), y(y), id(id) {} Angle operator-(Angle a) const { return {x-a.x, y-a.y, id}; } Angle reflect(Angle a) { return Angle(2 * x - a.x, 2 * y - a.y, -1); } int upper() const { return y > 0 || (y == 0 && x > 0); } Point p() const { return Point(x, y); } }; bool operator<(Angle a, Angle b) { return make_tuple(!a.upper(), 1LL * a.y * b.x) < make_tuple(!b.upper(), 1LL * a.x * b.y); } bool operator==(Angle a, Angle b) { return !(a < b || b < a); } bool operator!=(Angle a, Angle b) { return !(a == b); } Angle operator+(Angle a, Angle b) { // where b is a vector Angle r(a.x + b.x, a.y + b.y, -1); return r; } const double kPi = 4.0 * atan(1.0); const double kEps = 1e-11; const double kInf = 1e9; #define X() real() #define Y() imag() double dot(Point a, Point b) { return (conj(a) * b).X(); } double cross(Point a, Point b) { return (conj(a) * b).Y(); } Point perp(Point a) { return Point{-a.Y(), a.X()}; } // +90deg double det(Point a, Point b, Point c) { return cross(b - a, c - a); } int sgn(double x) { if (x < -kEps) return -1; if (x > kEps) return +1; return 0; } Point LineIntersection(Point a, Point b, Point p, Point q) { double c1 = det(a, b, p), c2 = det(a, b, q); assert(sgn(c1 - c2)); // undefined if parallel return (q * c1 - p * c2) / (c1 - c2); } struct HalfplaneSet : multimap<Angle, Point> { using Iter = multimap<Angle, Point>::iterator; HalfplaneSet() { insert({{+1, 0}, {-kInf, -kInf}}); insert({{0, +1}, {+kInf, -kInf}}); insert({{-1, 0}, {+kInf, +kInf}}); insert({{0, -1}, {-kInf, +kInf}}); } Iter get_next(Iter it) { return (next(it) == end() ? begin() : next(it)); } Iter get_prev(Iter it) { return (it == begin() ? prev(end()) : prev(it)); } Iter fix(Iter it) { return it == end() ? begin() : it; } // Cuts everything to the RIGHT of a, b // For LEFT, just swap a with b void Cut(Angle a, Angle b) { //auto c = a; a = b; b = c; if (empty()) return; int old_size = size(); auto eval = [&](Iter it) { return sgn(det(a.p(), b.p(), it->second)); }; auto intersect = [&](Iter it) { return LineIntersection(a.p(), b.p(), it->second, it->first.p() + it->second); }; auto it = fix(lower_bound(b - a)); if (eval(it) >= 0) return; while (size() && eval(get_prev(it)) < 0) fix(erase(get_prev(it))); while (size() && eval(get_next(it)) < 0) it = fix(erase(it)); if (empty()) return; if (eval(get_next(it)) > 0) it->second = intersect(it); else it = fix(erase(it)); if (old_size <= 2) return; it = get_prev(it); insert(it, {b - a, intersect(it)}); if (eval(it) == 0) erase(it); } double Area() { if (size() <= 2) return 0; double ret = 0; for (auto it = begin(); it != end(); ++it) ret += cross(it->second, get_next(it)->second); return ret / 2.0; } }; double solve() { int n; cin >> n; vector<Angle> a; for (int i = 0; i < n; ++i) { Angle x, y; cin >> x.x >> x.y; cin >> y.x >> y.y; x.id = y.id = i; a.push_back(x); a.push_back(y); } HalfplaneSet s; for (int i = 0; i < 2 * n; ++i) { vector<Angle> pts; Angle orig = {0, 0}; for (int j = 0; j < 2 * n; ++j) { if (i == j) { continue; } auto cur = a[j] - a[i]; pts.push_back(cur); pts.push_back(orig.reflect(cur)); } pts.push_back({1, 0, -1}); pts.push_back({-1, 0, -1}); sort(pts.begin(), pts.end()); vector<int> cnt(n); int t = 0; auto add = [&cnt, &t](int id, int mul) { if (id == -1) { return; } cnt[id] += mul; if (cnt[id] == 1 && mul == -1) --t; if (cnt[id] == 2) ++t; }; int m = (int) pts.size(); int it = 0; while (pts[it].upper()) { add(pts[it].id, +1); ++it; } for (int j = 0; j < m; ++j) { int cur = j; while (cur < m && pts[cur] == pts[j]) { add(pts[cur].id, -1); ++cur; } auto ref = orig.reflect(pts[j]); while (pts[it] != ref) { add(pts[it].id, +1); it = (it + 1) % m; } while (pts[it] == ref) { add(pts[it].id, +1); it = (it + 1) % m; } if (!t) { s.Cut(pts[j] + a[i], orig + a[i]); s.Cut(pts[cur % m] + a[i], orig + a[i]); } j = cur - 1; } } return s.Area(); } void test() { HalfplaneSet s; s.Cut({1, 0}, {0, 0}); s.Cut({0, 1}, {1, 0}); s.Cut({0, 0}, {0, 1}); cout << (K)s.Area() << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(17) << fixed << (K)solve() << '\n'; }
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | #include <bits/stdc++.h> using namespace std; // Half-plane intersection was taken from // https://github.com/bicsi/kactl/tree/master/content/geometry using K = long double; #define double __float128 using Point = complex<double>; struct Angle { int x, y; int id; Angle() {} Angle(int x, int y) : x(x), y(y) {} Angle(int x, int y, int id): x(x), y(y), id(id) {} Angle operator-(Angle a) const { return {x-a.x, y-a.y, id}; } Angle reflect(Angle a) { return Angle(2 * x - a.x, 2 * y - a.y, -1); } int upper() const { return y > 0 || (y == 0 && x > 0); } Point p() const { return Point(x, y); } }; bool operator<(Angle a, Angle b) { return make_tuple(!a.upper(), 1LL * a.y * b.x) < make_tuple(!b.upper(), 1LL * a.x * b.y); } bool operator==(Angle a, Angle b) { return !(a < b || b < a); } bool operator!=(Angle a, Angle b) { return !(a == b); } Angle operator+(Angle a, Angle b) { // where b is a vector Angle r(a.x + b.x, a.y + b.y, -1); return r; } const double kPi = 4.0 * atan(1.0); const double kEps = 1e-11; const double kInf = 1e9; #define X() real() #define Y() imag() double dot(Point a, Point b) { return (conj(a) * b).X(); } double cross(Point a, Point b) { return (conj(a) * b).Y(); } Point perp(Point a) { return Point{-a.Y(), a.X()}; } // +90deg double det(Point a, Point b, Point c) { return cross(b - a, c - a); } int sgn(double x) { if (x < -kEps) return -1; if (x > kEps) return +1; return 0; } Point LineIntersection(Point a, Point b, Point p, Point q) { double c1 = det(a, b, p), c2 = det(a, b, q); assert(sgn(c1 - c2)); // undefined if parallel return (q * c1 - p * c2) / (c1 - c2); } struct HalfplaneSet : multimap<Angle, Point> { using Iter = multimap<Angle, Point>::iterator; HalfplaneSet() { insert({{+1, 0}, {-kInf, -kInf}}); insert({{0, +1}, {+kInf, -kInf}}); insert({{-1, 0}, {+kInf, +kInf}}); insert({{0, -1}, {-kInf, +kInf}}); } Iter get_next(Iter it) { return (next(it) == end() ? begin() : next(it)); } Iter get_prev(Iter it) { return (it == begin() ? prev(end()) : prev(it)); } Iter fix(Iter it) { return it == end() ? begin() : it; } // Cuts everything to the RIGHT of a, b // For LEFT, just swap a with b void Cut(Angle a, Angle b) { //auto c = a; a = b; b = c; if (empty()) return; int old_size = size(); auto eval = [&](Iter it) { return sgn(det(a.p(), b.p(), it->second)); }; auto intersect = [&](Iter it) { return LineIntersection(a.p(), b.p(), it->second, it->first.p() + it->second); }; auto it = fix(lower_bound(b - a)); if (eval(it) >= 0) return; while (size() && eval(get_prev(it)) < 0) fix(erase(get_prev(it))); while (size() && eval(get_next(it)) < 0) it = fix(erase(it)); if (empty()) return; if (eval(get_next(it)) > 0) it->second = intersect(it); else it = fix(erase(it)); if (old_size <= 2) return; it = get_prev(it); insert(it, {b - a, intersect(it)}); if (eval(it) == 0) erase(it); } double Area() { if (size() <= 2) return 0; double ret = 0; for (auto it = begin(); it != end(); ++it) ret += cross(it->second, get_next(it)->second); return ret / 2.0; } }; double solve() { int n; cin >> n; vector<Angle> a; for (int i = 0; i < n; ++i) { Angle x, y; cin >> x.x >> x.y; cin >> y.x >> y.y; x.id = y.id = i; a.push_back(x); a.push_back(y); } HalfplaneSet s; for (int i = 0; i < 2 * n; ++i) { vector<Angle> pts; Angle orig = {0, 0}; for (int j = 0; j < 2 * n; ++j) { if (i == j) { continue; } auto cur = a[j] - a[i]; pts.push_back(cur); pts.push_back(orig.reflect(cur)); } pts.push_back({1, 0, -1}); pts.push_back({-1, 0, -1}); sort(pts.begin(), pts.end()); vector<int> cnt(n); int t = 0; auto add = [&cnt, &t](int id, int mul) { if (id == -1) { return; } cnt[id] += mul; if (cnt[id] == 1 && mul == -1) --t; if (cnt[id] == 2) ++t; }; int m = (int) pts.size(); int it = 0; while (pts[it].upper()) { add(pts[it].id, +1); ++it; } for (int j = 0; j < m; ++j) { int cur = j; while (cur < m && pts[cur] == pts[j]) { add(pts[cur].id, -1); ++cur; } auto ref = orig.reflect(pts[j]); while (pts[it] != ref) { add(pts[it].id, +1); it = (it + 1) % m; } while (pts[it] == ref) { add(pts[it].id, +1); it = (it + 1) % m; } if (!t) { s.Cut(pts[j] + a[i], orig + a[i]); s.Cut(pts[cur % m] + a[i], orig + a[i]); } j = cur - 1; } } return s.Area(); } void test() { HalfplaneSet s; s.Cut({1, 0}, {0, 0}); s.Cut({0, 1}, {1, 0}); s.Cut({0, 0}, {0, 1}); cout << (K)s.Area() << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(17) << fixed << (K)solve() << '\n'; } |