@@ -387,6 +387,36 @@ def set_all_edges(self):
387
387
cell .left = cell .right = cell .top = cell .bottom = True
388
388
return self
389
389
390
+ def generate_closest_points (self , coord , upper_value , joint_tol ):
391
+ return [
392
+ i
393
+ for i , t in enumerate (upper_value )
394
+ if np .isclose (coord , t [0 ], atol = joint_tol )
395
+ ]
396
+
397
+ def add_directed_edges (self , l_init , k_init , J , direction ):
398
+ L = l_init
399
+ K = k_init
400
+
401
+ while J < K :
402
+ match direction :
403
+ case 'left' :
404
+ self .cells [J ][L ].left = True
405
+ case 'right' :
406
+ self .cells [J ][L ].right = True
407
+ case 'top' :
408
+ self .cells [L ][J ].top = True
409
+ case 'bottom' :
410
+ self .cells [L ][J ].bottom = True
411
+ case 'left and right' :
412
+ self .cells [J ][L ].left = True
413
+ self .cells [J ][L - 1 ].right = True
414
+ case _:
415
+ self .cells [L ][J ].top = True
416
+ self .cells [L - 1 ][J ].bottom = True
417
+ J += 1
418
+ return J
419
+
390
420
def set_edges (self , vertical , horizontal , joint_tol = 2 ):
391
421
"""Sets a cell's edges to True depending on whether the cell's
392
422
coordinates overlap with the line's coordinates within a
@@ -403,122 +433,36 @@ def set_edges(self, vertical, horizontal, joint_tol=2):
403
433
for v in vertical :
404
434
# find closest x coord
405
435
# iterate over y coords and find closest start and end points
406
- i = [
407
- i
408
- for i , t in enumerate (self .cols )
409
- if np .isclose (v [0 ], t [0 ], atol = joint_tol )
410
- ]
411
- j = [
412
- j
413
- for j , t in enumerate (self .rows )
414
- if np .isclose (v [3 ], t [0 ], atol = joint_tol )
415
- ]
416
- k = [
417
- k
418
- for k , t in enumerate (self .rows )
419
- if np .isclose (v [1 ], t [0 ], atol = joint_tol )
420
- ]
436
+ i = self .generate_closest_points (v [0 ], self .cols , joint_tol )
437
+ j = self .generate_closest_points (v [3 ], self .rows , joint_tol )
438
+ k = self .generate_closest_points (v [1 ], self .rows , joint_tol )
439
+
421
440
if not j :
422
441
continue
423
442
J = j [0 ]
424
443
if i == [0 ]: # only left edge
425
- L = i [0 ]
426
- if k :
427
- K = k [0 ]
428
- while J < K :
429
- self .cells [J ][L ].left = True
430
- J += 1
431
- else :
432
- K = len (self .rows )
433
- while J < K :
434
- self .cells [J ][L ].left = True
435
- J += 1
444
+ J = self .add_directed_edges (i [0 ], k [0 ] if k else len (self .rows ), J , 'left' )
436
445
elif i == []: # only right edge
437
- L = len (self .cols ) - 1
438
- if k :
439
- K = k [0 ]
440
- while J < K :
441
- self .cells [J ][L ].right = True
442
- J += 1
443
- else :
444
- K = len (self .rows )
445
- while J < K :
446
- self .cells [J ][L ].right = True
447
- J += 1
446
+ J = self .add_directed_edges (len (self .cols ) - 1 , k [0 ] if k else len (self .rows ), J , 'right' )
448
447
else : # both left and right edges
449
- L = i [0 ]
450
- if k :
451
- K = k [0 ]
452
- while J < K :
453
- self .cells [J ][L ].left = True
454
- self .cells [J ][L - 1 ].right = True
455
- J += 1
456
- else :
457
- K = len (self .rows )
458
- while J < K :
459
- self .cells [J ][L ].left = True
460
- self .cells [J ][L - 1 ].right = True
461
- J += 1
448
+ J = self .add_directed_edges (i [0 ], k [0 ] if k else len (self .rows ), J , 'left and right' )
462
449
463
450
for h in horizontal :
464
451
# find closest y coord
465
452
# iterate over x coords and find closest start and end points
466
- i = [
467
- i
468
- for i , t in enumerate (self .rows )
469
- if np .isclose (h [1 ], t [0 ], atol = joint_tol )
470
- ]
471
- j = [
472
- j
473
- for j , t in enumerate (self .cols )
474
- if np .isclose (h [0 ], t [0 ], atol = joint_tol )
475
- ]
476
- k = [
477
- k
478
- for k , t in enumerate (self .cols )
479
- if np .isclose (h [2 ], t [0 ], atol = joint_tol )
480
- ]
453
+ i = self .generate_closest_points (h [1 ], self .rows , joint_tol )
454
+ j = self .generate_closest_points (h [0 ], self .cols , joint_tol )
455
+ k = self .generate_closest_points (h [2 ], self .cols , joint_tol )
456
+
481
457
if not j :
482
458
continue
483
459
J = j [0 ]
484
460
if i == [0 ]: # only top edge
485
- L = i [0 ]
486
- if k :
487
- K = k [0 ]
488
- while J < K :
489
- self .cells [L ][J ].top = True
490
- J += 1
491
- else :
492
- K = len (self .cols )
493
- while J < K :
494
- self .cells [L ][J ].top = True
495
- J += 1
461
+ J = self .add_directed_edges (i [0 ], k [0 ] if k else len (self .rows ), J , 'top' )
496
462
elif i == []: # only bottom edge
497
- L = len (self .rows ) - 1
498
- if k :
499
- K = k [0 ]
500
- while J < K :
501
- self .cells [L ][J ].bottom = True
502
- J += 1
503
- else :
504
- K = len (self .cols )
505
- while J < K :
506
- self .cells [L ][J ].bottom = True
507
- J += 1
463
+ J = self .add_directed_edges (len (self .rows ) - 1 , k [0 ] if k else len (self .rows ), J , 'bottom' )
508
464
else : # both top and bottom edges
509
- L = i [0 ]
510
- if k :
511
- K = k [0 ]
512
- while J < K :
513
- self .cells [L ][J ].top = True
514
- self .cells [L - 1 ][J ].bottom = True
515
- J += 1
516
- else :
517
- K = len (self .cols )
518
- while J < K :
519
- self .cells [L ][J ].top = True
520
- self .cells [L - 1 ][J ].bottom = True
521
- J += 1
465
+ J = self .add_directed_edges (i [0 ], k [0 ] if k else len (self .rows ), J , 'top and bottom' )
522
466
523
467
return self
524
468
0 commit comments