#include <bits/stdc++.h> #define mk std::make_pair #define st first #define nd second #define ld long double #define debug if(0) struct Point{ ld x, y; Point(ld x, ld y){ this->x = x; this->y = y; } Point(){ x = 0; y = 0; } }; std::vector<Point> Z; std::vector<std::pair<Point, Point> > E; std::vector<Point> P; int n; void input(){ std::cin >> n; ld ax, ay, bx, by; for (int i = 1; i <= n; i++){ std::cin >> ax >> ay >> bx >> by; Point A(ax, ay); Point B(bx, by); E.push_back(mk(A, B)); Z.push_back(A); Z.push_back(B); } } ////////////// TUTAJ SA WSZYSTKIE FUNKCJE UTILITY //////////////////////// ld distance(const Point &p1, const Point &p2){ return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y); } ld det(Point p1, Point p2, Point p3){ ld D = p1.x*p2.y + p3.x*p1.y + p2.x*p3.y - p2.x*p1.y - p1.x*p3.y - p3.x*p2.y; return D; } ld triangleArea(Point &p1, Point &p2, Point &p3){ ld area = det(p1, p2, p3); if (area < 0) area *= (ld)(-1); area /= (ld)(2); return area; } ld polygonArea(std::vector<Point> &v){ if (v.size() <= 2) return (ld)(0); ld area = 0; for (int i = 1; i <= v.size()-3; i++) area += triangleArea(v[0], v[i], v[i+1]); return area; } Point pointOfIntersection(Point &U, Point &V, Point &X, Point &Y){ // punkt przeciecia odcinka X --> Y z prostą wyznaczona przez punkty U i V (wiem ze sie przecinaja, sprawdzilem wczesniej) if (U.x == V.x){ // plaszczyzna wyznaczona jest przez linie pionowa ld c = (X.y - Y.y)/(X.x - Y.x); ld d = (X.x*Y.y - Y.x*X.y)/(X.x-Y.x); ld x = U.x; ld y = c*x+d; Point H(x, y); return H; } else if (X.x == Y.x){ // odcinek jest pionowy ld a = (U.y - V.y)/(U.x - V.x); ld b = (U.x*V.y - V.x*U.y)/(U.x-V.x); ld x = X.x; ld y = a*x+b; Point H(x, y); return H; } else{ // nic nie jest pionowe ld a = (U.y - V.y)/(U.x - V.x); ld b = (U.x*V.y - V.x*U.y)/(U.x-V.x); ld c = (X.y - Y.y)/(X.x - Y.x); ld d = (X.x*Y.y - Y.x*X.y)/(X.x-Y.x); ld x = (d-b)/(a-c); ld y = a*x+b; Point H(x, y); return H; } } /////////////////////////////////////////////////////////////////////////// bool exists(Point &U, Point &V){ // zwraca true wtedy i tylko wtedy gdy po prawej stronie znajduje sie jakis odcinkek for (auto p: E){ ld d1 = det(U, V, p.st); ld d2 = det(U, V, p.nd); if (d1 < 0 && d2 < 0) return true; } return false; } void findIntersection(Point &U, Point &V){ // polplaszczyzna wyznaczona przez wektor U --> V, chce zrobic czesc wspolna z jej lewą stroną if (P.empty()) return; std::vector<Point> tmp; for (int i = 0; i < P.size()-1; i++){ Point X = P[i]; Point Y = P[i+1]; ld dt = det(U, V, X); ld dh = det(U, V, Y); if (dt <= 0.0000000001 && dt >= -0.0000000001) dt = 0; if (dh <= 0.0000000001 && dh >= -0.0000000001) dh = 0; if (dt < 0 && dh > 0){ // wchodzi Point H = pointOfIntersection(U, V, X, Y); tmp.push_back(H); tmp.push_back(Y); } else if (dt > 0 && dh < 0){ // wychodzi Point H = pointOfIntersection(U, V, X, Y); tmp.push_back(H); } else if (dt > 0 && dh > 0){ // 1 tmp.push_back(Y); } else if (dt < 0 && dh < 0){ // 2 continue; } else if (dt == 0 && dh == 0){ // 1 tmp.push_back(Y); } else if (dt < 0 && dh == 0){ // 1t tmp.push_back(Y); } else if (dt == 0 && dh > 0){ // 1 tmp.push_back(Y); } else if (dt > 0 && dh == 0){ // 1 tmp.push_back(Y); } else if (dt == 0 && dh < 0){ // 2 continue; } } P = tmp; if (P.size() > 0) P.push_back(P.front()); } void findArea(){ for (int i = 0; i < Z.size(); i++){ for (int j = 0; j < Z.size(); j++){ if (i == j) continue; if (!exists(Z[i], Z[j])) findIntersection(Z[i], Z[j]); } } } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); P.emplace_back(-500, -500); P.emplace_back(500, -500); P.emplace_back(500, 500); P.emplace_back(-500, 500); P.emplace_back(-500, -500); findArea(); ld area = polygonArea(P); std::cout << std::setprecision(16) << std::fixed << area << "\n"; }
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 | #include <bits/stdc++.h> #define mk std::make_pair #define st first #define nd second #define ld long double #define debug if(0) struct Point{ ld x, y; Point(ld x, ld y){ this->x = x; this->y = y; } Point(){ x = 0; y = 0; } }; std::vector<Point> Z; std::vector<std::pair<Point, Point> > E; std::vector<Point> P; int n; void input(){ std::cin >> n; ld ax, ay, bx, by; for (int i = 1; i <= n; i++){ std::cin >> ax >> ay >> bx >> by; Point A(ax, ay); Point B(bx, by); E.push_back(mk(A, B)); Z.push_back(A); Z.push_back(B); } } ////////////// TUTAJ SA WSZYSTKIE FUNKCJE UTILITY //////////////////////// ld distance(const Point &p1, const Point &p2){ return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y); } ld det(Point p1, Point p2, Point p3){ ld D = p1.x*p2.y + p3.x*p1.y + p2.x*p3.y - p2.x*p1.y - p1.x*p3.y - p3.x*p2.y; return D; } ld triangleArea(Point &p1, Point &p2, Point &p3){ ld area = det(p1, p2, p3); if (area < 0) area *= (ld)(-1); area /= (ld)(2); return area; } ld polygonArea(std::vector<Point> &v){ if (v.size() <= 2) return (ld)(0); ld area = 0; for (int i = 1; i <= v.size()-3; i++) area += triangleArea(v[0], v[i], v[i+1]); return area; } Point pointOfIntersection(Point &U, Point &V, Point &X, Point &Y){ // punkt przeciecia odcinka X --> Y z prostą wyznaczona przez punkty U i V (wiem ze sie przecinaja, sprawdzilem wczesniej) if (U.x == V.x){ // plaszczyzna wyznaczona jest przez linie pionowa ld c = (X.y - Y.y)/(X.x - Y.x); ld d = (X.x*Y.y - Y.x*X.y)/(X.x-Y.x); ld x = U.x; ld y = c*x+d; Point H(x, y); return H; } else if (X.x == Y.x){ // odcinek jest pionowy ld a = (U.y - V.y)/(U.x - V.x); ld b = (U.x*V.y - V.x*U.y)/(U.x-V.x); ld x = X.x; ld y = a*x+b; Point H(x, y); return H; } else{ // nic nie jest pionowe ld a = (U.y - V.y)/(U.x - V.x); ld b = (U.x*V.y - V.x*U.y)/(U.x-V.x); ld c = (X.y - Y.y)/(X.x - Y.x); ld d = (X.x*Y.y - Y.x*X.y)/(X.x-Y.x); ld x = (d-b)/(a-c); ld y = a*x+b; Point H(x, y); return H; } } /////////////////////////////////////////////////////////////////////////// bool exists(Point &U, Point &V){ // zwraca true wtedy i tylko wtedy gdy po prawej stronie znajduje sie jakis odcinkek for (auto p: E){ ld d1 = det(U, V, p.st); ld d2 = det(U, V, p.nd); if (d1 < 0 && d2 < 0) return true; } return false; } void findIntersection(Point &U, Point &V){ // polplaszczyzna wyznaczona przez wektor U --> V, chce zrobic czesc wspolna z jej lewą stroną if (P.empty()) return; std::vector<Point> tmp; for (int i = 0; i < P.size()-1; i++){ Point X = P[i]; Point Y = P[i+1]; ld dt = det(U, V, X); ld dh = det(U, V, Y); if (dt <= 0.0000000001 && dt >= -0.0000000001) dt = 0; if (dh <= 0.0000000001 && dh >= -0.0000000001) dh = 0; if (dt < 0 && dh > 0){ // wchodzi Point H = pointOfIntersection(U, V, X, Y); tmp.push_back(H); tmp.push_back(Y); } else if (dt > 0 && dh < 0){ // wychodzi Point H = pointOfIntersection(U, V, X, Y); tmp.push_back(H); } else if (dt > 0 && dh > 0){ // 1 tmp.push_back(Y); } else if (dt < 0 && dh < 0){ // 2 continue; } else if (dt == 0 && dh == 0){ // 1 tmp.push_back(Y); } else if (dt < 0 && dh == 0){ // 1t tmp.push_back(Y); } else if (dt == 0 && dh > 0){ // 1 tmp.push_back(Y); } else if (dt > 0 && dh == 0){ // 1 tmp.push_back(Y); } else if (dt == 0 && dh < 0){ // 2 continue; } } P = tmp; if (P.size() > 0) P.push_back(P.front()); } void findArea(){ for (int i = 0; i < Z.size(); i++){ for (int j = 0; j < Z.size(); j++){ if (i == j) continue; if (!exists(Z[i], Z[j])) findIntersection(Z[i], Z[j]); } } } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); P.emplace_back(-500, -500); P.emplace_back(500, -500); P.emplace_back(500, 500); P.emplace_back(-500, 500); P.emplace_back(-500, -500); findArea(); ld area = polygonArea(P); std::cout << std::setprecision(16) << std::fixed << area << "\n"; } |