#include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin #define FOR(i,a,n) for (auto i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,(n)-1) #define FORD(i,a,n) for (auto i = (a), i##__ = (n); i >= i##__; --i) #define REPD(i,n) FORD(i,(n)-1,0) #define ALL(x) x.begin(), x.end() #define SZ(x) ((int) x.size()) #define X first #define Y second #define V vector #define A array constexpr char nl = '\n'; template<class A, class B> A&& mini(A &&a, B &&b) { if (b < a) a = b; return a; } template<class A, class B> A&& maxi(A &&a, B &&b) { if (b > a) a = b; return a; } int first_bit(int x) { return x == 0 ? 0 : sizeof(x) * 8 - __builtin_clz(x); } int ceil2(int x) { return x < 2 ? x : 1 << first_bit(x - 1); } constexpr int inf = 0x3f3f3f3f; constexpr long long inf_l = 0x3f3f3f3f3f3f3f3f; #define _eif(a...) typename enable_if<a, int>::type() #define _func(func, a...) template<class T> auto func(T &&x) -> decltype(a) #define _rref(a...) typename remove_reference<a>::type #define _create_trait(name, a...) \ _func(_##name, a, true_type{}); \ false_type _##name(...); \ template<class T> struct name : decltype(_##name(declval<T>())) {}; struct Debug; _create_trait(is_debug_func, x(declval<add_lvalue_reference<Debug>::type>())); _create_trait(is_func, x()) _create_trait(is_string, string(x)) _create_trait(is_ptr, *x, _eif(is_string<T>() == false)); _create_trait(is_container, x.begin(), _eif(is_string<T>() == false)) template<class Iter> struct Off { Iter _a, _b; }; _func(O, _eif(is_container<T>() == true), Off<decltype(x.begin())>()) { return { ALL(x) }; } _func(O, _eif(is_container<T>() == false), x) { return x; } #define _operator(a...) _func(operator<<, a, *this) struct Debug { Debug() { cerr << boolalpha << setprecision(5); } ~Debug() { cerr << nl; } Debug& operator()(int x = 1) { REP(_, x) *this << " "; return *this; } _operator(cerr << x, _eif(is_func<T>() == false && is_ptr<T>() == false && is_integral<_rref(T)>() == true)) { using L = long long; if(abs(int(x)) == inf || abs(L(x)) == inf_l) cerr << ((int(x) == inf || L(x) == inf_l) ? "+∞" : (int(x) == -inf || L(x) == -inf_l) ? "-∞" : "?"); else cerr << x; return *this; } _operator(cerr << x, _eif(is_func<T>() == false && is_ptr<T>() == false && is_integral<_rref(T)>() == false)) { cerr << x; return *this; } _operator(x.first) { return *this << "(" << O(x.first) << ", " << O(x.second) << ")"; } _operator(_eif(is_container<T>() == true)) { *this << "{\n"; for (auto a = x.begin(); a != x.end(); ++a) *this << " " << distance(x.begin(), a) << ": " << O(*a) << '\n'; return *this << "}"; } _operator(x._a) { *this << "{"; for (auto a = x._a, b = x._b; a != b; ++a) *this << O(*a) << (next(a) == b ? "" : ", "); return *this << "}"; } _operator(_eif(is_func<T>() == true)) { x(); return *this; } _operator(_eif(is_debug_func<T>() == true)) { x(*this); return *this; } _operator(_eif(is_ptr<T>() == true && is_func<T>() == false && is_debug_func<T>() == false)) { return *this << *x; } }; struct DebugOff { template<class T> DebugOff& operator<<(T&&) { return *this; } DebugOff& operator()(int = 0) { return *this; } }; #ifdef DEBUG # define D Debug() #else # define D DebugOff() #endif #define I(a...) #a ": " << a << " " using VI = V<int>; using VVI = V<VI>; using L = long long; using VL = V<L>; using VB = V<bool>; using II = pair<int, int>; using VII = V<II>; using VVII = V<VII>; // end of templates v8 by Tomasz Nowak using Double = long double; Double eps = 1e-9; bool equal(Double a, Double b) { return abs(a - b) < eps; } struct Point { Double x, y; void operator()(Debug &d) { d << make_pair(x, y); } Point(Double a = 0, Double b = 0) : x(a), y(b) {} }; bool operator<(Point a, Point b) { return equal(a.x, b.x) ? a.y < b.y : a.x < b.x; } Double operator*(Point a, Point b) { return a.x * b.y - a.y * b.x; } Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } int dir(Point a, Point b, Point c) { Double d = (c - b) * (b - a); return equal(d, 0) ? 0 : d > 0 ? 1 : -1; } bool operator!=(Point a, Point b) { return equal(a.x, b.x) == false || equal(a.y, b.y) == false; } bool operator==(Point a, Point b) { return !(a != b); } vector<Point> gorna_otoczka(vector<Point> &sorted) { vector<Point> ans; for(Point &p : sorted) { while(SZ(ans) >= 2 && dir(ans.end()[-2], ans.back(), p) >= 0) ans.pop_back(); ans.emplace_back(p); } return ans; } vector<Point> stworz_otoczke(vector<Point> &all) { if(SZ(all) < 3) return {}; sort(all.begin(), all.end()); vector<Point> ans = gorna_otoczka(all); ans.pop_back(); reverse(all.begin(), all.end()); vector<Point> dolna = gorna_otoczka(all); dolna.pop_back(); for(Point &p : dolna) ans.emplace_back(p); return ans; } Point null_point(inf, inf); bool is_null(Point p) { return equal(p.x, inf) || equal(p.y, inf); } Point intersection_line(Point a1, Point a2, Point b1, Point b2) { Double A1 = a2.y - a1.y; Double B1 = a1.x - a2.x; Double C1 = A1 * a1.x + B1 * a1.y; Double A2 = b2.y - b1.y; Double B2 = b1.x - b2.x; Double C2 = A2 * b1.x + B2 * b1.y; Double det = A1 * B2 - A2 * B1; if(equal(det, 0)) return null_point; Double x = (B2 * C1 - B1 * C2) / det; Double y = (A1 * C2 - A2 * C1) / det; return Point(x, y); } bool intersects(Point a, Point b, Point w, Point u) { // prosta, odcinek return dir(a, u, b) * dir(a, w, b) == -1; } vector<Point> cut(vector<Point> otoczka, Point w1, Point w2) { vector<Point> on_right; for(Point &p : otoczka) if(dir(w1, w2, p) <= 0) on_right.emplace_back(p); REP(i, SZ(otoczka)) { int j = i == SZ(otoczka) - 1 ? 0 : i + 1; Point &a = otoczka[i], &b = otoczka[j]; if(intersects(w1, w2, a, b) == false) continue; Point inter = intersection_line(w1, w2, a, b); if(is_null(inter) == false) on_right.emplace_back(inter); } return stworz_otoczke(on_right); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(14); cout << fixed; int n; cin >> n; vector<pair<Point, Point>> in(n); for(auto &p : in) cin >> p.first.x >> p.first.y >> p.second.x >> p.second.y; D << I(n) << I(in); vector<pair<Point, Point>> halfplanes; REP(i, n) FOR(j, i + 1, n - 1) REP(d1, 2) REP(d2, 2) { Point &a = d1 ? in[i].second : in[i].first; Point &b = d2 ? in[j].second : in[j].first; VI on_right(n), on_left(n); REP(k, n) REP(d, 2) { int cross = dir(a, b, d ? in[k].second : in[k].first); if(cross == 0) continue; else if(cross < 0) ++on_right[k]; else ++on_left[k]; } int two_right = 0, two_left = 0; REP(k, n) { if(on_right[k] == 2) ++two_right; if(on_left[k] == 2) ++two_left; } if(two_right == 0 && two_left == 0) return cout << 0, 0; else if(two_right == 0) halfplanes.emplace_back(b, a); else if(two_left == 0) halfplanes.emplace_back(a, b); } // D << I(halfplanes); cerr << fixed; Double inf_d = 500; vector<Point> plane = { Point(+inf_d, +inf_d), Point(+inf_d, -inf_d), Point(-inf_d, -inf_d), Point(-inf_d, +inf_d) }; for(auto &halfplane : halfplanes) { plane = cut(plane, halfplane.first, halfplane.second); if(SZ(plane) < 3) return cout << 0.0f, 0; } D << I(plane); Double area = 0; REP(i, SZ(plane)) { int j = i == SZ(plane) - 1 ? 0 : i + 1; area += (plane[j] - plane[i]) * plane[i]; } cout << -area / 2 << nl; }
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | #include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin #define FOR(i,a,n) for (auto i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,(n)-1) #define FORD(i,a,n) for (auto i = (a), i##__ = (n); i >= i##__; --i) #define REPD(i,n) FORD(i,(n)-1,0) #define ALL(x) x.begin(), x.end() #define SZ(x) ((int) x.size()) #define X first #define Y second #define V vector #define A array constexpr char nl = '\n'; template<class A, class B> A&& mini(A &&a, B &&b) { if (b < a) a = b; return a; } template<class A, class B> A&& maxi(A &&a, B &&b) { if (b > a) a = b; return a; } int first_bit(int x) { return x == 0 ? 0 : sizeof(x) * 8 - __builtin_clz(x); } int ceil2(int x) { return x < 2 ? x : 1 << first_bit(x - 1); } constexpr int inf = 0x3f3f3f3f; constexpr long long inf_l = 0x3f3f3f3f3f3f3f3f; #define _eif(a...) typename enable_if<a, int>::type() #define _func(func, a...) template<class T> auto func(T &&x) -> decltype(a) #define _rref(a...) typename remove_reference<a>::type #define _create_trait(name, a...) \ _func(_##name, a, true_type{}); \ false_type _##name(...); \ template<class T> struct name : decltype(_##name(declval<T>())) {}; struct Debug; _create_trait(is_debug_func, x(declval<add_lvalue_reference<Debug>::type>())); _create_trait(is_func, x()) _create_trait(is_string, string(x)) _create_trait(is_ptr, *x, _eif(is_string<T>() == false)); _create_trait(is_container, x.begin(), _eif(is_string<T>() == false)) template<class Iter> struct Off { Iter _a, _b; }; _func(O, _eif(is_container<T>() == true), Off<decltype(x.begin())>()) { return { ALL(x) }; } _func(O, _eif(is_container<T>() == false), x) { return x; } #define _operator(a...) _func(operator<<, a, *this) struct Debug { Debug() { cerr << boolalpha << setprecision(5); } ~Debug() { cerr << nl; } Debug& operator()(int x = 1) { REP(_, x) *this << " "; return *this; } _operator(cerr << x, _eif(is_func<T>() == false && is_ptr<T>() == false && is_integral<_rref(T)>() == true)) { using L = long long; if(abs(int(x)) == inf || abs(L(x)) == inf_l) cerr << ((int(x) == inf || L(x) == inf_l) ? "+∞" : (int(x) == -inf || L(x) == -inf_l) ? "-∞" : "?"); else cerr << x; return *this; } _operator(cerr << x, _eif(is_func<T>() == false && is_ptr<T>() == false && is_integral<_rref(T)>() == false)) { cerr << x; return *this; } _operator(x.first) { return *this << "(" << O(x.first) << ", " << O(x.second) << ")"; } _operator(_eif(is_container<T>() == true)) { *this << "{\n"; for (auto a = x.begin(); a != x.end(); ++a) *this << " " << distance(x.begin(), a) << ": " << O(*a) << '\n'; return *this << "}"; } _operator(x._a) { *this << "{"; for (auto a = x._a, b = x._b; a != b; ++a) *this << O(*a) << (next(a) == b ? "" : ", "); return *this << "}"; } _operator(_eif(is_func<T>() == true)) { x(); return *this; } _operator(_eif(is_debug_func<T>() == true)) { x(*this); return *this; } _operator(_eif(is_ptr<T>() == true && is_func<T>() == false && is_debug_func<T>() == false)) { return *this << *x; } }; struct DebugOff { template<class T> DebugOff& operator<<(T&&) { return *this; } DebugOff& operator()(int = 0) { return *this; } }; #ifdef DEBUG # define D Debug() #else # define D DebugOff() #endif #define I(a...) #a ": " << a << " " using VI = V<int>; using VVI = V<VI>; using L = long long; using VL = V<L>; using VB = V<bool>; using II = pair<int, int>; using VII = V<II>; using VVII = V<VII>; // end of templates v8 by Tomasz Nowak using Double = long double; Double eps = 1e-9; bool equal(Double a, Double b) { return abs(a - b) < eps; } struct Point { Double x, y; void operator()(Debug &d) { d << make_pair(x, y); } Point(Double a = 0, Double b = 0) : x(a), y(b) {} }; bool operator<(Point a, Point b) { return equal(a.x, b.x) ? a.y < b.y : a.x < b.x; } Double operator*(Point a, Point b) { return a.x * b.y - a.y * b.x; } Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } int dir(Point a, Point b, Point c) { Double d = (c - b) * (b - a); return equal(d, 0) ? 0 : d > 0 ? 1 : -1; } bool operator!=(Point a, Point b) { return equal(a.x, b.x) == false || equal(a.y, b.y) == false; } bool operator==(Point a, Point b) { return !(a != b); } vector<Point> gorna_otoczka(vector<Point> &sorted) { vector<Point> ans; for(Point &p : sorted) { while(SZ(ans) >= 2 && dir(ans.end()[-2], ans.back(), p) >= 0) ans.pop_back(); ans.emplace_back(p); } return ans; } vector<Point> stworz_otoczke(vector<Point> &all) { if(SZ(all) < 3) return {}; sort(all.begin(), all.end()); vector<Point> ans = gorna_otoczka(all); ans.pop_back(); reverse(all.begin(), all.end()); vector<Point> dolna = gorna_otoczka(all); dolna.pop_back(); for(Point &p : dolna) ans.emplace_back(p); return ans; } Point null_point(inf, inf); bool is_null(Point p) { return equal(p.x, inf) || equal(p.y, inf); } Point intersection_line(Point a1, Point a2, Point b1, Point b2) { Double A1 = a2.y - a1.y; Double B1 = a1.x - a2.x; Double C1 = A1 * a1.x + B1 * a1.y; Double A2 = b2.y - b1.y; Double B2 = b1.x - b2.x; Double C2 = A2 * b1.x + B2 * b1.y; Double det = A1 * B2 - A2 * B1; if(equal(det, 0)) return null_point; Double x = (B2 * C1 - B1 * C2) / det; Double y = (A1 * C2 - A2 * C1) / det; return Point(x, y); } bool intersects(Point a, Point b, Point w, Point u) { // prosta, odcinek return dir(a, u, b) * dir(a, w, b) == -1; } vector<Point> cut(vector<Point> otoczka, Point w1, Point w2) { vector<Point> on_right; for(Point &p : otoczka) if(dir(w1, w2, p) <= 0) on_right.emplace_back(p); REP(i, SZ(otoczka)) { int j = i == SZ(otoczka) - 1 ? 0 : i + 1; Point &a = otoczka[i], &b = otoczka[j]; if(intersects(w1, w2, a, b) == false) continue; Point inter = intersection_line(w1, w2, a, b); if(is_null(inter) == false) on_right.emplace_back(inter); } return stworz_otoczke(on_right); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(14); cout << fixed; int n; cin >> n; vector<pair<Point, Point>> in(n); for(auto &p : in) cin >> p.first.x >> p.first.y >> p.second.x >> p.second.y; D << I(n) << I(in); vector<pair<Point, Point>> halfplanes; REP(i, n) FOR(j, i + 1, n - 1) REP(d1, 2) REP(d2, 2) { Point &a = d1 ? in[i].second : in[i].first; Point &b = d2 ? in[j].second : in[j].first; VI on_right(n), on_left(n); REP(k, n) REP(d, 2) { int cross = dir(a, b, d ? in[k].second : in[k].first); if(cross == 0) continue; else if(cross < 0) ++on_right[k]; else ++on_left[k]; } int two_right = 0, two_left = 0; REP(k, n) { if(on_right[k] == 2) ++two_right; if(on_left[k] == 2) ++two_left; } if(two_right == 0 && two_left == 0) return cout << 0, 0; else if(two_right == 0) halfplanes.emplace_back(b, a); else if(two_left == 0) halfplanes.emplace_back(a, b); } // D << I(halfplanes); cerr << fixed; Double inf_d = 500; vector<Point> plane = { Point(+inf_d, +inf_d), Point(+inf_d, -inf_d), Point(-inf_d, -inf_d), Point(-inf_d, +inf_d) }; for(auto &halfplane : halfplanes) { plane = cut(plane, halfplane.first, halfplane.second); if(SZ(plane) < 3) return cout << 0.0f, 0; } D << I(plane); Double area = 0; REP(i, SZ(plane)) { int j = i == SZ(plane) - 1 ? 0 : i + 1; area += (plane[j] - plane[i]) * plane[i]; } cout << -area / 2 << nl; } |