Implementation of solving Rectangle-Intersection Problem. Performance:: O(nlog n + k) time and O(n) space, where k is the count of intersection pairs. Args: V (list): Rectangle-related x-edges data, [(index, Rect, x), (...), ...]. num (int): Count of V instances, e
(V:list, num:int, index_groups:list)
| 86 | # 4 Call procedure detect(V, H, 2n). |
| 87 | |
| 88 | def solve_rects_intersection(V:list, num:int, index_groups:list): |
| 89 | '''Implementation of solving Rectangle-Intersection Problem. |
| 90 | |
| 91 | Performance:: |
| 92 | |
| 93 | O(nlog n + k) time and O(n) space, where k is the count of intersection pairs. |
| 94 | |
| 95 | Args: |
| 96 | V (list): Rectangle-related x-edges data, [(index, Rect, x), (...), ...]. |
| 97 | num (int): Count of V instances, equal to len(V). |
| 98 | index_groups (list): Target adjacent list for connectivity between rects. |
| 99 | |
| 100 | Procedure ``detect(V, H, m)``:: |
| 101 | |
| 102 | if m < 2 then return else |
| 103 | - let V1 be the first ⌊m/2⌋ and let V2 be the rest of the vertical edges in V in the sorted order; |
| 104 | - let S11 and S22 be the set of rectangles represented only in V1 and V2 but not spanning V2 and V1, respectively; |
| 105 | - let S12 be the set of rectangles represented only in V1 and spanning V2; |
| 106 | - let S21 be the set of rectangles represented only in V2 and spanning V1 |
| 107 | - let H1 and H2 be the list of y-intervals corresponding to the elements of V1 and V2 respectively |
| 108 | - stab(S12, S22); stab(S21, S11); stab(S12, S21) |
| 109 | - detect(V1, H1, ⌊m/2⌋); detect(V2, H2, m − ⌊m/2⌋) |
| 110 | ''' |
| 111 | if num < 2: return |
| 112 | |
| 113 | # start/end points of left/right intervals |
| 114 | center_pos = int(num/2.0) |
| 115 | X0, X, X1 = V[0][-1], V[center_pos-1][-1], V[-1][-1] |
| 116 | |
| 117 | # split into two groups |
| 118 | left = V[0:center_pos] |
| 119 | right = V[center_pos:] |
| 120 | |
| 121 | # filter rects according to their position to each intervals |
| 122 | S11 = list(filter( lambda item: item[1][2]<=X, left )) |
| 123 | S12 = list(filter( lambda item: item[1][2]>=X1, left )) |
| 124 | S22 = list(filter( lambda item: item[1][0]>X, right )) |
| 125 | S21 = list(filter( lambda item: item[1][0]<=X0, right )) |
| 126 | |
| 127 | # intersection in x-direction is fulfilled, so check y-direction further |
| 128 | _stab(S12, S22, index_groups) |
| 129 | _stab(S21, S11, index_groups) |
| 130 | _stab(S12, S21, index_groups) |
| 131 | |
| 132 | # recursive process |
| 133 | solve_rects_intersection(left, center_pos, index_groups) |
| 134 | solve_rects_intersection(right, num-center_pos, index_groups) |
| 135 | |
| 136 | |
| 137 | def _stab(S1:list, S2:list, index_groups:list): |
no test coverage detected
searching dependent graphs…