#include <cstdio> #include <algorithm> #include <vector> #include <cmath> typedef long long LL; typedef long double LD; struct vec2 { LD x, y; }; struct ivec2 { int x, y; }; ivec2 operator-(ivec2 u, ivec2 v) { return ivec2{u.x-v.x, u.y-v.y}; } LD det(vec2 u, vec2 v) { return u.x*v.y - u.y*v.x; } int det(ivec2 u, ivec2 v) { return u.x*v.y - u.y*v.x; } // Ax + By + C > 0 struct halfplane { int A, B, C; //u -> v z prawej halfplane(ivec2 u, ivec2 v) { ivec2 w = u-v; A = w.y; B = -w.x; C = - A*u.x - B*u.y; } }; vec2 intersection(halfplane h1, halfplane h2) { LD d = det(ivec2{h1.A, h2.A}, ivec2{h1.B, h2.B}); LD x = LD(h2.B)*h1.C - LD(h1.B)*h2.C; LD y = LD(h1.A)*h2.C - LD(h2.A)*h1.C; return vec2{-x/d, -y/d}; } bool inside(halfplane h, vec2 v) { return h.A*v.x + h.B*v.y + h.C > 0; } void radial_sort(std::vector<halfplane>& hp) { auto mid = std::partition(hp.begin(), hp.end(), [](halfplane h) { if(h.B != 0) return h.B < 0; return h.A > 0; }); auto srt_cmp = [](halfplane h1, halfplane h2) { int d = det(ivec2{h1.A, h2.A}, ivec2{h1.B, h2.B}); if(d != 0) return d < 0; LD a1 = sqrt(h1.A*h1.A + h1.B*h1.B); LD a2 = sqrt(h2.A*h2.A + h2.B*h2.B); return h1.C/a1 < h2.C/a2; }; std::sort(hp.begin(), mid, srt_cmp); std::sort(mid, hp.end(), srt_cmp); auto new_end = std::unique(hp.begin(), hp.end(), [](halfplane h1, halfplane h2) { return det(ivec2{h1.A, h2.A}, ivec2{h1.B, h2.B}) == 0; }); hp.erase(new_end, hp.end()); } LD area(std::vector<halfplane> hp) { radial_sort(hp); std::vector<std::pair<vec2, int>> stack = {{vec2{0, 0}, 0}}; int i = 1; for(; det(ivec2{hp[0].A, hp[0].B}, ivec2{hp[i].A, hp[i].B}) <= 0; i++) { while(stack.size() > 1 && !inside(hp[i], stack.back().first)) stack.pop_back(); stack.emplace_back(intersection(hp[stack.back().second], hp[i]), i); } auto beg = 0; bool at_inf = true; for(; i < int(hp.size()); i++) { if(!at_inf && inside(hp[i], stack[beg].first)) continue; at_inf = false; while(beg+1 < int(stack.size()) && !inside(hp[i], stack[beg+1].first)) beg++; if(beg+1 == int(stack.size())) return 0; stack[beg].first = intersection(hp[i], hp[stack[beg].second]); while(!inside(hp[i], stack.back().first)) stack.pop_back(); stack.emplace_back(intersection(hp[i], hp[stack.back().second]), i); } LD area = 0.0; for(int j = beg+1; j < int(stack.size()); j++) area += det(stack[j].first, stack[j-1].first); area += det(stack[beg].first, stack.back().first); return area * 0.5; } ivec2 points[2][100]; int main() { int n; scanf("%i", &n); for(int i = 0; i < n; i++) scanf("%i%i%i%i", &points[0][i].x, &points[0][i].y, &points[1][i].x, &points[1][i].y); std::vector<halfplane> hp; for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) { for(int ind1 = 0; ind1 < 2; ind1++) for(int ind2 = 0; ind2 < 2; ind2++) { ivec2 u = points[ind1][i]; ivec2 v = points[ind2][j]; bool allleft = true; bool allright = true; for(int k = 0; k < n; k++) { if(k == i || k == j) continue; int d0 = det(v-u, points[0][k]-u); int d1 = det(v-u, points[1][k]-u); if(d0 < 0 && d1 < 0) allright = false; if(d0 > 0 && d1 > 0) allleft = false; } if(allright) hp.emplace_back(u, v); if(allleft) hp.emplace_back(v, u); } } printf("%.13Lf\n", area(hp)); }
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 | #include <cstdio> #include <algorithm> #include <vector> #include <cmath> typedef long long LL; typedef long double LD; struct vec2 { LD x, y; }; struct ivec2 { int x, y; }; ivec2 operator-(ivec2 u, ivec2 v) { return ivec2{u.x-v.x, u.y-v.y}; } LD det(vec2 u, vec2 v) { return u.x*v.y - u.y*v.x; } int det(ivec2 u, ivec2 v) { return u.x*v.y - u.y*v.x; } // Ax + By + C > 0 struct halfplane { int A, B, C; //u -> v z prawej halfplane(ivec2 u, ivec2 v) { ivec2 w = u-v; A = w.y; B = -w.x; C = - A*u.x - B*u.y; } }; vec2 intersection(halfplane h1, halfplane h2) { LD d = det(ivec2{h1.A, h2.A}, ivec2{h1.B, h2.B}); LD x = LD(h2.B)*h1.C - LD(h1.B)*h2.C; LD y = LD(h1.A)*h2.C - LD(h2.A)*h1.C; return vec2{-x/d, -y/d}; } bool inside(halfplane h, vec2 v) { return h.A*v.x + h.B*v.y + h.C > 0; } void radial_sort(std::vector<halfplane>& hp) { auto mid = std::partition(hp.begin(), hp.end(), [](halfplane h) { if(h.B != 0) return h.B < 0; return h.A > 0; }); auto srt_cmp = [](halfplane h1, halfplane h2) { int d = det(ivec2{h1.A, h2.A}, ivec2{h1.B, h2.B}); if(d != 0) return d < 0; LD a1 = sqrt(h1.A*h1.A + h1.B*h1.B); LD a2 = sqrt(h2.A*h2.A + h2.B*h2.B); return h1.C/a1 < h2.C/a2; }; std::sort(hp.begin(), mid, srt_cmp); std::sort(mid, hp.end(), srt_cmp); auto new_end = std::unique(hp.begin(), hp.end(), [](halfplane h1, halfplane h2) { return det(ivec2{h1.A, h2.A}, ivec2{h1.B, h2.B}) == 0; }); hp.erase(new_end, hp.end()); } LD area(std::vector<halfplane> hp) { radial_sort(hp); std::vector<std::pair<vec2, int>> stack = {{vec2{0, 0}, 0}}; int i = 1; for(; det(ivec2{hp[0].A, hp[0].B}, ivec2{hp[i].A, hp[i].B}) <= 0; i++) { while(stack.size() > 1 && !inside(hp[i], stack.back().first)) stack.pop_back(); stack.emplace_back(intersection(hp[stack.back().second], hp[i]), i); } auto beg = 0; bool at_inf = true; for(; i < int(hp.size()); i++) { if(!at_inf && inside(hp[i], stack[beg].first)) continue; at_inf = false; while(beg+1 < int(stack.size()) && !inside(hp[i], stack[beg+1].first)) beg++; if(beg+1 == int(stack.size())) return 0; stack[beg].first = intersection(hp[i], hp[stack[beg].second]); while(!inside(hp[i], stack.back().first)) stack.pop_back(); stack.emplace_back(intersection(hp[i], hp[stack.back().second]), i); } LD area = 0.0; for(int j = beg+1; j < int(stack.size()); j++) area += det(stack[j].first, stack[j-1].first); area += det(stack[beg].first, stack.back().first); return area * 0.5; } ivec2 points[2][100]; int main() { int n; scanf("%i", &n); for(int i = 0; i < n; i++) scanf("%i%i%i%i", &points[0][i].x, &points[0][i].y, &points[1][i].x, &points[1][i].y); std::vector<halfplane> hp; for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) { for(int ind1 = 0; ind1 < 2; ind1++) for(int ind2 = 0; ind2 < 2; ind2++) { ivec2 u = points[ind1][i]; ivec2 v = points[ind2][j]; bool allleft = true; bool allright = true; for(int k = 0; k < n; k++) { if(k == i || k == j) continue; int d0 = det(v-u, points[0][k]-u); int d1 = det(v-u, points[1][k]-u); if(d0 < 0 && d1 < 0) allright = false; if(d0 > 0 && d1 > 0) allleft = false; } if(allright) hp.emplace_back(u, v); if(allleft) hp.emplace_back(v, u); } } printf("%.13Lf\n", area(hp)); } |