Given a list of points (`intersections`), return all rectangular "cells" that those points describe. `intersections` should be a dictionary with (x0, top) tuples as keys, and a list of edge objects as values. The edge objects should correspond to the edges that touch the inters
(intersections)
| 1352 | |
| 1353 | |
| 1354 | def intersections_to_cells(intersections): |
| 1355 | """ |
| 1356 | Given a list of points (`intersections`), return all rectangular "cells" |
| 1357 | that those points describe. |
| 1358 | |
| 1359 | `intersections` should be a dictionary with (x0, top) tuples as keys, |
| 1360 | and a list of edge objects as values. The edge objects should correspond |
| 1361 | to the edges that touch the intersection. |
| 1362 | """ |
| 1363 | |
| 1364 | def edge_connects(p1, p2) -> bool: |
| 1365 | def edges_to_set(edges): |
| 1366 | return set(map(obj_to_bbox, edges)) |
| 1367 | |
| 1368 | if p1[0] == p2[0]: |
| 1369 | common = edges_to_set(intersections[p1]["v"]).intersection( |
| 1370 | edges_to_set(intersections[p2]["v"]) |
| 1371 | ) |
| 1372 | if len(common): |
| 1373 | return True |
| 1374 | |
| 1375 | if p1[1] == p2[1]: |
| 1376 | common = edges_to_set(intersections[p1]["h"]).intersection( |
| 1377 | edges_to_set(intersections[p2]["h"]) |
| 1378 | ) |
| 1379 | if len(common): |
| 1380 | return True |
| 1381 | return False |
| 1382 | |
| 1383 | points = list(sorted(intersections.keys())) |
| 1384 | n_points = len(points) |
| 1385 | |
| 1386 | def find_smallest_cell(points, i: int): |
| 1387 | if i == n_points - 1: |
| 1388 | return None |
| 1389 | pt = points[i] |
| 1390 | rest = points[i + 1 :] |
| 1391 | # Get all the points directly below and directly right |
| 1392 | below = [x for x in rest if x[0] == pt[0]] |
| 1393 | right = [x for x in rest if x[1] == pt[1]] |
| 1394 | for below_pt in below: |
| 1395 | if not edge_connects(pt, below_pt): |
| 1396 | continue |
| 1397 | |
| 1398 | for right_pt in right: |
| 1399 | if not edge_connects(pt, right_pt): |
| 1400 | continue |
| 1401 | |
| 1402 | bottom_right = (right_pt[0], below_pt[1]) |
| 1403 | |
| 1404 | if ( |
| 1405 | (bottom_right in intersections) |
| 1406 | and edge_connects(bottom_right, right_pt) |
| 1407 | and edge_connects(bottom_right, below_pt) |
| 1408 | ): |
| 1409 | return (pt[0], pt[1], bottom_right[0], bottom_right[1]) |
| 1410 | return None |
| 1411 |
no test coverage detected
searching dependent graphs…