#include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define pb push_back #define mp make_pair #define st first #define nd second typedef LL Int10; typedef LL Int20; struct Point { Int10 x, y; }; Int20 operator*(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; } Int20 det(const Point& a, const Point& b) { return a.x * b.y - b.x * a.y; } Point operator-(const Point& a, const Point& b) { return {a.x - b.x, a.y - b.y}; } Point cross(const Point& a) { return {-a.y, a.x}; } struct HalfPlane { // p * a <= c Point p; Int20 c; }; ostream& operator<<(ostream& o, const HalfPlane& h) { return o << h.p.x << "X" << (h.p.y < 0 ? "" : "+") <<h.p.y<<"Y <= "<<h.c; } struct Intersection { double x; double y; }; Intersection line_intersection(const HalfPlane& a, const HalfPlane& b) { Int20 d = det(a.p, b.p); assert(d); Point p = cross({a.p.x * b.c - b.p.x * a.c, a.p.y * b.c - b.p.y * a.c}); return {p.x * 1.0 / d, p.y * 1.0 / d}; } Point towers[105][2]; const double EPS = 1e-10; int main(int argc, char* argv[]) { int N; scanf("%d", &N); REP(i,N)REP(j,2) scanf("%lld%lld", &towers[i][j].x, &towers[i][j].y); vector<HalfPlane> upper, lower; double minX = -1e3, maxX = 1e3; REP(i,N)REP(j,N) if (i != j) REP(k,2)REP(l,2) { Point a = towers[i][k], b = towers[j][l]; HalfPlane h; h.p = cross(b-a); h.c = -1e8; REP(m,N) { h.c = max(h.c, min(h.p * towers[m][0], h.p * towers[m][1])); } if (h.p.y == 0) { if (h.p.x > 0) maxX = min(maxX, h.c / (double)h.p.x); else minX = max(minX, h.c / (double)h.p.x); } else if (h.p.y > 0) upper.pb(h); else lower.pb(h); } if (minX + 1e-9 >= maxX) { printf("%0.15lf\n", 0.0); return 0; } auto compute_hull = [](vector<HalfPlane> half_planes) { sort(half_planes.begin(), half_planes.end(), [](const HalfPlane& h1, const HalfPlane& h2) { return det(h1.p, h2.p) < 0; }); half_planes.erase( unique(half_planes.begin(), half_planes.end(), [](const HalfPlane& h1, const HalfPlane& h2) { return det(h1.p, h2.p) == 0; }), half_planes.end() ); vector<HalfPlane> hull; // cout << "START " << endl; for (auto& h: half_planes) { // cout << h << endl; while (hull.size() > 1) { Intersection i = line_intersection(hull[hull.size() - 2], hull[hull.size() - 1]); if (i.x * h.p.x + i.y * h.p.y <= h.c) break; hull.pop_back(); } hull.pb(h); } return hull; }; vector<HalfPlane> upper_hull = compute_hull(upper); vector<HalfPlane> lower_hull = compute_hull(lower); reverse(lower_hull.begin(), lower_hull.end()); vector<double> Xs = {minX, maxX}; REP(i, (int)upper_hull.size() - 1) { double x = line_intersection(upper_hull[i], upper_hull[i+1]).x; if (x > minX && x < maxX) Xs.pb(x); } REP(i, (int)lower_hull.size() - 1) { double x = line_intersection(lower_hull[i], lower_hull[i+1]).x; if (x > minX && x < maxX) Xs.pb(x); } sort(Xs.begin(), Xs.end()); vector<pair<double, double>> Ys; int uix = 0, lix = 0; for (auto x: Xs) { while (uix + 1 < upper_hull.size() && line_intersection(upper_hull[uix], upper_hull[uix+1]).x < x) ++uix; while (lix + 1 < lower_hull.size() && line_intersection(lower_hull[lix], lower_hull[lix+1]).x < x) ++lix; auto at_x = [](const HalfPlane& h, const double& x) { return (h.c - h.p.x * x) / h.p.y; }; Ys.pb({at_x(lower_hull[lix], x), at_x(upper_hull[uix], x)}); } double result = 0.0; REP(i, Xs.size() - 1) { double r1 = Ys[i].nd - Ys[i].st; double r2 = Ys[i+1].nd - Ys[i+1].st; double d = Xs[i+1] - Xs[i]; if (fabs(d) <= 0.0) continue; if (r1 <= 0.0 && r2 <= 0.0) continue; else if (r1 < 0.0 && r2 >= 0.0) { d = d * r2 / (r2 - r1); result += d * r2 / 2; } else if (r1 >= 0.0 && r2 < 0.0) { d = d * r1 / (r1 - r2); result += d * r1 / 2; } else { result += (r1 + r2) * d / 2; } // printf("%lf %lf %lf %lf\n", r1, r2, d, result); } printf("%0.15lf\n", result); }
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 | #include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define pb push_back #define mp make_pair #define st first #define nd second typedef LL Int10; typedef LL Int20; struct Point { Int10 x, y; }; Int20 operator*(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; } Int20 det(const Point& a, const Point& b) { return a.x * b.y - b.x * a.y; } Point operator-(const Point& a, const Point& b) { return {a.x - b.x, a.y - b.y}; } Point cross(const Point& a) { return {-a.y, a.x}; } struct HalfPlane { // p * a <= c Point p; Int20 c; }; ostream& operator<<(ostream& o, const HalfPlane& h) { return o << h.p.x << "X" << (h.p.y < 0 ? "" : "+") <<h.p.y<<"Y <= "<<h.c; } struct Intersection { double x; double y; }; Intersection line_intersection(const HalfPlane& a, const HalfPlane& b) { Int20 d = det(a.p, b.p); assert(d); Point p = cross({a.p.x * b.c - b.p.x * a.c, a.p.y * b.c - b.p.y * a.c}); return {p.x * 1.0 / d, p.y * 1.0 / d}; } Point towers[105][2]; const double EPS = 1e-10; int main(int argc, char* argv[]) { int N; scanf("%d", &N); REP(i,N)REP(j,2) scanf("%lld%lld", &towers[i][j].x, &towers[i][j].y); vector<HalfPlane> upper, lower; double minX = -1e3, maxX = 1e3; REP(i,N)REP(j,N) if (i != j) REP(k,2)REP(l,2) { Point a = towers[i][k], b = towers[j][l]; HalfPlane h; h.p = cross(b-a); h.c = -1e8; REP(m,N) { h.c = max(h.c, min(h.p * towers[m][0], h.p * towers[m][1])); } if (h.p.y == 0) { if (h.p.x > 0) maxX = min(maxX, h.c / (double)h.p.x); else minX = max(minX, h.c / (double)h.p.x); } else if (h.p.y > 0) upper.pb(h); else lower.pb(h); } if (minX + 1e-9 >= maxX) { printf("%0.15lf\n", 0.0); return 0; } auto compute_hull = [](vector<HalfPlane> half_planes) { sort(half_planes.begin(), half_planes.end(), [](const HalfPlane& h1, const HalfPlane& h2) { return det(h1.p, h2.p) < 0; }); half_planes.erase( unique(half_planes.begin(), half_planes.end(), [](const HalfPlane& h1, const HalfPlane& h2) { return det(h1.p, h2.p) == 0; }), half_planes.end() ); vector<HalfPlane> hull; // cout << "START " << endl; for (auto& h: half_planes) { // cout << h << endl; while (hull.size() > 1) { Intersection i = line_intersection(hull[hull.size() - 2], hull[hull.size() - 1]); if (i.x * h.p.x + i.y * h.p.y <= h.c) break; hull.pop_back(); } hull.pb(h); } return hull; }; vector<HalfPlane> upper_hull = compute_hull(upper); vector<HalfPlane> lower_hull = compute_hull(lower); reverse(lower_hull.begin(), lower_hull.end()); vector<double> Xs = {minX, maxX}; REP(i, (int)upper_hull.size() - 1) { double x = line_intersection(upper_hull[i], upper_hull[i+1]).x; if (x > minX && x < maxX) Xs.pb(x); } REP(i, (int)lower_hull.size() - 1) { double x = line_intersection(lower_hull[i], lower_hull[i+1]).x; if (x > minX && x < maxX) Xs.pb(x); } sort(Xs.begin(), Xs.end()); vector<pair<double, double>> Ys; int uix = 0, lix = 0; for (auto x: Xs) { while (uix + 1 < upper_hull.size() && line_intersection(upper_hull[uix], upper_hull[uix+1]).x < x) ++uix; while (lix + 1 < lower_hull.size() && line_intersection(lower_hull[lix], lower_hull[lix+1]).x < x) ++lix; auto at_x = [](const HalfPlane& h, const double& x) { return (h.c - h.p.x * x) / h.p.y; }; Ys.pb({at_x(lower_hull[lix], x), at_x(upper_hull[uix], x)}); } double result = 0.0; REP(i, Xs.size() - 1) { double r1 = Ys[i].nd - Ys[i].st; double r2 = Ys[i+1].nd - Ys[i+1].st; double d = Xs[i+1] - Xs[i]; if (fabs(d) <= 0.0) continue; if (r1 <= 0.0 && r2 <= 0.0) continue; else if (r1 < 0.0 && r2 >= 0.0) { d = d * r2 / (r2 - r1); result += d * r2 / 2; } else if (r1 >= 0.0 && r2 < 0.0) { d = d * r1 / (r1 - r2); result += d * r1 / 2; } else { result += (r1 + r2) * d / 2; } // printf("%lf %lf %lf %lf\n", r1, r2, d, result); } printf("%0.15lf\n", result); } |