def get_voids_between_lines_and_territory(self, lines): boundary = self.get_boundary() graph = self.get_graph(boundary) voids = [] for i_lp1, lp1 in enumerate(lines): for point in get_neighboring(lp1): if point in boundary: prev = None for lp2 in lines[:i_lp1 + 1]: start_points = self._get_start_points(lp2, boundary) for start_point in start_points: if prev and (self.is_siblings(prev, start_point) or prev == start_point): prev = start_point continue end_index = boundary.index(point) start_index = boundary.index(start_point) try: path = nx.shortest_path(graph, end_index, start_index, weight='weight') except (nx.NetworkXNoPath, nx.NodeNotFound): continue if len(path) > 1 and path[0] == path[-1]: path = path[1:] path = [boundary[index] for index in path] lines_path = lines[lines.index(lp2):i_lp1 + 1] voids.append(lines_path + path) prev = start_point return voids
def get_voids_between_lines_and_territory(self, lines): boundary = self.get_boundary() voids = [] for cur in lines: for point in get_neighboring(cur): if point in boundary: start_point = self.get_nearest_boundary(lines[0], boundary) if start_point: end_index = boundary.index(point) start_index = boundary.index(start_point) try: path = self.get_path(start_index, end_index, boundary) except (nx.NetworkXNoPath, nx.NodeNotFound): continue if len(path) > 1 and path[0] == path[-1]: path = path[1:] path = [boundary[index] for index in path] lines_path = lines[:lines.index(cur) + 1] voids.append(lines_path + path) return voids
def get_boundary(self): boundary = [] for point in self.points: if any([ neighboring not in self.points for neighboring in get_neighboring(point) ]): boundary.append(point) return boundary
def get_boundary(self): # TODO переписать boundary = [] for point in self.cells: if any([ neighboring not in self.cells for neighboring in get_neighboring(point) ]): boundary.append(point) return boundary
def capture_voids_between_lines(self, lines): captured = [] for index, cur in enumerate(lines): for point in get_neighboring(cur): if point in lines: end_index = lines.index(point) path = lines[index:end_index] if len(path) >= 8: captured.extend(self._capture(path)) return captured
def __init__(self, x, y, color): self.color = color self.points = {(x, y), *get_neighboring((x, y))} self.changed = True
def get_nearest_boundary(self, point, boundary): for neighbor in [point, *get_neighboring(point)]: if neighbor in boundary: return neighbor
def __init__(self, x, y, color): self.color = color self.points = {(x, y), *get_neighboring((x, y))} # fro scripted_coll_8 # self.points = {(x, y), *get_neighboring((x, y)), (x - WIDTH, y - 3 * WIDTH), (x, y - 4 * WIDTH), (x + WIDTH, y - 3 * WIDTH)} self.changed = True
def _get_start_points(self, point, boundary): res = [] for neighbor in [point, *get_neighboring(point)]: if neighbor in boundary: res.append(neighbor) return res
def get_siblings(self, point, boundary): return [ sibling for sibling in get_neighboring(point) if sibling in boundary ]