#include <cstdio> #include <vector> #include <algorithm> const int max_n = 200; const double eps = 1e-12; const int max_m = 2000000; double eq(double x, double y) { double diff = x - y; if (diff < 0) diff = -diff; return diff < eps; } double lt(double x, double y) { return x + eps < y; } struct Point { double x, y; bool operator<(const Point& other) const { return lt(x, other.x) || eq(x, other.x) && lt(y, other.y); } }; int n, top; Point A[max_n]; Point B[max_n]; int stack[max_m]; std::vector<Point> good_points; std::vector<Point> points; std::vector<Point> convex_hull; double vec(double v_x, double v_y, double u_x, double u_y) { return v_x * u_x + v_y * u_y; } double scal(double v_x, double v_y, double u_x, double u_y) { return v_x * u_y - v_y * u_x; } struct Direction { Direction(double _x, double _y, bool _o) : vx(_x), vy(_y), open(_o) { if (eq(vx, 0)) { if (vy > -eps) quarter = 1, angle = 0.0; else quarter = 3, angle = 0.0; } else if (vx > eps) { if (eq(vy, 0.0)) quarter = 0, angle = 0.0; else if (vy > eps) quarter = 0, angle = vy / vx; else quarter = 3, angle = -vx / vy; } else { if (eq(vy, 0.0)) quarter = 2, angle = 0.0; else if (vy > eps) quarter = 1, angle = -vx / vy; else quarter = 2, angle = vy / vx; } } double vx, vy; bool open; int quarter; double angle; bool operator<(const Direction& other) const { return quarter < other.quarter || quarter == other.quarter && lt(angle, other.angle) || quarter == other.quarter && eq(angle, other.angle) && open && !other.open; } }; std::vector<Direction> dirs; bool inside(double open_x, double open_y, double close_x, double close_y) { // 1.0 , 0.0 vector double sco = scal(open_x, open_y, 1.0, 0.0); if (sco < -eps) return false; if (eq(sco, 0.0) && vec(open_x, open_y, 1.0, 0.0) > eps) return false; double scc = scal(1.0, 0.0, close_x, close_y); if (scc < -eps) return false; if (eq(scc, 0.0) && vec(close_x, close_y, 1.0, 0.0) < -eps) return false; return true; } bool is_good(Point p) { // printf("CAN=%.4lf %.4lf\n", p.x, p.y); dirs.clear(); int start_bilans = 0; for (int i = 0; i < n; ++i) { double v_x = A[i].x - p.x; double v_y = A[i].y - p.y; double lv_x = -v_y; double lv_y = v_x; double rv_x = v_y; double rv_y = -v_x; double u_x = B[i].x - p.x; double u_y = B[i].y - p.y; double lu_x = -u_y; double lu_y = u_x; double ru_x = u_y; double ru_y = -u_x; // printf("==%.5lf %.5lf %.5lf %.5lf\n", lv_x, lv_y, rv_x, rv_y); // printf("==%.5lf %.5lf %.5lf %.5lf\n", lu_x, lu_y, ru_x, ru_y); if (eq(u_x, 0.0) && eq(u_y, 0.0)) { Direction open(rv_x, rv_y, true); dirs.push_back(open); Direction close(lv_x, lv_y, false); dirs.push_back(close); // printf("1. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; continue; } if (eq(v_x, 0.0) && eq(v_y, 0.0)) { Direction open(ru_x, ru_y, true); dirs.push_back(open); Direction close(lu_x, lu_y, false); dirs.push_back(close); // printf("2. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; continue; } if (scal(rv_x, rv_y, lu_x, lu_y) > 0.0) { Direction open(rv_x, rv_y, true); dirs.push_back(open); Direction close(lu_x, lu_y, false); dirs.push_back(close); // printf("3. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; } else { Direction open(ru_x, ru_y, true); dirs.push_back(open); Direction close(lv_x, lv_y, false); dirs.push_back(close); // printf("4. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; } } std::sort(dirs.begin(), dirs.end()); for (int i = 0; i < dirs.size(); ++i) { // printf("$ %.4lf %.4lf %d %d %.4lf\n", dirs[i].vx, dirs[i].vy, dirs[i].open, dirs[i].quarter, dirs[i].angle); if (dirs[i].open) ++start_bilans; else --start_bilans; if (start_bilans == 0) return false; } return true; } bool find_intersection(const Point& x_1, const Point& x_2, const Point& y_1, const Point& y_2, Point& result) { double A_1 = x_2.y - x_1.y; double B_1 = x_1.x - x_2.x; double C_1 = A_1 * x_1.x + B_1 * x_1.y; double A_2 = y_2.y - y_1.y; double B_2 = y_1.x - y_2.x; double C_2 = A_2 * y_1.x + B_2 * y_1.y; double det = A_1 * B_2 - A_2 * B_1; if (eq(det, 0)) return false; result.x = (B_2 * C_1 - B_1 * C_2) / det; result.y = (A_1 * C_2 - A_2 * C_1) / det; return true; if (lt(x_1.x, x_2.x)) { return x_1.x < result.x && result.x < x_2.x; } if (lt(x_2.x, x_1.x)) { return x_2.x < result.x && result.x < x_1.x; } if (lt(x_1.y, x_2.y)) { return x_1.y < result.y && result.y < x_2.y; } return x_2.y < result.y && result.y < x_1.y; } double skret(Point a, Point b, Point c) { return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); } int main() { scanf("%d", &n); std::vector<Point> pp; for (int i = 0; i < n; ++i) { scanf("%lf %lf %lf %lf", &A[i].x, &A[i].y, &B[i].x, &B[i].y); pp.push_back(A[i]); pp.push_back(B[i]); } /* for (double x = -20.0; x < 20.0; x += 0.0001) { for (double y = -20.0; y < 20.0; y += 0.0001) { Point p; p.x = x; p.y = y; if (is_good(p)) { printf("good at %.5lf %.5lf\n", x, y); } } } return 0;*/ for (int i = 0; i < pp.size(); ++i) { for (int j = i + 1; j < pp.size(); ++j) { for (int k = i + 1; k < pp.size(); ++k) { for (int r = k + 1; r < pp.size(); ++r) { Point intersect; if (find_intersection(pp[i], pp[j], pp[k], pp[r], intersect) && is_good(intersect)) good_points.push_back(intersect); } } } } /* for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j == i) continue; Point intersect; if (find_intersection(A[i], B[i], A[j], B[j], intersect) && is_good(intersect)) good_points.push_back(intersect); for (int k = 0; k < n; ++k) { if (k == i || k == j) continue; if (find_intersection(A[i], A[j], B[i], A[k], intersect) && is_good(intersect)) good_points.push_back(intersect); if (find_intersection(A[i], A[j], B[i], B[k], intersect) && is_good(intersect)) good_points.push_back(intersect); if (find_intersection(A[i], B[j], B[i], A[k], intersect) && is_good(intersect)) good_points.push_back(intersect); if (find_intersection(A[i], B[j], B[i], B[k], intersect) && is_good(intersect)) good_points.push_back(intersect); } } } */ std::sort(good_points.begin(), good_points.end()); for (int i = 0; i < good_points.size(); ++i) { if (i == 0 || !eq(good_points[i].x, good_points[i - 1].x) || !eq(good_points[i].y, good_points[i - 1].y)) { points.push_back(good_points[i]); } } /* for (int i = 0; i < points.size(); ++i) { printf("G: %.4lf %.4lf\n", points[i].x, points[i].y); } */ top = 0; for (int i = 0; i < points.size(); ++i) { while (top > 1 && skret(points[i], points[stack[top - 1]], points[stack[top - 2]]) > eps) { --top; } stack[top++] = i; } for (int i = 0; i < top; ++i) { convex_hull.push_back(points[stack[i]]); } top = 0; for (int i = points.size(); i > 0; --i) { while (top > 1 && skret(points[i - 1], points[stack[top - 1]], points[stack[top - 2]]) > eps) { --top; } stack[top++] = i - 1; } for (int i = 0; i < top; ++i) { convex_hull.push_back(points[stack[i]]); } double result = 0.0; for (int i = 0; i + 1 < convex_hull.size(); ++i) { // printf("X: %lf %lf\n", convex_hull[i].x, convex_hull[i].y); result += convex_hull[i].x * convex_hull[i + 1].y - convex_hull[i].y * convex_hull[i + 1].x; } result /= 2.0; printf("%.12lf\n", result); 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 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | #include <cstdio> #include <vector> #include <algorithm> const int max_n = 200; const double eps = 1e-12; const int max_m = 2000000; double eq(double x, double y) { double diff = x - y; if (diff < 0) diff = -diff; return diff < eps; } double lt(double x, double y) { return x + eps < y; } struct Point { double x, y; bool operator<(const Point& other) const { return lt(x, other.x) || eq(x, other.x) && lt(y, other.y); } }; int n, top; Point A[max_n]; Point B[max_n]; int stack[max_m]; std::vector<Point> good_points; std::vector<Point> points; std::vector<Point> convex_hull; double vec(double v_x, double v_y, double u_x, double u_y) { return v_x * u_x + v_y * u_y; } double scal(double v_x, double v_y, double u_x, double u_y) { return v_x * u_y - v_y * u_x; } struct Direction { Direction(double _x, double _y, bool _o) : vx(_x), vy(_y), open(_o) { if (eq(vx, 0)) { if (vy > -eps) quarter = 1, angle = 0.0; else quarter = 3, angle = 0.0; } else if (vx > eps) { if (eq(vy, 0.0)) quarter = 0, angle = 0.0; else if (vy > eps) quarter = 0, angle = vy / vx; else quarter = 3, angle = -vx / vy; } else { if (eq(vy, 0.0)) quarter = 2, angle = 0.0; else if (vy > eps) quarter = 1, angle = -vx / vy; else quarter = 2, angle = vy / vx; } } double vx, vy; bool open; int quarter; double angle; bool operator<(const Direction& other) const { return quarter < other.quarter || quarter == other.quarter && lt(angle, other.angle) || quarter == other.quarter && eq(angle, other.angle) && open && !other.open; } }; std::vector<Direction> dirs; bool inside(double open_x, double open_y, double close_x, double close_y) { // 1.0 , 0.0 vector double sco = scal(open_x, open_y, 1.0, 0.0); if (sco < -eps) return false; if (eq(sco, 0.0) && vec(open_x, open_y, 1.0, 0.0) > eps) return false; double scc = scal(1.0, 0.0, close_x, close_y); if (scc < -eps) return false; if (eq(scc, 0.0) && vec(close_x, close_y, 1.0, 0.0) < -eps) return false; return true; } bool is_good(Point p) { // printf("CAN=%.4lf %.4lf\n", p.x, p.y); dirs.clear(); int start_bilans = 0; for (int i = 0; i < n; ++i) { double v_x = A[i].x - p.x; double v_y = A[i].y - p.y; double lv_x = -v_y; double lv_y = v_x; double rv_x = v_y; double rv_y = -v_x; double u_x = B[i].x - p.x; double u_y = B[i].y - p.y; double lu_x = -u_y; double lu_y = u_x; double ru_x = u_y; double ru_y = -u_x; // printf("==%.5lf %.5lf %.5lf %.5lf\n", lv_x, lv_y, rv_x, rv_y); // printf("==%.5lf %.5lf %.5lf %.5lf\n", lu_x, lu_y, ru_x, ru_y); if (eq(u_x, 0.0) && eq(u_y, 0.0)) { Direction open(rv_x, rv_y, true); dirs.push_back(open); Direction close(lv_x, lv_y, false); dirs.push_back(close); // printf("1. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; continue; } if (eq(v_x, 0.0) && eq(v_y, 0.0)) { Direction open(ru_x, ru_y, true); dirs.push_back(open); Direction close(lu_x, lu_y, false); dirs.push_back(close); // printf("2. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; continue; } if (scal(rv_x, rv_y, lu_x, lu_y) > 0.0) { Direction open(rv_x, rv_y, true); dirs.push_back(open); Direction close(lu_x, lu_y, false); dirs.push_back(close); // printf("3. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; } else { Direction open(ru_x, ru_y, true); dirs.push_back(open); Direction close(lv_x, lv_y, false); dirs.push_back(close); // printf("4. %.5lf %.5lf %.5lf %.5lf\n", open.vx, open.vy, close.vx, close.vy); if (inside(open.vx, open.vy, close.vx, close.vy)) ++start_bilans; } } std::sort(dirs.begin(), dirs.end()); for (int i = 0; i < dirs.size(); ++i) { // printf("$ %.4lf %.4lf %d %d %.4lf\n", dirs[i].vx, dirs[i].vy, dirs[i].open, dirs[i].quarter, dirs[i].angle); if (dirs[i].open) ++start_bilans; else --start_bilans; if (start_bilans == 0) return false; } return true; } bool find_intersection(const Point& x_1, const Point& x_2, const Point& y_1, const Point& y_2, Point& result) { double A_1 = x_2.y - x_1.y; double B_1 = x_1.x - x_2.x; double C_1 = A_1 * x_1.x + B_1 * x_1.y; double A_2 = y_2.y - y_1.y; double B_2 = y_1.x - y_2.x; double C_2 = A_2 * y_1.x + B_2 * y_1.y; double det = A_1 * B_2 - A_2 * B_1; if (eq(det, 0)) return false; result.x = (B_2 * C_1 - B_1 * C_2) / det; result.y = (A_1 * C_2 - A_2 * C_1) / det; return true; if (lt(x_1.x, x_2.x)) { return x_1.x < result.x && result.x < x_2.x; } if (lt(x_2.x, x_1.x)) { return x_2.x < result.x && result.x < x_1.x; } if (lt(x_1.y, x_2.y)) { return x_1.y < result.y && result.y < x_2.y; } return x_2.y < result.y && result.y < x_1.y; } double skret(Point a, Point b, Point c) { return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); } int main() { scanf("%d", &n); std::vector<Point> pp; for (int i = 0; i < n; ++i) { scanf("%lf %lf %lf %lf", &A[i].x, &A[i].y, &B[i].x, &B[i].y); pp.push_back(A[i]); pp.push_back(B[i]); } /* for (double x = -20.0; x < 20.0; x += 0.0001) { for (double y = -20.0; y < 20.0; y += 0.0001) { Point p; p.x = x; p.y = y; if (is_good(p)) { printf("good at %.5lf %.5lf\n", x, y); } } } return 0;*/ for (int i = 0; i < pp.size(); ++i) { for (int j = i + 1; j < pp.size(); ++j) { for (int k = i + 1; k < pp.size(); ++k) { for (int r = k + 1; r < pp.size(); ++r) { Point intersect; if (find_intersection(pp[i], pp[j], pp[k], pp[r], intersect) && is_good(intersect)) good_points.push_back(intersect); } } } } /* for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j == i) continue; Point intersect; if (find_intersection(A[i], B[i], A[j], B[j], intersect) && is_good(intersect)) good_points.push_back(intersect); for (int k = 0; k < n; ++k) { if (k == i || k == j) continue; if (find_intersection(A[i], A[j], B[i], A[k], intersect) && is_good(intersect)) good_points.push_back(intersect); if (find_intersection(A[i], A[j], B[i], B[k], intersect) && is_good(intersect)) good_points.push_back(intersect); if (find_intersection(A[i], B[j], B[i], A[k], intersect) && is_good(intersect)) good_points.push_back(intersect); if (find_intersection(A[i], B[j], B[i], B[k], intersect) && is_good(intersect)) good_points.push_back(intersect); } } } */ std::sort(good_points.begin(), good_points.end()); for (int i = 0; i < good_points.size(); ++i) { if (i == 0 || !eq(good_points[i].x, good_points[i - 1].x) || !eq(good_points[i].y, good_points[i - 1].y)) { points.push_back(good_points[i]); } } /* for (int i = 0; i < points.size(); ++i) { printf("G: %.4lf %.4lf\n", points[i].x, points[i].y); } */ top = 0; for (int i = 0; i < points.size(); ++i) { while (top > 1 && skret(points[i], points[stack[top - 1]], points[stack[top - 2]]) > eps) { --top; } stack[top++] = i; } for (int i = 0; i < top; ++i) { convex_hull.push_back(points[stack[i]]); } top = 0; for (int i = points.size(); i > 0; --i) { while (top > 1 && skret(points[i - 1], points[stack[top - 1]], points[stack[top - 2]]) > eps) { --top; } stack[top++] = i - 1; } for (int i = 0; i < top; ++i) { convex_hull.push_back(points[stack[i]]); } double result = 0.0; for (int i = 0; i + 1 < convex_hull.size(); ++i) { // printf("X: %lf %lf\n", convex_hull[i].x, convex_hull[i].y); result += convex_hull[i].x * convex_hull[i + 1].y - convex_hull[i].y * convex_hull[i + 1].x; } result /= 2.0; printf("%.12lf\n", result); return 0; } |