#include <bits/stdc++.h> using namespace std; typedef long double ld; const ld pi = acos(-1); const ld eps = 1e-12; struct point_t { ld x, y; point_t(ld x = 0, ld y = 0): x(x), y(y) { } point_t operator + (const point_t &b) const { return point_t(x + b.x, y + b.y); } point_t operator - (const point_t &b) const { return point_t(x - b.x, y - b.y); } point_t operator * (const ld &b) const { return point_t(x * b, y * b); } ld operator * (const point_t &b) const { return x * b.y - y * b.x; } bool operator < (const point_t &b) const { return x < b.x || (x == b.x && y < b.y); } bool operator == (const point_t &b) const { return x == b.x && y == b.y; } bool operator != (const point_t &b) const { return x != b.x || y != b.y; } }; struct line_t { point_t p, v; ld slope; line_t() { } line_t(point_t p, point_t v): p(p), v(v) { slope = atan2l(v.y, v.x); } bool operator < (const line_t &b) const { return slope < b.slope; } }; template<typename T> inline int sign(const T &x) { return fabsl(x) < eps ? 0 : x > 0 ? 1 : -1; } bool on_left(const line_t &l, const point_t &p) { return sign(l.v * (p - l.p)) >= 0; } point_t intersect(const line_t &u, const line_t &v) { ld w = ((v.p - u.p) * v.v) / (u.v * v.v); return u.p + u.v * w; } ld semiplane(vector<line_t> l) { int n = l.size(); sort(l.begin(), l.end()); vector<line_t> q(n); vector<point_t> p(n); int ql = 0, qr = 0; q[0] = l[0]; for (int i = 1; i < n; ++i) { while (ql < qr && !on_left(l[i], p[qr - 1])) { --qr; } while (ql < qr && !on_left(l[i], p[ql])) { ++ql; } if (!sign(l[i].v * q[qr].v)) { if (!on_left(l[i], q[qr].p)) { q[qr] = l[i]; } } else { q[++qr] = l[i]; } if (ql < qr) { p[qr - 1] = intersect(q[qr - 1], q[qr]); } } while (ql < qr && !on_left(q[ql], p[qr - 1])) { --qr; } if (ql + 1 >= qr) { return 0; } p[qr] = intersect(q[qr], q[ql]); ld answer = p[qr] * p[ql]; for (int i = ql; i < qr; ++i) { answer += p[i] * p[i + 1]; } if (answer < 0) { answer = 0; } return answer / 2; } int main() { #ifdef wxh010910 freopen("input.txt", "r", stdin); #endif int n; scanf("%d", &n); vector<point_t> a(n), b(n); for (int i = 0; i < n; ++i) { int ax, ay, bx, by; scanf("%d %d %d %d", &ax, &ay, &bx, &by); a[i] = point_t(ax, ay); b[i] = point_t(bx, by); } vector<ld> angles; angles.push_back(0); angles.push_back(pi * 2); auto add = [&](point_t a, point_t b) { if (a == b) { return; } ld theta = atan2l(b.y - a.y, b.x - a.x) + pi / 2; while (theta < 0) { theta += pi * 2; } while (theta >= pi * 2) { theta -= pi * 2; } angles.push_back(theta); theta += pi; while (theta < 0) { theta += pi * 2; } while (theta >= pi * 2) { theta -= pi * 2; } angles.push_back(theta); }; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { add(a[i], a[j]); add(a[i], b[j]); add(b[i], a[j]); add(b[i], b[j]); } add(a[i], b[i]); } sort(angles.begin(), angles.end()); angles.erase(unique(angles.begin(), angles.end()), angles.end()); vector<line_t> lines; auto solve = [&](ld theta) { theta = 2 * pi - theta; ld c = cosl(theta), s = sinl(theta); ld xmax = -1e9; point_t p(0, 0); for (int i = 0; i < n; ++i) { ld xa = a[i].x * c - a[i].y * s; ld xb = b[i].x * c - b[i].y * s; if (xa > xb) { if (xb > xmax) { xmax = xb; p = b[i]; } } else { if (xa > xmax) { xmax = xa; p = a[i]; } } } return p; }; auto insert = [&](point_t p, ld theta) { lines.push_back(line_t(p, point_t(cosl(theta + pi / 2), sinl(theta + pi / 2)))); }; for (int i = 0; i < angles.size() - 1; ++i) { point_t p = solve((angles[i] + angles[i + 1]) / 2); insert(p, angles[i]); insert(p, angles[i + 1]); } printf("%.10lf\n", (double)(semiplane(lines) + eps)); return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long double ld; const ld pi = acos(-1); const ld eps = 1e-12; struct point_t { ld x, y; point_t(ld x = 0, ld y = 0): x(x), y(y) { } point_t operator + (const point_t &b) const { return point_t(x + b.x, y + b.y); } point_t operator - (const point_t &b) const { return point_t(x - b.x, y - b.y); } point_t operator * (const ld &b) const { return point_t(x * b, y * b); } ld operator * (const point_t &b) const { return x * b.y - y * b.x; } bool operator < (const point_t &b) const { return x < b.x || (x == b.x && y < b.y); } bool operator == (const point_t &b) const { return x == b.x && y == b.y; } bool operator != (const point_t &b) const { return x != b.x || y != b.y; } }; struct line_t { point_t p, v; ld slope; line_t() { } line_t(point_t p, point_t v): p(p), v(v) { slope = atan2l(v.y, v.x); } bool operator < (const line_t &b) const { return slope < b.slope; } }; template<typename T> inline int sign(const T &x) { return fabsl(x) < eps ? 0 : x > 0 ? 1 : -1; } bool on_left(const line_t &l, const point_t &p) { return sign(l.v * (p - l.p)) >= 0; } point_t intersect(const line_t &u, const line_t &v) { ld w = ((v.p - u.p) * v.v) / (u.v * v.v); return u.p + u.v * w; } ld semiplane(vector<line_t> l) { int n = l.size(); sort(l.begin(), l.end()); vector<line_t> q(n); vector<point_t> p(n); int ql = 0, qr = 0; q[0] = l[0]; for (int i = 1; i < n; ++i) { while (ql < qr && !on_left(l[i], p[qr - 1])) { --qr; } while (ql < qr && !on_left(l[i], p[ql])) { ++ql; } if (!sign(l[i].v * q[qr].v)) { if (!on_left(l[i], q[qr].p)) { q[qr] = l[i]; } } else { q[++qr] = l[i]; } if (ql < qr) { p[qr - 1] = intersect(q[qr - 1], q[qr]); } } while (ql < qr && !on_left(q[ql], p[qr - 1])) { --qr; } if (ql + 1 >= qr) { return 0; } p[qr] = intersect(q[qr], q[ql]); ld answer = p[qr] * p[ql]; for (int i = ql; i < qr; ++i) { answer += p[i] * p[i + 1]; } if (answer < 0) { answer = 0; } return answer / 2; } int main() { #ifdef wxh010910 freopen("input.txt", "r", stdin); #endif int n; scanf("%d", &n); vector<point_t> a(n), b(n); for (int i = 0; i < n; ++i) { int ax, ay, bx, by; scanf("%d %d %d %d", &ax, &ay, &bx, &by); a[i] = point_t(ax, ay); b[i] = point_t(bx, by); } vector<ld> angles; angles.push_back(0); angles.push_back(pi * 2); auto add = [&](point_t a, point_t b) { if (a == b) { return; } ld theta = atan2l(b.y - a.y, b.x - a.x) + pi / 2; while (theta < 0) { theta += pi * 2; } while (theta >= pi * 2) { theta -= pi * 2; } angles.push_back(theta); theta += pi; while (theta < 0) { theta += pi * 2; } while (theta >= pi * 2) { theta -= pi * 2; } angles.push_back(theta); }; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { add(a[i], a[j]); add(a[i], b[j]); add(b[i], a[j]); add(b[i], b[j]); } add(a[i], b[i]); } sort(angles.begin(), angles.end()); angles.erase(unique(angles.begin(), angles.end()), angles.end()); vector<line_t> lines; auto solve = [&](ld theta) { theta = 2 * pi - theta; ld c = cosl(theta), s = sinl(theta); ld xmax = -1e9; point_t p(0, 0); for (int i = 0; i < n; ++i) { ld xa = a[i].x * c - a[i].y * s; ld xb = b[i].x * c - b[i].y * s; if (xa > xb) { if (xb > xmax) { xmax = xb; p = b[i]; } } else { if (xa > xmax) { xmax = xa; p = a[i]; } } } return p; }; auto insert = [&](point_t p, ld theta) { lines.push_back(line_t(p, point_t(cosl(theta + pi / 2), sinl(theta + pi / 2)))); }; for (int i = 0; i < angles.size() - 1; ++i) { point_t p = solve((angles[i] + angles[i + 1]) / 2); insert(p, angles[i]); insert(p, angles[i + 1]); } printf("%.10lf\n", (double)(semiplane(lines) + eps)); return 0; } |