#include <bits/stdc++.h> using namespace std; // Przepraszam za brzydki kod : ( typedef long double LL; typedef long double LD; typedef pair<int,int> PII; #define MP make_pair #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SZ(x) (int)x.size() #define ALL(c) c.begin(),c.end() // Kod pożyczony z biblioteczki UW0: // https://github.com/mareksom/acmlib/blob/master/code/kamil/halfplanes.cpp // halfplanes_online #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " template<typename T> T K(T a) { return a * a; } // 'K(int)' may overflow! typedef long double ll; // can be changed to 'long double' typedef long double ld; // const ld PI = 2 * acos(0); const ld eps = 1e-12; #pragma GCC diagnostic ignored "-Wnarrowing" struct P { ll x, y; P operator + (P b) { return P{x + b.x, y + b.y}; } P operator - (P b) { return P{x - b.x, y - b.y}; } P operator * (ld/*ll*/ mul) { return P{x * mul, y * mul}; } P operator / (ld mul) { assert(mul); return P{x / mul, y / mul}; } ll operator * (P b) { return x * b.y - y * b.x; } ll dot(P b) { return x * b.x + y * b.y; } ld len() { return sqrt(K(x) + K(y)); } P scaleTo(ld to) { return *this * (to / len()); } ld dist(P & b) { return (*this - b).len(); } P rotate90() { return P{-y, x}; } ld angle() { return atan2(y, x); } P rotate(ld ang) { ld c = cos(ang), s = sin(ang); return P{x * c - y * s, x * s + y * c}; } // '<' and 'below()' needed for Convex Hull bool operator < (P he) { return make_pair(x, y) < make_pair(he.x, he.y); } bool below(P a, P b) { return (b - a) * (*this - a) <= 0/*eps*/; } //INFO 1 void write(string s) { cerr << "(" << x << ", " << y << ")" << s; } // Internal/External Similitude Center P apol_in(P b, ld ratio) { // ratio = dist()/he.dist() return (*this + b * ratio) / (1 + ratio); } P apol_out(P b, ld ratio) { return (*this - b * ratio) / (1 - ratio); } void print(debug & dd) const { dd << make_pair(x, y); } }; /* using debug() ostream & operator << (ostream & dd, P p) { dd << "(" << p.x << ", " << p.y << ") "; return dd; } */ debug & operator << (debug & dd, P p) { p.print(dd); return dd; } struct L2 { P one, two; // P p[2]; P & operator [](int i) { return p[i]; } // const P & operator [](int i) const { return p[i]; } P dir() { return two - one; } P normal() { return dir().rotate90(); } ld dist(P he) { return abs((he - one) * (he - two)) / one.dist(two); } ld segDist(P he) { // epsilon not needed, but it would work too if((he - two) * normal() < 0 && normal() * (he - one) < 0) return dist(he); return min(one.dist(he), two.dist(he)); } P inter(L2 he) { P A = dir(), B = he.dir(); ll den = A * B; assert(abs(den) > eps); // parallel, maybe equal return (A * (he.one * he.two) - B * (one * two)) * (1.0 / den); // https://en.wikipedia.org/wiki/Line-line_intersection // A = (x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4) // A'= (x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4) // B = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) // return P{A / B, A' / B}; } P project(P he) { P unit_normal = normal().scaleTo(1); return he + unit_normal * unit_normal.dot(one - he); } P reflect(P he) { return project(he) * 2 - he; } void print(debug & dd) const { dd << imie(one) << imie(two); } void write() { cerr << "L2{ "; one.write(", "); cerr << " }\n"; } // for CH: sort by slope; below() : change to L3 or compare 'x' of intersections }; L2 toL2(ll a, ll b, ll c) { P first; if(abs(b) > eps) first = P{0, (ld) -c / b}; else if(abs(a) > eps) first = P{(ld) -c / a, 0}; else assert(false); return L2{first, first + P{b, -a}}; } ll det(ll t[3][3]) { // for CH of lines Ax+By+C=0 ll s = 0; for(int i = 0; i < 3; ++i) for(int j = i + 1, mul = 1; j != i + 3; ++j, mul -= 2) s += t[0][i] * t[1][j%3] * t[2][3-i-j%3] * mul; return s; } struct L3 { // a * x + b * y + c = 0, assert(b > 0 || (b == 0 && a > 0)) ll a, b, c; L3 fix() { // <done>TODO, test it</done> assert(abs(b) > eps || abs(a) > eps); ll g = (b > eps || (abs(b) < eps && a > eps)) ? 1 : -1; // __gcd(x,0) is undef-beh, http://codeforces.com/blog/entry/13410 // if(is_integral<ll>::value) g *= abs(__gcd(c, __gcd(a?b:a, a?a:b))); // if(is_floating_point<ll>::value) g *= sqrt(K(a) + K(b)); return L3{a / g, b / g, c / g}; } ld dist(P he) { return abs(a * he.x + b * he.y + c) / sqrt(K(a) + K(b)); } P dir() { return P{b, -a}; } P normal() { return P{a, b}; } // equivalently: dir().rotate90() P project(P he) { ld den = K(a) + K(b); // non-integer because we need division return P{(b * (b * he.x - a * he.y) - a * c) / den, (a * (a * he.y - b * he.x) - b * c) / den }; } P reflect(P he) { return project(he) * 2 - he; } P inter(L3 he) { #define Q(i, j) (i * he.j - j * he.i) ll den = Q(a, b); assert(abs(den) > 1e-14); // parallel, maybe equal return P{Q(b, c), Q(c, a)} * (1.0 / den); #undef Q } bool operator < (L3 he) { // produces the order for finding an upper envelope // assert(b > 0 && he.b > 0); // a / b < he.a / he.b, ties: -c/b < ... if(abs(a * he.b - b * he.a) < eps) return b * he.c < c * he.b; // <done>test it</done> return a * he.b < b * he.a; } bool below(L3 A, L3 C) { ll t[3][3] = { {A.a,A.b,A.c}, {a,b,c}, {C.a,C.b,C.c} }; return det(t) <= 0/*eps*/; // WARN1 } }; L3 toL3(P one, P two) { ll a = two.y - one.y; ll b = one.x - two.x; return L3{a, b, -(a * one.x + b * one.y)}.fix(); } #define X real() #define Y imag() typedef complex<LL> Pu; const LD EPS = 1e-10; struct line { LL a,b,c; line(LL a_ = 0, LL b_ = 0, LL c_ = 0): a(a_), b(b_), c(c_) {} // <= 10^9 line (Pu const &A, Pu const &B): a(A.Y-B.Y), b(B.X-A.X), c(A.X*B.Y-A.Y*B.X) {} //pts <= 10^6 line operator - () const {return line(-a, -b, -c); } bool up() const { return a?(a<0):(b>0);} }; inline LL wek(line const &a, line const &b) {return a.a*b.b-a.b*b.a;} inline bool rown(line a, line b) {return abs(wek(a,b)) < EPS;} inline bool pokr(line a, line b) {return rown(a,b) && a.a*b.c == b.a*a.c && a.b*b.c == b.b*a.c;} inline bool podobne(line a, line b) {return rown(a,b) && a.up() == b.up();} inline complex<LD> prosta_prosta(line a, line b) { LL det = wek(a,b); LL x = -a.c*b.b+b.c*a.b; LL y = -a.a*b.c+a.c*b.a; return complex<LD>(x,y)/(LD)det; } inline LL weaker (line a, line b) { // czy a jest slabsze niz b assert(rown(a,b)); if (abs(a.a) > abs(a.b)) return a.c*abs(b.a) - b.c*abs(a.a); else return a.c*abs(b.b) - b.c*abs(a.b); } struct Comp { bool operator()(const line& a, const line& b) const { if (a.up() != b.up()) return a.up() > b.up(); return wek(a,b) > 0; } }; struct przeciecie_polplaszczyzn { bool empty, pek; set<line, Comp> S; typedef set<line, Comp>::iterator iter; przeciecie_polplaszczyzn() : empty(false), pek(false) {}; iter next(iter it){return (++it == S.end() ? S.begin() : it);} iter prev(iter it){return (it == S.begin() ? --S.end() : --it);} bool hide(line a, line b, line c) { if (rown(a,b)) { if (weaker(a, -b) < 0) empty = true; return false; } if (wek(a,b) < 0) swap(a,b); complex<LD> r = prosta_prosta(a,b); LD v = r.X * c.a + r.Y * c.b + c.c; if (wek(a,c) >=0 && wek(c,b) >=0 && v > -EPS) return true; if (wek(a,c) < 0 && wek(c,b) < 0) { if (v < -EPS) empty = true; else if (v < EPS) pek = true; } return false; } void add(line l) { if (empty) return; if (l.a == 0 && l.b == 0) { if (l.c < 0) empty = true; return; } iter it = S.lower_bound(l); //rownolegle if(it != S.end() && podobne(*it, l)) { if (weaker(l, *it)>=0) return; iter del = it; it = next(it); S.erase(del); } //*it>p if(SZ(S) >= 2 && it == S.end()) it = S.begin(); while(SZ(S) >= 2 && hide(l, *next(it), *it)) { iter del = it; it = next(it); S.erase(del); } //*it<p if(SZ(S) >= 2) it = prev(it); while(SZ(S) >= 2 && hide(l, *prev(it), *it)) { iter del = it; it = prev(it); S.erase(del); } if(S.size() < 2 || !hide(*it, *next(it), l)) S.insert(l); } /* 0 - puste 1 - punkt 2 - odcinek 3 - półprosta 4 - prosta 5 - dodatnie (może nieskończone) pole (S.size() daje wowczas liczbę boków) */ int type() { if(empty) return 0; if(SZ(S) <= 4){ vector<line> res(ALL(S)); if (SZ(res) == 2 && rown(res[0], res[1]) && abs(weaker(res[0], -res[1]))<EPS) return 0; REP(i, SZ(res)) REP(j, i) if(pokr(res[i], res[j])) { if(SZ(res) == 2) return 4; if(SZ(res) == 3) return 3; if(SZ(res) == 4 && pokr(res[0], res[2]) && pokr(res[1], res[3])) return 1; return 2; } if(SZ(res) == 3 && pek) return 1; } return 5; } }; // END OF LIB bool operator == (P a, P b) { return a.x == b.x && a.y == b.y; } bool operator != (P a, P b) { return not (a == b); } ld dir_dist(L3 l, P he) { return (l.a * he.x + l.b * he.y + l.c) / sqrt(K(l.a) + K(l.b)); } P jedn(P a) { return a.scaleTo(1.0); } L3 from_line(line l) { return L3({l.a, l.b, l.c}); } // END OF HELP FUNS vector<vector<P>> towers; przeciecie_polplaszczyzn pp; bool cosik = false; void dorzuc(P a, P b) { line l1 = line({a.x, a.y}, {b.x, b.y}); line l2 = line({b.x, b.y}, {a.x, a.y}); L3 l3 = L3({l1.a, l1.b, l1.c}); ld mniej = 1e9; ld wiecej = -1e9; int mniej_id = 0; int wiecej_id = 0; for (int i = 1; i < towers.size(); i ++) { ld ad = dir_dist(l3, towers[i][0]); ld bd = dir_dist(l3, towers[i][1]); if (ad * bd >= 0) { // This point is damn good if (ad < 0 || bd < 0) { mniej = min({mniej, max(ad, bd)}); if (mniej == max(ad, bd)) mniej_id = i; } if (ad > 0 || bd > 0) { wiecej = max({wiecej, min(ad, bd)}); if (wiecej == min(ad, bd)) wiecej_id = i; } } } // cerr << l1.up() << "\n"; if (mniej <= 0) { P n = jedn(l3.normal()) * mniej; P a1 = a + n; P b1 = b + n; line x = line({a1.x, a1.y}, {b1.x, b1.y}); if (abs(max(dir_dist(from_line(x), towers[mniej_id][0]), dir_dist(from_line(x), towers[mniej_id][1]))) > EPS) { a1 = a - n; b1 = b - n; x = line({a1.x, a1.y}, {b1.x, b1.y}); } // cerr << x.a << " " << x.b << " " << x.c << "\n"; pp.add(x); cosik = true; } if (wiecej >= 0) { P n = jedn(l3.normal()) * wiecej; P a1 = a + n; P b1 = b + n; line x = line({b1.x, b1.y}, {a1.x, a1.y}); if (abs(max(dir_dist(from_line(x), towers[wiecej_id][0]), dir_dist(from_line(x), towers[wiecej_id][1]))) > EPS) { a1 = a - n; b1 = b - n; x = line({b1.x, b1.y}, {a1.x, a1.y}); } // cerr << x.a << " " << x.b << " " << x.c << "\n"; pp.add(x); cosik = true; } // cerr << mniej << "\n"; // cerr << wiecej << "\n"; // cerr << l1.a << " " << l1.b << " " << l1.c << "\n"; // cerr << "{ "<< a.x << " " << a.y << "} \n"; // cerr << "{ " << b.x << " " << b.y << "} \n"; // line a3 = line(l1.a, l1.b, l1.c + wiecej * sqrt(K(l1.a) + K(l1.b))); // line b3 = line(l2.a, l2.b, l2.c - mniej * sqrt(K(l2.a) + K(l2.b))); // if (wiecej >= 0) cerr << a3.a << " " << a3.b << " " << a3.c << "\n"; // if (mniej <= 0) cerr << b3.a << " " << b3.b << " " << b3.c << "\n"; // cout << pp.type() << "\n"; /* if (pp.type() == 5) { cerr << "BEG OF S\n"; for (line l : pp.S) { cerr << l.a <<" " << l.b << " " << l.c << "\n"; } cerr <<"END OF S\n"; }*/ } int main() { ios_base::sync_with_stdio(false); cout << setprecision(16) << fixed; int n; cin >> n; towers.resize(1); for (int i = 1; i <= n; i ++) { int a, b, c, d; cin >> a >> b >> c >> d; P p1 = P({a, b}); P p2 = P({c, d}); towers.push_back({p1, p2}); } for (int i = 1; i <= n; i ++) { for (int j = i + 1; j <= n; j ++) { for (P a : towers[i]) { for (P b : towers[j]) { if (a != b) { dorzuc(a, b); } } } } } LD result = 0; if (pp.type() != 5 || cosik == false) { cout << result << "\n"; return 0; } if (pp.type() == 5) { // cerr << "BEG OF S\n"; for (line l : pp.S) { // cerr << l.a <<" " << l.b << " " << l.c << "\n"; } // cerr <<"END OF S\n"; } // cout << "LOOOOL\n"; line last = *pp.S.rbegin(); vector<P> polygon; for (line l : pp.S) { polygon.push_back(from_line(last).inter(from_line(l))); last = l; } // cerr << polygon.size() << "\n"; // cerr << "POLYGON\n"; for (P p : polygon) { // cerr << p.x << " " << p.y << "\n"; } for (int i = 1; i < polygon.size() - 1; i ++) { result += (polygon[i] - polygon[0]) * (polygon[i+1] - polygon[0]); } cout << abs(result) / 2 <<"\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 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | #include <bits/stdc++.h> using namespace std; // Przepraszam za brzydki kod : ( typedef long double LL; typedef long double LD; typedef pair<int,int> PII; #define MP make_pair #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SZ(x) (int)x.size() #define ALL(c) c.begin(),c.end() // Kod pożyczony z biblioteczki UW0: // https://github.com/mareksom/acmlib/blob/master/code/kamil/halfplanes.cpp // halfplanes_online #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " template<typename T> T K(T a) { return a * a; } // 'K(int)' may overflow! typedef long double ll; // can be changed to 'long double' typedef long double ld; // const ld PI = 2 * acos(0); const ld eps = 1e-12; #pragma GCC diagnostic ignored "-Wnarrowing" struct P { ll x, y; P operator + (P b) { return P{x + b.x, y + b.y}; } P operator - (P b) { return P{x - b.x, y - b.y}; } P operator * (ld/*ll*/ mul) { return P{x * mul, y * mul}; } P operator / (ld mul) { assert(mul); return P{x / mul, y / mul}; } ll operator * (P b) { return x * b.y - y * b.x; } ll dot(P b) { return x * b.x + y * b.y; } ld len() { return sqrt(K(x) + K(y)); } P scaleTo(ld to) { return *this * (to / len()); } ld dist(P & b) { return (*this - b).len(); } P rotate90() { return P{-y, x}; } ld angle() { return atan2(y, x); } P rotate(ld ang) { ld c = cos(ang), s = sin(ang); return P{x * c - y * s, x * s + y * c}; } // '<' and 'below()' needed for Convex Hull bool operator < (P he) { return make_pair(x, y) < make_pair(he.x, he.y); } bool below(P a, P b) { return (b - a) * (*this - a) <= 0/*eps*/; } //INFO 1 void write(string s) { cerr << "(" << x << ", " << y << ")" << s; } // Internal/External Similitude Center P apol_in(P b, ld ratio) { // ratio = dist()/he.dist() return (*this + b * ratio) / (1 + ratio); } P apol_out(P b, ld ratio) { return (*this - b * ratio) / (1 - ratio); } void print(debug & dd) const { dd << make_pair(x, y); } }; /* using debug() ostream & operator << (ostream & dd, P p) { dd << "(" << p.x << ", " << p.y << ") "; return dd; } */ debug & operator << (debug & dd, P p) { p.print(dd); return dd; } struct L2 { P one, two; // P p[2]; P & operator [](int i) { return p[i]; } // const P & operator [](int i) const { return p[i]; } P dir() { return two - one; } P normal() { return dir().rotate90(); } ld dist(P he) { return abs((he - one) * (he - two)) / one.dist(two); } ld segDist(P he) { // epsilon not needed, but it would work too if((he - two) * normal() < 0 && normal() * (he - one) < 0) return dist(he); return min(one.dist(he), two.dist(he)); } P inter(L2 he) { P A = dir(), B = he.dir(); ll den = A * B; assert(abs(den) > eps); // parallel, maybe equal return (A * (he.one * he.two) - B * (one * two)) * (1.0 / den); // https://en.wikipedia.org/wiki/Line-line_intersection // A = (x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4) // A'= (x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4) // B = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) // return P{A / B, A' / B}; } P project(P he) { P unit_normal = normal().scaleTo(1); return he + unit_normal * unit_normal.dot(one - he); } P reflect(P he) { return project(he) * 2 - he; } void print(debug & dd) const { dd << imie(one) << imie(two); } void write() { cerr << "L2{ "; one.write(", "); cerr << " }\n"; } // for CH: sort by slope; below() : change to L3 or compare 'x' of intersections }; L2 toL2(ll a, ll b, ll c) { P first; if(abs(b) > eps) first = P{0, (ld) -c / b}; else if(abs(a) > eps) first = P{(ld) -c / a, 0}; else assert(false); return L2{first, first + P{b, -a}}; } ll det(ll t[3][3]) { // for CH of lines Ax+By+C=0 ll s = 0; for(int i = 0; i < 3; ++i) for(int j = i + 1, mul = 1; j != i + 3; ++j, mul -= 2) s += t[0][i] * t[1][j%3] * t[2][3-i-j%3] * mul; return s; } struct L3 { // a * x + b * y + c = 0, assert(b > 0 || (b == 0 && a > 0)) ll a, b, c; L3 fix() { // <done>TODO, test it</done> assert(abs(b) > eps || abs(a) > eps); ll g = (b > eps || (abs(b) < eps && a > eps)) ? 1 : -1; // __gcd(x,0) is undef-beh, http://codeforces.com/blog/entry/13410 // if(is_integral<ll>::value) g *= abs(__gcd(c, __gcd(a?b:a, a?a:b))); // if(is_floating_point<ll>::value) g *= sqrt(K(a) + K(b)); return L3{a / g, b / g, c / g}; } ld dist(P he) { return abs(a * he.x + b * he.y + c) / sqrt(K(a) + K(b)); } P dir() { return P{b, -a}; } P normal() { return P{a, b}; } // equivalently: dir().rotate90() P project(P he) { ld den = K(a) + K(b); // non-integer because we need division return P{(b * (b * he.x - a * he.y) - a * c) / den, (a * (a * he.y - b * he.x) - b * c) / den }; } P reflect(P he) { return project(he) * 2 - he; } P inter(L3 he) { #define Q(i, j) (i * he.j - j * he.i) ll den = Q(a, b); assert(abs(den) > 1e-14); // parallel, maybe equal return P{Q(b, c), Q(c, a)} * (1.0 / den); #undef Q } bool operator < (L3 he) { // produces the order for finding an upper envelope // assert(b > 0 && he.b > 0); // a / b < he.a / he.b, ties: -c/b < ... if(abs(a * he.b - b * he.a) < eps) return b * he.c < c * he.b; // <done>test it</done> return a * he.b < b * he.a; } bool below(L3 A, L3 C) { ll t[3][3] = { {A.a,A.b,A.c}, {a,b,c}, {C.a,C.b,C.c} }; return det(t) <= 0/*eps*/; // WARN1 } }; L3 toL3(P one, P two) { ll a = two.y - one.y; ll b = one.x - two.x; return L3{a, b, -(a * one.x + b * one.y)}.fix(); } #define X real() #define Y imag() typedef complex<LL> Pu; const LD EPS = 1e-10; struct line { LL a,b,c; line(LL a_ = 0, LL b_ = 0, LL c_ = 0): a(a_), b(b_), c(c_) {} // <= 10^9 line (Pu const &A, Pu const &B): a(A.Y-B.Y), b(B.X-A.X), c(A.X*B.Y-A.Y*B.X) {} //pts <= 10^6 line operator - () const {return line(-a, -b, -c); } bool up() const { return a?(a<0):(b>0);} }; inline LL wek(line const &a, line const &b) {return a.a*b.b-a.b*b.a;} inline bool rown(line a, line b) {return abs(wek(a,b)) < EPS;} inline bool pokr(line a, line b) {return rown(a,b) && a.a*b.c == b.a*a.c && a.b*b.c == b.b*a.c;} inline bool podobne(line a, line b) {return rown(a,b) && a.up() == b.up();} inline complex<LD> prosta_prosta(line a, line b) { LL det = wek(a,b); LL x = -a.c*b.b+b.c*a.b; LL y = -a.a*b.c+a.c*b.a; return complex<LD>(x,y)/(LD)det; } inline LL weaker (line a, line b) { // czy a jest slabsze niz b assert(rown(a,b)); if (abs(a.a) > abs(a.b)) return a.c*abs(b.a) - b.c*abs(a.a); else return a.c*abs(b.b) - b.c*abs(a.b); } struct Comp { bool operator()(const line& a, const line& b) const { if (a.up() != b.up()) return a.up() > b.up(); return wek(a,b) > 0; } }; struct przeciecie_polplaszczyzn { bool empty, pek; set<line, Comp> S; typedef set<line, Comp>::iterator iter; przeciecie_polplaszczyzn() : empty(false), pek(false) {}; iter next(iter it){return (++it == S.end() ? S.begin() : it);} iter prev(iter it){return (it == S.begin() ? --S.end() : --it);} bool hide(line a, line b, line c) { if (rown(a,b)) { if (weaker(a, -b) < 0) empty = true; return false; } if (wek(a,b) < 0) swap(a,b); complex<LD> r = prosta_prosta(a,b); LD v = r.X * c.a + r.Y * c.b + c.c; if (wek(a,c) >=0 && wek(c,b) >=0 && v > -EPS) return true; if (wek(a,c) < 0 && wek(c,b) < 0) { if (v < -EPS) empty = true; else if (v < EPS) pek = true; } return false; } void add(line l) { if (empty) return; if (l.a == 0 && l.b == 0) { if (l.c < 0) empty = true; return; } iter it = S.lower_bound(l); //rownolegle if(it != S.end() && podobne(*it, l)) { if (weaker(l, *it)>=0) return; iter del = it; it = next(it); S.erase(del); } //*it>p if(SZ(S) >= 2 && it == S.end()) it = S.begin(); while(SZ(S) >= 2 && hide(l, *next(it), *it)) { iter del = it; it = next(it); S.erase(del); } //*it<p if(SZ(S) >= 2) it = prev(it); while(SZ(S) >= 2 && hide(l, *prev(it), *it)) { iter del = it; it = prev(it); S.erase(del); } if(S.size() < 2 || !hide(*it, *next(it), l)) S.insert(l); } /* 0 - puste 1 - punkt 2 - odcinek 3 - półprosta 4 - prosta 5 - dodatnie (może nieskończone) pole (S.size() daje wowczas liczbę boków) */ int type() { if(empty) return 0; if(SZ(S) <= 4){ vector<line> res(ALL(S)); if (SZ(res) == 2 && rown(res[0], res[1]) && abs(weaker(res[0], -res[1]))<EPS) return 0; REP(i, SZ(res)) REP(j, i) if(pokr(res[i], res[j])) { if(SZ(res) == 2) return 4; if(SZ(res) == 3) return 3; if(SZ(res) == 4 && pokr(res[0], res[2]) && pokr(res[1], res[3])) return 1; return 2; } if(SZ(res) == 3 && pek) return 1; } return 5; } }; // END OF LIB bool operator == (P a, P b) { return a.x == b.x && a.y == b.y; } bool operator != (P a, P b) { return not (a == b); } ld dir_dist(L3 l, P he) { return (l.a * he.x + l.b * he.y + l.c) / sqrt(K(l.a) + K(l.b)); } P jedn(P a) { return a.scaleTo(1.0); } L3 from_line(line l) { return L3({l.a, l.b, l.c}); } // END OF HELP FUNS vector<vector<P>> towers; przeciecie_polplaszczyzn pp; bool cosik = false; void dorzuc(P a, P b) { line l1 = line({a.x, a.y}, {b.x, b.y}); line l2 = line({b.x, b.y}, {a.x, a.y}); L3 l3 = L3({l1.a, l1.b, l1.c}); ld mniej = 1e9; ld wiecej = -1e9; int mniej_id = 0; int wiecej_id = 0; for (int i = 1; i < towers.size(); i ++) { ld ad = dir_dist(l3, towers[i][0]); ld bd = dir_dist(l3, towers[i][1]); if (ad * bd >= 0) { // This point is damn good if (ad < 0 || bd < 0) { mniej = min({mniej, max(ad, bd)}); if (mniej == max(ad, bd)) mniej_id = i; } if (ad > 0 || bd > 0) { wiecej = max({wiecej, min(ad, bd)}); if (wiecej == min(ad, bd)) wiecej_id = i; } } } // cerr << l1.up() << "\n"; if (mniej <= 0) { P n = jedn(l3.normal()) * mniej; P a1 = a + n; P b1 = b + n; line x = line({a1.x, a1.y}, {b1.x, b1.y}); if (abs(max(dir_dist(from_line(x), towers[mniej_id][0]), dir_dist(from_line(x), towers[mniej_id][1]))) > EPS) { a1 = a - n; b1 = b - n; x = line({a1.x, a1.y}, {b1.x, b1.y}); } // cerr << x.a << " " << x.b << " " << x.c << "\n"; pp.add(x); cosik = true; } if (wiecej >= 0) { P n = jedn(l3.normal()) * wiecej; P a1 = a + n; P b1 = b + n; line x = line({b1.x, b1.y}, {a1.x, a1.y}); if (abs(max(dir_dist(from_line(x), towers[wiecej_id][0]), dir_dist(from_line(x), towers[wiecej_id][1]))) > EPS) { a1 = a - n; b1 = b - n; x = line({b1.x, b1.y}, {a1.x, a1.y}); } // cerr << x.a << " " << x.b << " " << x.c << "\n"; pp.add(x); cosik = true; } // cerr << mniej << "\n"; // cerr << wiecej << "\n"; // cerr << l1.a << " " << l1.b << " " << l1.c << "\n"; // cerr << "{ "<< a.x << " " << a.y << "} \n"; // cerr << "{ " << b.x << " " << b.y << "} \n"; // line a3 = line(l1.a, l1.b, l1.c + wiecej * sqrt(K(l1.a) + K(l1.b))); // line b3 = line(l2.a, l2.b, l2.c - mniej * sqrt(K(l2.a) + K(l2.b))); // if (wiecej >= 0) cerr << a3.a << " " << a3.b << " " << a3.c << "\n"; // if (mniej <= 0) cerr << b3.a << " " << b3.b << " " << b3.c << "\n"; // cout << pp.type() << "\n"; /* if (pp.type() == 5) { cerr << "BEG OF S\n"; for (line l : pp.S) { cerr << l.a <<" " << l.b << " " << l.c << "\n"; } cerr <<"END OF S\n"; }*/ } int main() { ios_base::sync_with_stdio(false); cout << setprecision(16) << fixed; int n; cin >> n; towers.resize(1); for (int i = 1; i <= n; i ++) { int a, b, c, d; cin >> a >> b >> c >> d; P p1 = P({a, b}); P p2 = P({c, d}); towers.push_back({p1, p2}); } for (int i = 1; i <= n; i ++) { for (int j = i + 1; j <= n; j ++) { for (P a : towers[i]) { for (P b : towers[j]) { if (a != b) { dorzuc(a, b); } } } } } LD result = 0; if (pp.type() != 5 || cosik == false) { cout << result << "\n"; return 0; } if (pp.type() == 5) { // cerr << "BEG OF S\n"; for (line l : pp.S) { // cerr << l.a <<" " << l.b << " " << l.c << "\n"; } // cerr <<"END OF S\n"; } // cout << "LOOOOL\n"; line last = *pp.S.rbegin(); vector<P> polygon; for (line l : pp.S) { polygon.push_back(from_line(last).inter(from_line(l))); last = l; } // cerr << polygon.size() << "\n"; // cerr << "POLYGON\n"; for (P p : polygon) { // cerr << p.x << " " << p.y << "\n"; } for (int i = 1; i < polygon.size() - 1; i ++) { result += (polygon[i] - polygon[0]) * (polygon[i+1] - polygon[0]); } cout << abs(result) / 2 <<"\n"; } |