#include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif // halfplanes_online #define X real() #define Y imag() typedef complex<LL> P; 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 (P const &A, P 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;} int cross_prod(P a, P b){ return (conj(a) * b).Y; } inline bool rown(line a, line b) {return wek(a,b) == 0;} 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; } }; const LD EPS = 1e-12; 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]) && weaker(res[0], -res[1])<0) 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; } }; template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; vector<pair<P,P>> inp(n); REP(i, n){ int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; inp[i] = {{x1,y1}, {x2,y2}}; } przeciecie_polplaszczyzn pp; REP(j, n){ for(P p1: {inp[j].st, inp[j].nd}){ REP(i, j){ for(P p2: {inp[i].st, inp[i].nd}){ for(pair<P,P> l: {pair<P,P>{p1, p2}, pair<P,P>{p2, p1}}){ //debug(l.st, l.nd); REP(k, n){ if(i == k || j == k) continue; int c1 = cross_prod(l.nd - l.st, inp[k].st - l.st); int c2 = cross_prod(l.nd - l.st, inp[k].nd - l.st); //debug(inp[k].st, inp[k].nd, c1, c2); if(c1 < 0 && c2 < 0){ goto nxt; } } pp.add(line(l.st, l.nd)); //debug(pp.type()); nxt:; } } } } } if(pp.type() != 5){ cout<<0<<"\n"; return 0; } debug(pp.S.size()); auto it = pp.S.begin(); auto it2 = ++pp.S.begin(); vector<complex<LD>> pts; pts.reserve(pp.S.size()); REP(i, SZ(pp.S)){ debug(prosta_prosta(*it, *it2)); pts.push_back(prosta_prosta(*it, *it2)); it = pp.next(it); it2 = pp.next(it2); } LD pole = 0; REP(i, SZ(pts)){ int ni = (i + 1) % SZ(pts); complex<LD> p1 = pts[i], p2 = pts[ni]; pole += (p1.Y + p2.Y) * (p2.X - p1.X); } pole /= 2; pole = abs(pole); cout<<fixed<<setprecision(15); cout<<pole<<"\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 | #include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif // halfplanes_online #define X real() #define Y imag() typedef complex<LL> P; 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 (P const &A, P 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;} int cross_prod(P a, P b){ return (conj(a) * b).Y; } inline bool rown(line a, line b) {return wek(a,b) == 0;} 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; } }; const LD EPS = 1e-12; 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]) && weaker(res[0], -res[1])<0) 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; } }; template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; vector<pair<P,P>> inp(n); REP(i, n){ int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; inp[i] = {{x1,y1}, {x2,y2}}; } przeciecie_polplaszczyzn pp; REP(j, n){ for(P p1: {inp[j].st, inp[j].nd}){ REP(i, j){ for(P p2: {inp[i].st, inp[i].nd}){ for(pair<P,P> l: {pair<P,P>{p1, p2}, pair<P,P>{p2, p1}}){ //debug(l.st, l.nd); REP(k, n){ if(i == k || j == k) continue; int c1 = cross_prod(l.nd - l.st, inp[k].st - l.st); int c2 = cross_prod(l.nd - l.st, inp[k].nd - l.st); //debug(inp[k].st, inp[k].nd, c1, c2); if(c1 < 0 && c2 < 0){ goto nxt; } } pp.add(line(l.st, l.nd)); //debug(pp.type()); nxt:; } } } } } if(pp.type() != 5){ cout<<0<<"\n"; return 0; } debug(pp.S.size()); auto it = pp.S.begin(); auto it2 = ++pp.S.begin(); vector<complex<LD>> pts; pts.reserve(pp.S.size()); REP(i, SZ(pp.S)){ debug(prosta_prosta(*it, *it2)); pts.push_back(prosta_prosta(*it, *it2)); it = pp.next(it); it2 = pp.next(it2); } LD pole = 0; REP(i, SZ(pts)){ int ni = (i + 1) % SZ(pts); complex<LD> p1 = pts[i], p2 = pts[ni]; pole += (p1.Y + p2.Y) * (p2.X - p1.X); } pole /= 2; pole = abs(pole); cout<<fixed<<setprecision(15); cout<<pole<<"\n"; } |