#include <cstdio> #include <vector> #include <algorithm> #include <set> #include <map> int direction; struct point { double x; double y; bool operator<(const point& b) const { return (x < b.x) || ((x == b.x) && (y < b.y)); } bool operator==(const point& b) const { return (x == b.x) && (y == b.y); } void print() const { printf("{%lf, %lf}", x, y); } }; bool mysort2(std::pair<point, int> a, std::pair<point, int> b){ return (a.first.y < b.first.y) || ((a.first.y == b.first.y) && (a.first.x < b.first.x)); } bool mysort(std::pair<point, std::pair<point,point>> a, std::pair<point, std::pair<point,point>> b){ if (direction == 0){ return (a.first.x < b.first.x) || ((a.first.x == b.first.x) && (a.first.y <= b.first.y)); } else if (direction == 1){ return (a.first.x < b.first.x) || ((a.first.x == b.first.x) && (a.first.y >= b.first.y)); } else if (direction == 2){ return (a.first.x > b.first.x) || ((a.first.x == b.first.x) && (a.first.y >= b.first.y)); } else { return (a.first.x > b.first.x) || ((a.first.x == b.first.x) && (a.first.y <= b.first.y)); } } point curr; double vecf(point a, point b, point c){ return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); } inline int vec(point a, point b, point c){ double v = (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); if (v > 0.000000001){ return 1; } if (v < -0.000000001){ return -1; } return 0; } bool vecsort(point a, point b){ return vec(a, b, curr) >= 0; } std::map<point, int> points; std::vector<std::set<int>> kr; std::vector<point> id; std::vector<int> blocked; std::vector<int> kr2, kr3; std::vector<std::vector<int>> kr4; std::vector<std::pair<point,point>> line; std::set<std::pair<point,point>> all, err; bool vecsort2(int a, int b){ return vec(id[a], id[b], curr) >= 0; } int n, next; std::pair<bool, point> intersect(std::pair<point, point> a, std::pair<point, point> b){ int c1 = vec(b.first, b.second, a.first); int c2 = vec(b.first, b.second, a.second); int d1 = vec(a.first, a.second, b.first); int d2 = vec(a.first, a.second, b.second); if ((c1 * c2 < 0) && (d1 * d2 < 0)){ if ((c1 == 0) && (c2 == 0) && (d1 == 0) && (d2 == 0)){ return {false, {0,0}}; } double x1 = a.first.x, x2 = a.second.x, x3 = b.first.x, x4 = b.second.x; double y1 = a.first.y, y2 = a.second.y, y3 = b.first.y, y4 = b.second.y; double x = ((x1 * y2 - x2 * y1) * (x3 - x4) - (x3 * y4 - y3 * x4) * (x1 - x2)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); double y = ((x1 * y2 - x2 * y1) * (y3 - y4) - (x3 * y4 - y3 * x4) * (y1 - y2)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); return {true, {x, y}}; } else { return {false, {0,0}}; } } std::vector<std::pair<point, point>> convex(int s, int t){ std::vector<std::pair<point, point>> v; std::vector<point> tmp; if (intersect(line[s], line[t]).first){ //printf("a\n"); tmp.push_back(line[s].first); tmp.push_back(line[t].second); tmp.push_back(line[s].second); tmp.push_back(line[t].first); } else if (intersect(std::make_pair(line[s].second, line[t].first), std::make_pair(line[s].first, line[t].second)).first){ //printf("c\n"); tmp.push_back(line[s].first); tmp.push_back(line[t].first); tmp.push_back(line[t].second); tmp.push_back(line[s].second); } else { //printf("b\n"); std::vector<point> __v; __v.push_back(line[s].first); __v.push_back(line[s].second); __v.push_back(line[t].first); __v.push_back(line[t].second); sort(__v.begin(), __v.end()); point c = __v[0]; __v.erase(__v.begin()); tmp.push_back(c); curr = c; sort(__v.begin(), __v.end(), vecsort); __v.push_back(line[s].first); for (int i = 0; i < (int)__v.size(); i++){ if (i >= 2){ if (vec(tmp[1], tmp[2], tmp[0]) * vec(tmp[tmp.size() - 1], __v[i], tmp[tmp.size() - 2]) < 0){ tmp.pop_back(); } } if (i < (int)__v.size() - 1){ tmp.push_back(__v[i]); } } } if (vec(tmp[1], tmp[2], tmp[0]) < 0){ //printf("d\n"); for (int i = 0; i < (int)tmp.size(); i++){ v.push_back({tmp[i], tmp[(i + 1) % tmp.size()]}); } } else { //printf("e\n"); for (int i = 0; i < (int)tmp.size(); i++){ v.push_back({tmp[(i + 1) % tmp.size()], tmp[i]}); } } return v; } std::vector<int> cycle; int findcycle(int v, int pop){ //printf("what %d %d ", v, (int)kr4[v].size()); //id[v].print(); //printf("\n"); blocked[v] = 1; //printf("i am: %d\n", v); /*for (int i = 0; i < (int)kr4[v].size(); i++){ printf("%d ", kr4[v][i]); } printf("\n");*/ for (int i = 0; i < (int)kr4[v].size(); i++){ /*if ((v == 12) & (i == 1)){ printf("tetra %d %d\n", blocked[kr4[v][i]], kr4[v][i]); }*/ if (kr4[v][i] == v) continue; if (blocked[kr4[v][i]] == 0){ /*printf("z\n"); id[v].print(); id[kr4[v][i]].print(); id[pop].print(); printf("%lf\n", vecf(id[v], id[kr4[v][i]], id[pop]));*/ if ((pop == -1) && ((id[kr4[v][i]].x < id[v].x) || (id[kr4[v][i]].y < id[v].y))){ //printf("e\n"); continue; } if ((pop != -1) && (vec(id[v], id[kr4[v][i]], id[pop]) < 0)){ /*printf("a\n"); id[v].print(); id[kr4[v][i]].print(); id[pop].print(); printf("%lf\n", vecf(id[v], id[kr4[v][i]], id[pop]));*/ continue; } int t = findcycle(kr4[v][i], v); if (t == 2){ blocked[v] = 2; if (v == cycle[0]){ return 1; } else { cycle.push_back(v); return 2; } } else if (t == 1){ blocked[v] = 2; return 1; } } if ((blocked[kr4[v][i]] == 1) && (kr4[v][i] != pop)){ //printf("homhom\n"); cycle.push_back(kr4[v][i]); cycle.push_back(v); return 2; } } //printf("back\n"); blocked[v] = 2; return 0; } int main(){ scanf("%d", &n); for (int i = 0; i < n; i++){ int a,b,c,d; scanf("%d%d%d%d", &a, &b, &c, &d); line.push_back(std::make_pair(point({(double)a, (double)b}), point({(double)c, (double)d}))); id.push_back(point({(double)a, (double)b})); id.push_back(point({(double)c, (double)d})); kr.push_back(std::set<int>()); kr.push_back(std::set<int>()); kr[2 * i].insert(2 * i + 1); kr[2 * i + 1].insert(2 * i); points[{(double)a, (double)b}] = 2 * i; points[{(double)c, (double)d}] = 2 * i + 1; } next = 2 * n; for (int i = 0; i < line.size(); i++){ all.insert(line[i]); } for (int i = 0; i < n; i++){ for (int j = i + 1; j < n; j++){ auto v = convex(i, j); /*for (int k = 0; k < (int)v.size(); k++){ v[k].first.print(); v[k].second.print(); printf("\n"); } printf("\n");*/ for (int k = 0; k < (int)v.size(); k++){ if (v[k].first.x < v[k].second.x){ if (v[k].first.y < v[k].second.y){ direction = 0; } else { direction = 1; } } else { if (v[k].first.y < v[k].second.y){ direction = 3; } else { direction = 2; } } if (err.find(v[k]) != err.end()){ continue; } std::vector<std::pair<point, std::pair<point,point>>> is; std::vector<std::pair<point,point>> to_erase, to_introduce; is.push_back(std::make_pair(v[k].first, std::make_pair(point({0, 0}), point({0, 0})))); for (auto it = all.begin(); it != all.end(); it++){ if (err.find(*it) != err.end()){ //printf("cococo %d\n", err.size()); continue; } auto t = intersect(v[k], *it); if (t.first){ /*printf("chocho "); v[k].first.print(); v[k].second.print(); it->first.print(); it->second.print(); printf(" inter "); t.second.print(); printf("\n");*/ is.push_back({t.second, *it}); to_erase.push_back(*it); to_introduce.push_back({it->first, t.second}); to_introduce.push_back({t.second, it->second}); } } is.push_back(std::make_pair(v[k].second, std::make_pair(point({0,0}), point({0,0})))); std::sort(is.begin(), is.end(), mysort); for (int l = 0; l < (int)is.size() - 1; l++){ int v, w; if (points.find(is[l].first) != points.end()){ v = points[is[l].first]; } else { v = next; kr.push_back(std::set<int>()); points[is[l].first] = v; id.push_back(is[l].first); next++; } if (points.find(is[l + 1].first) != points.end()){ w = points[is[l + 1].first]; } else { w = next; kr.push_back(std::set<int>()); points[is[l + 1].first] = w; id.push_back(is[l + 1].first); next++; } /*printf("lom lom "); id[v].print(); id[w].print(); printf("\n");*/ if (kr[v].find(w) == kr[v].end()){ kr[v].insert(w); } to_introduce.push_back({id[v], id[w]}); if (l > 0){ int s, t; s = points[is[l].second.first]; t = points[is[l].second.second]; if ((v != s) && (v != t)){ if (kr[s].find(t) != kr[s].end()){ kr[s].erase(t); /*if (kr[s].find(t) != kr[s].end()){ printf("BUUUUUUUUUURZA\n"); }*/ kr[s].insert(v); kr[v].insert(t); //printf("zozo %d %d %d", s, t, v); /*id[s].print(); id[t].print(); id[v].print(); printf("\n");*/ } if (kr[t].find(s) != kr[t].end()){ kr[t].erase(s); /*if (kr[t].find(s) != kr[t].end()){ printf("BUUUUUUUUUURZA\n"); }*/ kr[t].insert(v); kr[v].insert(s); /*printf("toto %d %d %d", s, t, v); id[t].print(); id[s].print(); id[v].print(); printf("\n");*/ } } } } /*if (kr[7].find(6) != kr[7].end()){ printf("CHOCHOCHO\n"); }*/ if (is.size() > 2){ all.erase(std::make_pair(v[k].first, v[k].second)); all.erase(std::make_pair(v[k].second, v[k].first)); err.insert(std::make_pair(v[k].first, v[k].second)); err.insert(std::make_pair(v[k].second, v[k].first)); } for (int l = 0; l < (int)to_erase.size(); l++){ all.erase(to_erase[l]); err.insert(std::make_pair(to_erase[l].first, to_erase[l].second)); err.insert(std::make_pair(to_erase[l].second, to_erase[l].first)); } for (int l = 0; l < (int)to_introduce.size(); l++){ all.insert(to_introduce[l]); } } //printf("dupa1\n"); } } /*printf("jestem tu %d\n", (int)id.size()); for (int i = 0; i < (int)id.size(); i++){ printf("vertex %d at ", i); id[i].print(); printf(" size: %d\n", (int)kr[i].size()); for (auto it = kr[i].begin(); it != kr[i].end(); it++){ id[i].print(); printf("->"); id[*it].print(); printf(" (aka %d)", *it); printf("\n"); } }*/ blocked.resize(id.size()); for (int i = 0; i < (int)id.size(); i++){ for (auto it = kr[i].begin(); it != kr[i].end(); it++){ if ((id[*it].y > id[i].y) || ((id[*it].y == id[i].y) && (id[*it].x > id[i].x))){ kr2.push_back(*it); } else { kr3.push_back(*it); } } //printf("hos1 %d\n", i); curr = id[i]; std::sort(kr2.begin(), kr2.end(), vecsort2); ///printf("coco\n"); std::sort(kr3.begin(), kr3.end(), vecsort2); kr[i].clear(); kr4.push_back(std::vector<int>()); //printf("hos2 %d\n", i); for (int j = 0; j < (int)kr2.size(); j++){ kr4[i].push_back(kr2[j]); // printf("w%d ", kr2[j]); } for (int j = 0; j < (int)kr3.size(); j++){ kr4[i].push_back(kr3[j]); // printf("z%d ", kr3[j]); } kr2.clear(); kr3.clear(); } //printf("o_co_chodzi\n"); std::vector<std::pair<point,int>> mm; for (auto it = points.begin(); it != points.end(); it++){ mm.push_back(*it); } sort(mm.begin(), mm.end(), mysort2); double best = 0; for (int i = 0; i < mm.size(); i++){ for (int k = 0; k < mm.size(); k++){ blocked[k] = 0; } int f = mm[i].second; if (blocked[f] == 0){ // printf("entering %d", f); // mm[i].first.print(); // printf("\n"); if (findcycle(f, -1) > 0){ // printf("cycle\n"); // printf("%d %d\n", kr4[9][0], kr4[9][1]); /* for (int i = 0; i < (int)cycle.size(); i++){ printf("%d ", cycle[i]); } printf("\n"); for (int i = 0; i < (int)cycle.size(); i++){ id[cycle[i]].print(); } printf("\n");*/ point a,b,c; a = id[cycle[0]]; double res = 0; for (int i = 1; i < cycle.size() - 1; i++){ b = id[cycle[i]]; c = id[cycle[(i + 1)]]; res += vecf(b, c, a); } cycle.clear(); best = std::max(best, std::abs(res) / 2); //printf("%.8lf\n", std::abs(res) / 2); //return 0; } } } printf("%.8lf\n", best); }
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | #include <cstdio> #include <vector> #include <algorithm> #include <set> #include <map> int direction; struct point { double x; double y; bool operator<(const point& b) const { return (x < b.x) || ((x == b.x) && (y < b.y)); } bool operator==(const point& b) const { return (x == b.x) && (y == b.y); } void print() const { printf("{%lf, %lf}", x, y); } }; bool mysort2(std::pair<point, int> a, std::pair<point, int> b){ return (a.first.y < b.first.y) || ((a.first.y == b.first.y) && (a.first.x < b.first.x)); } bool mysort(std::pair<point, std::pair<point,point>> a, std::pair<point, std::pair<point,point>> b){ if (direction == 0){ return (a.first.x < b.first.x) || ((a.first.x == b.first.x) && (a.first.y <= b.first.y)); } else if (direction == 1){ return (a.first.x < b.first.x) || ((a.first.x == b.first.x) && (a.first.y >= b.first.y)); } else if (direction == 2){ return (a.first.x > b.first.x) || ((a.first.x == b.first.x) && (a.first.y >= b.first.y)); } else { return (a.first.x > b.first.x) || ((a.first.x == b.first.x) && (a.first.y <= b.first.y)); } } point curr; double vecf(point a, point b, point c){ return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); } inline int vec(point a, point b, point c){ double v = (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); if (v > 0.000000001){ return 1; } if (v < -0.000000001){ return -1; } return 0; } bool vecsort(point a, point b){ return vec(a, b, curr) >= 0; } std::map<point, int> points; std::vector<std::set<int>> kr; std::vector<point> id; std::vector<int> blocked; std::vector<int> kr2, kr3; std::vector<std::vector<int>> kr4; std::vector<std::pair<point,point>> line; std::set<std::pair<point,point>> all, err; bool vecsort2(int a, int b){ return vec(id[a], id[b], curr) >= 0; } int n, next; std::pair<bool, point> intersect(std::pair<point, point> a, std::pair<point, point> b){ int c1 = vec(b.first, b.second, a.first); int c2 = vec(b.first, b.second, a.second); int d1 = vec(a.first, a.second, b.first); int d2 = vec(a.first, a.second, b.second); if ((c1 * c2 < 0) && (d1 * d2 < 0)){ if ((c1 == 0) && (c2 == 0) && (d1 == 0) && (d2 == 0)){ return {false, {0,0}}; } double x1 = a.first.x, x2 = a.second.x, x3 = b.first.x, x4 = b.second.x; double y1 = a.first.y, y2 = a.second.y, y3 = b.first.y, y4 = b.second.y; double x = ((x1 * y2 - x2 * y1) * (x3 - x4) - (x3 * y4 - y3 * x4) * (x1 - x2)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); double y = ((x1 * y2 - x2 * y1) * (y3 - y4) - (x3 * y4 - y3 * x4) * (y1 - y2)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); return {true, {x, y}}; } else { return {false, {0,0}}; } } std::vector<std::pair<point, point>> convex(int s, int t){ std::vector<std::pair<point, point>> v; std::vector<point> tmp; if (intersect(line[s], line[t]).first){ //printf("a\n"); tmp.push_back(line[s].first); tmp.push_back(line[t].second); tmp.push_back(line[s].second); tmp.push_back(line[t].first); } else if (intersect(std::make_pair(line[s].second, line[t].first), std::make_pair(line[s].first, line[t].second)).first){ //printf("c\n"); tmp.push_back(line[s].first); tmp.push_back(line[t].first); tmp.push_back(line[t].second); tmp.push_back(line[s].second); } else { //printf("b\n"); std::vector<point> __v; __v.push_back(line[s].first); __v.push_back(line[s].second); __v.push_back(line[t].first); __v.push_back(line[t].second); sort(__v.begin(), __v.end()); point c = __v[0]; __v.erase(__v.begin()); tmp.push_back(c); curr = c; sort(__v.begin(), __v.end(), vecsort); __v.push_back(line[s].first); for (int i = 0; i < (int)__v.size(); i++){ if (i >= 2){ if (vec(tmp[1], tmp[2], tmp[0]) * vec(tmp[tmp.size() - 1], __v[i], tmp[tmp.size() - 2]) < 0){ tmp.pop_back(); } } if (i < (int)__v.size() - 1){ tmp.push_back(__v[i]); } } } if (vec(tmp[1], tmp[2], tmp[0]) < 0){ //printf("d\n"); for (int i = 0; i < (int)tmp.size(); i++){ v.push_back({tmp[i], tmp[(i + 1) % tmp.size()]}); } } else { //printf("e\n"); for (int i = 0; i < (int)tmp.size(); i++){ v.push_back({tmp[(i + 1) % tmp.size()], tmp[i]}); } } return v; } std::vector<int> cycle; int findcycle(int v, int pop){ //printf("what %d %d ", v, (int)kr4[v].size()); //id[v].print(); //printf("\n"); blocked[v] = 1; //printf("i am: %d\n", v); /*for (int i = 0; i < (int)kr4[v].size(); i++){ printf("%d ", kr4[v][i]); } printf("\n");*/ for (int i = 0; i < (int)kr4[v].size(); i++){ /*if ((v == 12) & (i == 1)){ printf("tetra %d %d\n", blocked[kr4[v][i]], kr4[v][i]); }*/ if (kr4[v][i] == v) continue; if (blocked[kr4[v][i]] == 0){ /*printf("z\n"); id[v].print(); id[kr4[v][i]].print(); id[pop].print(); printf("%lf\n", vecf(id[v], id[kr4[v][i]], id[pop]));*/ if ((pop == -1) && ((id[kr4[v][i]].x < id[v].x) || (id[kr4[v][i]].y < id[v].y))){ //printf("e\n"); continue; } if ((pop != -1) && (vec(id[v], id[kr4[v][i]], id[pop]) < 0)){ /*printf("a\n"); id[v].print(); id[kr4[v][i]].print(); id[pop].print(); printf("%lf\n", vecf(id[v], id[kr4[v][i]], id[pop]));*/ continue; } int t = findcycle(kr4[v][i], v); if (t == 2){ blocked[v] = 2; if (v == cycle[0]){ return 1; } else { cycle.push_back(v); return 2; } } else if (t == 1){ blocked[v] = 2; return 1; } } if ((blocked[kr4[v][i]] == 1) && (kr4[v][i] != pop)){ //printf("homhom\n"); cycle.push_back(kr4[v][i]); cycle.push_back(v); return 2; } } //printf("back\n"); blocked[v] = 2; return 0; } int main(){ scanf("%d", &n); for (int i = 0; i < n; i++){ int a,b,c,d; scanf("%d%d%d%d", &a, &b, &c, &d); line.push_back(std::make_pair(point({(double)a, (double)b}), point({(double)c, (double)d}))); id.push_back(point({(double)a, (double)b})); id.push_back(point({(double)c, (double)d})); kr.push_back(std::set<int>()); kr.push_back(std::set<int>()); kr[2 * i].insert(2 * i + 1); kr[2 * i + 1].insert(2 * i); points[{(double)a, (double)b}] = 2 * i; points[{(double)c, (double)d}] = 2 * i + 1; } next = 2 * n; for (int i = 0; i < line.size(); i++){ all.insert(line[i]); } for (int i = 0; i < n; i++){ for (int j = i + 1; j < n; j++){ auto v = convex(i, j); /*for (int k = 0; k < (int)v.size(); k++){ v[k].first.print(); v[k].second.print(); printf("\n"); } printf("\n");*/ for (int k = 0; k < (int)v.size(); k++){ if (v[k].first.x < v[k].second.x){ if (v[k].first.y < v[k].second.y){ direction = 0; } else { direction = 1; } } else { if (v[k].first.y < v[k].second.y){ direction = 3; } else { direction = 2; } } if (err.find(v[k]) != err.end()){ continue; } std::vector<std::pair<point, std::pair<point,point>>> is; std::vector<std::pair<point,point>> to_erase, to_introduce; is.push_back(std::make_pair(v[k].first, std::make_pair(point({0, 0}), point({0, 0})))); for (auto it = all.begin(); it != all.end(); it++){ if (err.find(*it) != err.end()){ //printf("cococo %d\n", err.size()); continue; } auto t = intersect(v[k], *it); if (t.first){ /*printf("chocho "); v[k].first.print(); v[k].second.print(); it->first.print(); it->second.print(); printf(" inter "); t.second.print(); printf("\n");*/ is.push_back({t.second, *it}); to_erase.push_back(*it); to_introduce.push_back({it->first, t.second}); to_introduce.push_back({t.second, it->second}); } } is.push_back(std::make_pair(v[k].second, std::make_pair(point({0,0}), point({0,0})))); std::sort(is.begin(), is.end(), mysort); for (int l = 0; l < (int)is.size() - 1; l++){ int v, w; if (points.find(is[l].first) != points.end()){ v = points[is[l].first]; } else { v = next; kr.push_back(std::set<int>()); points[is[l].first] = v; id.push_back(is[l].first); next++; } if (points.find(is[l + 1].first) != points.end()){ w = points[is[l + 1].first]; } else { w = next; kr.push_back(std::set<int>()); points[is[l + 1].first] = w; id.push_back(is[l + 1].first); next++; } /*printf("lom lom "); id[v].print(); id[w].print(); printf("\n");*/ if (kr[v].find(w) == kr[v].end()){ kr[v].insert(w); } to_introduce.push_back({id[v], id[w]}); if (l > 0){ int s, t; s = points[is[l].second.first]; t = points[is[l].second.second]; if ((v != s) && (v != t)){ if (kr[s].find(t) != kr[s].end()){ kr[s].erase(t); /*if (kr[s].find(t) != kr[s].end()){ printf("BUUUUUUUUUURZA\n"); }*/ kr[s].insert(v); kr[v].insert(t); //printf("zozo %d %d %d", s, t, v); /*id[s].print(); id[t].print(); id[v].print(); printf("\n");*/ } if (kr[t].find(s) != kr[t].end()){ kr[t].erase(s); /*if (kr[t].find(s) != kr[t].end()){ printf("BUUUUUUUUUURZA\n"); }*/ kr[t].insert(v); kr[v].insert(s); /*printf("toto %d %d %d", s, t, v); id[t].print(); id[s].print(); id[v].print(); printf("\n");*/ } } } } /*if (kr[7].find(6) != kr[7].end()){ printf("CHOCHOCHO\n"); }*/ if (is.size() > 2){ all.erase(std::make_pair(v[k].first, v[k].second)); all.erase(std::make_pair(v[k].second, v[k].first)); err.insert(std::make_pair(v[k].first, v[k].second)); err.insert(std::make_pair(v[k].second, v[k].first)); } for (int l = 0; l < (int)to_erase.size(); l++){ all.erase(to_erase[l]); err.insert(std::make_pair(to_erase[l].first, to_erase[l].second)); err.insert(std::make_pair(to_erase[l].second, to_erase[l].first)); } for (int l = 0; l < (int)to_introduce.size(); l++){ all.insert(to_introduce[l]); } } //printf("dupa1\n"); } } /*printf("jestem tu %d\n", (int)id.size()); for (int i = 0; i < (int)id.size(); i++){ printf("vertex %d at ", i); id[i].print(); printf(" size: %d\n", (int)kr[i].size()); for (auto it = kr[i].begin(); it != kr[i].end(); it++){ id[i].print(); printf("->"); id[*it].print(); printf(" (aka %d)", *it); printf("\n"); } }*/ blocked.resize(id.size()); for (int i = 0; i < (int)id.size(); i++){ for (auto it = kr[i].begin(); it != kr[i].end(); it++){ if ((id[*it].y > id[i].y) || ((id[*it].y == id[i].y) && (id[*it].x > id[i].x))){ kr2.push_back(*it); } else { kr3.push_back(*it); } } //printf("hos1 %d\n", i); curr = id[i]; std::sort(kr2.begin(), kr2.end(), vecsort2); ///printf("coco\n"); std::sort(kr3.begin(), kr3.end(), vecsort2); kr[i].clear(); kr4.push_back(std::vector<int>()); //printf("hos2 %d\n", i); for (int j = 0; j < (int)kr2.size(); j++){ kr4[i].push_back(kr2[j]); // printf("w%d ", kr2[j]); } for (int j = 0; j < (int)kr3.size(); j++){ kr4[i].push_back(kr3[j]); // printf("z%d ", kr3[j]); } kr2.clear(); kr3.clear(); } //printf("o_co_chodzi\n"); std::vector<std::pair<point,int>> mm; for (auto it = points.begin(); it != points.end(); it++){ mm.push_back(*it); } sort(mm.begin(), mm.end(), mysort2); double best = 0; for (int i = 0; i < mm.size(); i++){ for (int k = 0; k < mm.size(); k++){ blocked[k] = 0; } int f = mm[i].second; if (blocked[f] == 0){ // printf("entering %d", f); // mm[i].first.print(); // printf("\n"); if (findcycle(f, -1) > 0){ // printf("cycle\n"); // printf("%d %d\n", kr4[9][0], kr4[9][1]); /* for (int i = 0; i < (int)cycle.size(); i++){ printf("%d ", cycle[i]); } printf("\n"); for (int i = 0; i < (int)cycle.size(); i++){ id[cycle[i]].print(); } printf("\n");*/ point a,b,c; a = id[cycle[0]]; double res = 0; for (int i = 1; i < cycle.size() - 1; i++){ b = id[cycle[i]]; c = id[cycle[(i + 1)]]; res += vecf(b, c, a); } cycle.clear(); best = std::max(best, std::abs(res) / 2); //printf("%.8lf\n", std::abs(res) / 2); //return 0; } } } printf("%.8lf\n", best); } |