#define _USE_MATH_DEFINES///////////////////////////////////////////////////// #include <bits/stdc++.h>////////////////////////////////////////////////////// #ifdef LOC//////////////////////////////////////////////////////////////////// #include "debuglib.h"///////////////////////////////////////////////////////// #else///////////////////////////////////////////////////////////////////////// #define deb(...)////////////////////////////////////////////////////////////// #define DBP(...)////////////////////////////////////////////////////////////// #endif//////////////////////////////////////////////////////////////////////// #define x first/////////////////////////////////////////////////////////////// #define y second////////////////////////////////////////////////////////////// #define mp make_pair////////////////////////////////////////////////////////// #define pb push_back////////////////////////////////////////////////////////// #define sz(x)int((x).size())////////////////////////////////////////////////// #define each(a,x)for(auto&a:(x))////////////////////////////////////////////// #define all(x)(x).begin(),(x).end()/////////////////////////////////////////// #define rep(i,b,e)for(int i=(b);i<(e);i++)//////////////////////////////////// using namespace std;using namespace rel_ops;using ll=int64_t;using Pii=pair/// <int,int>;using ull=uint64_t;using Vi=vector<int>;void run();int main(){cin.// sync_with_stdio(0);cin.tie(0);cout<<fixed<<setprecision(13);run();return 0;}// //--------------------------------------------------------------------------// using dbl = long double; constexpr dbl EPS = 1e-9; int cmp(dbl a, dbl b, dbl eps=EPS) { return (a>b+eps) - (a+eps<b); } template<class T, class S> struct bvec2 { T x, y; S operator+(S r) const {return{x+r.x,y+r.y};} S operator-(S r) const {return{x-r.x,y-r.y};} S operator*(T r) const { return {x*r, y*r}; } S operator/(T r) const { return {x/r, y/r}; } S operator-() const { return {-x, -y}; } T dot(S r) const { return x*r.x+y*r.y; } T cross(S r) const { return x*r.y-y*r.x; } T len2() const { return x*x + y*y; } dbl len() const { return sqrt(len2()); } S perp() const { return {-y,x}; } //90deg pair<T, T> yxPair() const { return mp(y,x); } }; struct vec2d : bvec2<dbl, vec2d> { vec2d() : bvec2{0, 0} {} vec2d(dbl a, dbl b) : bvec2{a, b} {} vec2d unit() const { return *this/len(); } bool operator==(vec2d r) const { return !cmp(x, r.x) && !cmp(y, r.y); } int cmpAngle(vec2d r) const { int u = upper() - r.upper(); return u ? u : cmp(cross(r), 0); } bool upper() const { int t = cmp(y, 0); return t > 0 || (t == 0 && cmp(x, 0) >= 0); } }; template<class T, class P, class S> struct bline2 { // norm*point == off P norm; // Normal vector [A; B] T off; // Offset (C parameter of equation) static S through(P a, P b) { return { (b-a).perp(), b.cross(a) }; } }; struct line2d : bline2<dbl, vec2d, line2d> { line2d() : bline2{{}, 0} {} line2d(vec2d n, dbl c) : bline2{n, c} {} int side(vec2d a) { return cmp(norm.dot(a), off); } bool intersect(line2d a, vec2d& out) { dbl d = norm.cross(a.norm); if (cmp(d, 0) == 0) return false; out = (norm*a.off-a.norm*off).perp() / d; return true; } bool lower(const line2d& r) const { return off*r.norm.len() < r.off*norm.len(); } bool operator<(const line2d& r) const { return norm.cmpAngle(r.norm) > 0; } }; using vec2 = vec2d; using line2 = line2d; // --- struct HalfLine { line2 line; vec2 begin; int id; }; vector<pair<vec2, vec2>> points; vector<line2> halfPlanes; void answer(dbl ans) { cout << ans << endl; exit(0); } void check(vec2 from, vec2 to) { line2 line = line2::through(from, to); bool negHalf = 0, posHalf = 0; each(p, points) { int a = line.side(p.x), b = line.side(p.y); if (a < 0 && b < 0) negHalf = 1; else if (a > 0 && b > 0) posHalf = 1; } if (!negHalf && !posHalf) { answer(0); } if (negHalf == posHalf) return; if (posHalf) { line.norm = -line.norm; line.off = -line.off; } else { swap(from, to); } halfPlanes.pb(line); } void run() { int n; cin >> n; points.resize(n); each(p, points) cin >> p.x.x >> p.x.y >> p.y.x >> p.y.y; rep(i, 0, n) rep(j, i+1, n) { check(points[i].x, points[j].x); check(points[i].x, points[j].y); check(points[i].y, points[j].x); check(points[i].y, points[j].y); } if (halfPlanes.empty()) answer(0); sort(all(halfPlanes)); vector<HalfLine> inter; rep(step, 0, 2) { rep(i, 0, sz(halfPlanes)) { auto& cur = halfPlanes[i]; HalfLine half{cur, {}, i}; bool ok = 1; while (!inter.empty()) { auto& top = inter.back(); if (!cur.intersect(top.line, half.begin)) { if (top.line.lower(cur)) { ok = 0; break; } inter.pop_back(); continue; } if (sz(inter) <= 1) break; dbl curPos = half.begin.cross(top.line.norm); dbl topPos = top.begin.cross(top.line.norm); if (cmp(topPos, curPos) > 0) break; inter.pop_back(); } if (ok) inter.pb(half); } } Vi counts(sz(halfPlanes)); vector<vec2> border; each(h, inter) { counts[h.id]++; if (counts[h.id] == 2) { border.pb(h.begin); } } dbl area = 0; rep(i, 2, sz(border)) { vec2 a = border[i] - border[0]; vec2 b = border[i-1] - border[0]; area += abs(a.cross(b) / 2); } cout << area << endl; }
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 | #define _USE_MATH_DEFINES///////////////////////////////////////////////////// #include <bits/stdc++.h>////////////////////////////////////////////////////// #ifdef LOC//////////////////////////////////////////////////////////////////// #include "debuglib.h"///////////////////////////////////////////////////////// #else///////////////////////////////////////////////////////////////////////// #define deb(...)////////////////////////////////////////////////////////////// #define DBP(...)////////////////////////////////////////////////////////////// #endif//////////////////////////////////////////////////////////////////////// #define x first/////////////////////////////////////////////////////////////// #define y second////////////////////////////////////////////////////////////// #define mp make_pair////////////////////////////////////////////////////////// #define pb push_back////////////////////////////////////////////////////////// #define sz(x)int((x).size())////////////////////////////////////////////////// #define each(a,x)for(auto&a:(x))////////////////////////////////////////////// #define all(x)(x).begin(),(x).end()/////////////////////////////////////////// #define rep(i,b,e)for(int i=(b);i<(e);i++)//////////////////////////////////// using namespace std;using namespace rel_ops;using ll=int64_t;using Pii=pair/// <int,int>;using ull=uint64_t;using Vi=vector<int>;void run();int main(){cin.// sync_with_stdio(0);cin.tie(0);cout<<fixed<<setprecision(13);run();return 0;}// //--------------------------------------------------------------------------// using dbl = long double; constexpr dbl EPS = 1e-9; int cmp(dbl a, dbl b, dbl eps=EPS) { return (a>b+eps) - (a+eps<b); } template<class T, class S> struct bvec2 { T x, y; S operator+(S r) const {return{x+r.x,y+r.y};} S operator-(S r) const {return{x-r.x,y-r.y};} S operator*(T r) const { return {x*r, y*r}; } S operator/(T r) const { return {x/r, y/r}; } S operator-() const { return {-x, -y}; } T dot(S r) const { return x*r.x+y*r.y; } T cross(S r) const { return x*r.y-y*r.x; } T len2() const { return x*x + y*y; } dbl len() const { return sqrt(len2()); } S perp() const { return {-y,x}; } //90deg pair<T, T> yxPair() const { return mp(y,x); } }; struct vec2d : bvec2<dbl, vec2d> { vec2d() : bvec2{0, 0} {} vec2d(dbl a, dbl b) : bvec2{a, b} {} vec2d unit() const { return *this/len(); } bool operator==(vec2d r) const { return !cmp(x, r.x) && !cmp(y, r.y); } int cmpAngle(vec2d r) const { int u = upper() - r.upper(); return u ? u : cmp(cross(r), 0); } bool upper() const { int t = cmp(y, 0); return t > 0 || (t == 0 && cmp(x, 0) >= 0); } }; template<class T, class P, class S> struct bline2 { // norm*point == off P norm; // Normal vector [A; B] T off; // Offset (C parameter of equation) static S through(P a, P b) { return { (b-a).perp(), b.cross(a) }; } }; struct line2d : bline2<dbl, vec2d, line2d> { line2d() : bline2{{}, 0} {} line2d(vec2d n, dbl c) : bline2{n, c} {} int side(vec2d a) { return cmp(norm.dot(a), off); } bool intersect(line2d a, vec2d& out) { dbl d = norm.cross(a.norm); if (cmp(d, 0) == 0) return false; out = (norm*a.off-a.norm*off).perp() / d; return true; } bool lower(const line2d& r) const { return off*r.norm.len() < r.off*norm.len(); } bool operator<(const line2d& r) const { return norm.cmpAngle(r.norm) > 0; } }; using vec2 = vec2d; using line2 = line2d; // --- struct HalfLine { line2 line; vec2 begin; int id; }; vector<pair<vec2, vec2>> points; vector<line2> halfPlanes; void answer(dbl ans) { cout << ans << endl; exit(0); } void check(vec2 from, vec2 to) { line2 line = line2::through(from, to); bool negHalf = 0, posHalf = 0; each(p, points) { int a = line.side(p.x), b = line.side(p.y); if (a < 0 && b < 0) negHalf = 1; else if (a > 0 && b > 0) posHalf = 1; } if (!negHalf && !posHalf) { answer(0); } if (negHalf == posHalf) return; if (posHalf) { line.norm = -line.norm; line.off = -line.off; } else { swap(from, to); } halfPlanes.pb(line); } void run() { int n; cin >> n; points.resize(n); each(p, points) cin >> p.x.x >> p.x.y >> p.y.x >> p.y.y; rep(i, 0, n) rep(j, i+1, n) { check(points[i].x, points[j].x); check(points[i].x, points[j].y); check(points[i].y, points[j].x); check(points[i].y, points[j].y); } if (halfPlanes.empty()) answer(0); sort(all(halfPlanes)); vector<HalfLine> inter; rep(step, 0, 2) { rep(i, 0, sz(halfPlanes)) { auto& cur = halfPlanes[i]; HalfLine half{cur, {}, i}; bool ok = 1; while (!inter.empty()) { auto& top = inter.back(); if (!cur.intersect(top.line, half.begin)) { if (top.line.lower(cur)) { ok = 0; break; } inter.pop_back(); continue; } if (sz(inter) <= 1) break; dbl curPos = half.begin.cross(top.line.norm); dbl topPos = top.begin.cross(top.line.norm); if (cmp(topPos, curPos) > 0) break; inter.pop_back(); } if (ok) inter.pb(half); } } Vi counts(sz(halfPlanes)); vector<vec2> border; each(h, inter) { counts[h.id]++; if (counts[h.id] == 2) { border.pb(h.begin); } } dbl area = 0; rep(i, 2, sz(border)) { vec2 a = border[i] - border[0]; vec2 b = border[i-1] - border[0]; area += abs(a.cross(b) / 2); } cout << area << endl; } |