#include <bits/stdc++.h> using namespace std; int N; const long double ERR = 0.0000000001; struct czworka{ int x1; int y1; int x2; int y2; bool prawo; // czy na prawo od wektora P1 -> P2 }; struct wielokat{ vector<long double> x; vector<long double> y; // ostatni punkt jest powtorzony int S; }; int ind1, ind2; vector<czworka> Czar; vector<czworka> X; long double xp, yp; short wp; bool blisko(long double a, long double b){ if(abs(a - b) < ERR){ return true; } return false; } int det(int x1, int y1, int x2, int y2, int x3, int y3){ int k = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1; if(k > 0){ k = 1; } else if(k < 0){ k = -1; } // det = 1 oznacza, ze P3 jest na LEWO od P1 -> P2 return k; } int det2(long double x1, long double y1, long double x2, long double y2, long double x3, long double y3){ long double k = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1; if(blisko(k, 0)){ return 0; } else if(k > 0){ return 1; } return -1; // det = 1 oznacza, ze P3 jest na LEWO od P1 -> P2 } bool nowa_prosta(int x1, int y1, int x2, int y2){ int det1, det2; bool czy1 = false; bool czy2 = false; for(int i = 0; i < N; i++){ if((i != ind1) && (i != ind2)){ det1 = det(x1, y1, x2, y2, Czar[i].x1, Czar[i].y1); det2 = det(x1, y1, x2, y2, Czar[i].x2, Czar[i].y2); if(det1 * det2 == 1){ if(det1 == 1){ czy1 = true; if(czy2){ break; } } else { czy2 = true; if(czy1){ break; } } } } } // czy1 jest na lewo // czy2 jest na prawo czworka nowe; nowe.x1 = x1; nowe.y1 = y1; nowe.x2 = x2; nowe.y2 = y2; if(!czy1){ if(!czy2){ return true; } nowe.prawo = false; X.push_back(nowe); } if(!czy2){ nowe.prawo = true; X.push_back(nowe); } return false; } void proste_dwie(long double x1, long double y1, long double x2, long double y2, long double x3, long double y3, long double x4, long double y4){ if(blisko(x1, x2)){ long double a2 = (y4 - y3)/(x4 - x3); long double b2 = y3 - a2 * x3; xp = x1; yp = a2 * xp + b2; } else { if(blisko(x3, x4)){ long double a1 = (y2 - y1)/(x2 - x1); long double b1 = y1 - a1 * x1; xp = x3; yp = a1 * xp + b1; } else { long double a1 = (y2 - y1)/(x2 - x1); long double b1 = y1 - a1 * x1; long double a2 = (y4 - y3)/(x4 - x3); long double b2 = y3 - a2 * x3; xp = (b1 - b2)/(a2 - a1); yp = a1 * xp + b1; } } } wielokat przetnij(wielokat W, long double x1, long double y1, long double x2, long double y2){ wielokat Q; Q.S = 0; long double d1, d2; d1 = det2(x1, y1, x2, y2, W.x[0], W.y[0]); d2 = det2(x1, y1, x2, y2, W.x[1], W.y[1]); for(int j = 0; j < W.S - 1; j++){ if((d1 < 0) && (d2 < 0)){ // wtedy NIE } else if(d1 < 0){ proste_dwie(x1, y1, x2, y2, W.x[j], W.y[j], W.x[j + 1], W.y[j + 1]); Q.x.push_back(xp); Q.y.push_back(yp); Q.S++; if(d2 >= 0){ Q.x.push_back(W.x[j + 1]); Q.y.push_back(W.y[j + 1]); Q.S++; } } else if(d2 < 0){ if(d1 >= 0){ proste_dwie(x1, y1, x2, y2, W.x[j], W.y[j], W.x[j + 1], W.y[j + 1]); Q.x.push_back(xp); Q.y.push_back(yp); Q.S++; } } else { Q.x.push_back(W.x[j + 1]); Q.y.push_back(W.y[j + 1]); Q.S++; } d1 = d2; if(j != W.S - 2){ d2 = det2(x1, y1, x2, y2, W.x[j + 2], W.y[j + 2]); } } if(Q.S > 0){ Q.x.push_back(Q.x[0]); Q.y.push_back(Q.y[0]); Q.S++; } return Q; } long double pole(wielokat W){ if(W.S < 4){ return 0; } long double x1 = W.x[0]; long double y1 = W.y[0]; long double P = 0; for(int i = 1; i < W.S - 2; i++){ long double x2 = W.x[i]; long double y2 = W.y[i]; long double x3 = W.x[i + 1]; long double y3 = W.y[i + 1]; long double PT = abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)); P += PT / 2; } return P; } int main(){ cin >> N; czworka nowe; for(int i = 0; i < N; i++){ cin >> nowe.x1 >> nowe.y1 >> nowe.x2 >> nowe.y2; Czar.push_back(nowe); } bool KONIEC = false; for(int i = 0; i < N - 1; i++){ for(int j = i + 1; j < N; j++){ ind1 = i; ind2 = j; if((nowa_prosta(Czar[i].x1, Czar[i].y1, Czar[j].x1, Czar[j].y1)) || (nowa_prosta(Czar[i].x2, Czar[i].y2, Czar[j].x1, Czar[j].y1)) || (nowa_prosta(Czar[i].x1, Czar[i].y1, Czar[j].x2, Czar[j].y2)) || (nowa_prosta(Czar[i].x2, Czar[i].y2, Czar[j].x2, Czar[j].y2))){ KONIEC = true; break; } } if(KONIEC) break; } if(KONIEC){ cout << setprecision(19) << fixed << (long double)(0.0) << endl; } else { wielokat W; W.x.push_back(-1000); W.x.push_back(-1000); W.x.push_back(1000); W.x.push_back(1000); W.x.push_back(-1000); W.y.push_back(-1000); W.y.push_back(1000); W.y.push_back(1000); W.y.push_back(-1000); W.y.push_back(-1000); W.S = 5; for(int i = 0; i < X.size(); i++){ if(X[i].prawo){ W = przetnij(W, X[i].x1, X[i].y1, X[i].x2, X[i].y2); } else { W = przetnij(W, X[i].x2, X[i].y2, X[i].x1, X[i].y1); } if(W.S == 0){ break; } } if(W.S == 0){ cout << setprecision(19) << fixed << (long double)(0.0) << endl; } else { /*cout << "========================== WIERZCHOLKI =============================" << endl; for(int i = 0; i < W.S; i++){ cout << "(" << W.x[i] << ", " << W.y[i] << ")" << endl; } cout << "========================== KONIEC =============================" << endl; */ long double P = pole(W); //P += ERR/10; cout << setprecision(19) << fixed << P << endl; } } }
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 | #include <bits/stdc++.h> using namespace std; int N; const long double ERR = 0.0000000001; struct czworka{ int x1; int y1; int x2; int y2; bool prawo; // czy na prawo od wektora P1 -> P2 }; struct wielokat{ vector<long double> x; vector<long double> y; // ostatni punkt jest powtorzony int S; }; int ind1, ind2; vector<czworka> Czar; vector<czworka> X; long double xp, yp; short wp; bool blisko(long double a, long double b){ if(abs(a - b) < ERR){ return true; } return false; } int det(int x1, int y1, int x2, int y2, int x3, int y3){ int k = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1; if(k > 0){ k = 1; } else if(k < 0){ k = -1; } // det = 1 oznacza, ze P3 jest na LEWO od P1 -> P2 return k; } int det2(long double x1, long double y1, long double x2, long double y2, long double x3, long double y3){ long double k = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1; if(blisko(k, 0)){ return 0; } else if(k > 0){ return 1; } return -1; // det = 1 oznacza, ze P3 jest na LEWO od P1 -> P2 } bool nowa_prosta(int x1, int y1, int x2, int y2){ int det1, det2; bool czy1 = false; bool czy2 = false; for(int i = 0; i < N; i++){ if((i != ind1) && (i != ind2)){ det1 = det(x1, y1, x2, y2, Czar[i].x1, Czar[i].y1); det2 = det(x1, y1, x2, y2, Czar[i].x2, Czar[i].y2); if(det1 * det2 == 1){ if(det1 == 1){ czy1 = true; if(czy2){ break; } } else { czy2 = true; if(czy1){ break; } } } } } // czy1 jest na lewo // czy2 jest na prawo czworka nowe; nowe.x1 = x1; nowe.y1 = y1; nowe.x2 = x2; nowe.y2 = y2; if(!czy1){ if(!czy2){ return true; } nowe.prawo = false; X.push_back(nowe); } if(!czy2){ nowe.prawo = true; X.push_back(nowe); } return false; } void proste_dwie(long double x1, long double y1, long double x2, long double y2, long double x3, long double y3, long double x4, long double y4){ if(blisko(x1, x2)){ long double a2 = (y4 - y3)/(x4 - x3); long double b2 = y3 - a2 * x3; xp = x1; yp = a2 * xp + b2; } else { if(blisko(x3, x4)){ long double a1 = (y2 - y1)/(x2 - x1); long double b1 = y1 - a1 * x1; xp = x3; yp = a1 * xp + b1; } else { long double a1 = (y2 - y1)/(x2 - x1); long double b1 = y1 - a1 * x1; long double a2 = (y4 - y3)/(x4 - x3); long double b2 = y3 - a2 * x3; xp = (b1 - b2)/(a2 - a1); yp = a1 * xp + b1; } } } wielokat przetnij(wielokat W, long double x1, long double y1, long double x2, long double y2){ wielokat Q; Q.S = 0; long double d1, d2; d1 = det2(x1, y1, x2, y2, W.x[0], W.y[0]); d2 = det2(x1, y1, x2, y2, W.x[1], W.y[1]); for(int j = 0; j < W.S - 1; j++){ if((d1 < 0) && (d2 < 0)){ // wtedy NIE } else if(d1 < 0){ proste_dwie(x1, y1, x2, y2, W.x[j], W.y[j], W.x[j + 1], W.y[j + 1]); Q.x.push_back(xp); Q.y.push_back(yp); Q.S++; if(d2 >= 0){ Q.x.push_back(W.x[j + 1]); Q.y.push_back(W.y[j + 1]); Q.S++; } } else if(d2 < 0){ if(d1 >= 0){ proste_dwie(x1, y1, x2, y2, W.x[j], W.y[j], W.x[j + 1], W.y[j + 1]); Q.x.push_back(xp); Q.y.push_back(yp); Q.S++; } } else { Q.x.push_back(W.x[j + 1]); Q.y.push_back(W.y[j + 1]); Q.S++; } d1 = d2; if(j != W.S - 2){ d2 = det2(x1, y1, x2, y2, W.x[j + 2], W.y[j + 2]); } } if(Q.S > 0){ Q.x.push_back(Q.x[0]); Q.y.push_back(Q.y[0]); Q.S++; } return Q; } long double pole(wielokat W){ if(W.S < 4){ return 0; } long double x1 = W.x[0]; long double y1 = W.y[0]; long double P = 0; for(int i = 1; i < W.S - 2; i++){ long double x2 = W.x[i]; long double y2 = W.y[i]; long double x3 = W.x[i + 1]; long double y3 = W.y[i + 1]; long double PT = abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)); P += PT / 2; } return P; } int main(){ cin >> N; czworka nowe; for(int i = 0; i < N; i++){ cin >> nowe.x1 >> nowe.y1 >> nowe.x2 >> nowe.y2; Czar.push_back(nowe); } bool KONIEC = false; for(int i = 0; i < N - 1; i++){ for(int j = i + 1; j < N; j++){ ind1 = i; ind2 = j; if((nowa_prosta(Czar[i].x1, Czar[i].y1, Czar[j].x1, Czar[j].y1)) || (nowa_prosta(Czar[i].x2, Czar[i].y2, Czar[j].x1, Czar[j].y1)) || (nowa_prosta(Czar[i].x1, Czar[i].y1, Czar[j].x2, Czar[j].y2)) || (nowa_prosta(Czar[i].x2, Czar[i].y2, Czar[j].x2, Czar[j].y2))){ KONIEC = true; break; } } if(KONIEC) break; } if(KONIEC){ cout << setprecision(19) << fixed << (long double)(0.0) << endl; } else { wielokat W; W.x.push_back(-1000); W.x.push_back(-1000); W.x.push_back(1000); W.x.push_back(1000); W.x.push_back(-1000); W.y.push_back(-1000); W.y.push_back(1000); W.y.push_back(1000); W.y.push_back(-1000); W.y.push_back(-1000); W.S = 5; for(int i = 0; i < X.size(); i++){ if(X[i].prawo){ W = przetnij(W, X[i].x1, X[i].y1, X[i].x2, X[i].y2); } else { W = przetnij(W, X[i].x2, X[i].y2, X[i].x1, X[i].y1); } if(W.S == 0){ break; } } if(W.S == 0){ cout << setprecision(19) << fixed << (long double)(0.0) << endl; } else { /*cout << "========================== WIERZCHOLKI =============================" << endl; for(int i = 0; i < W.S; i++){ cout << "(" << W.x[i] << ", " << W.y[i] << ")" << endl; } cout << "========================== KONIEC =============================" << endl; */ long double P = pole(W); //P += ERR/10; cout << setprecision(19) << fixed << P << endl; } } } |