def load(graph: Graph, file_name: str): graph.clear() # получаем данные из файла # vertexes, v_coordinates = LoadGraph.__split_file(file_name) with open(file_name, 'r') as file: data = json.load(file) try: graph.vertexes = data['vertexes'] except KeyError: return False # загружем данные о координатах или генерируем, если их нет if 'coordinates' in data: coordinates = data['coordinates'] for d in coordinates: graph.vertexes_coordinates[d['name']] = Vertex( d['name'], d['x'], d['y']) else: for v in graph.vertexes: graph.vertexes_coordinates[v] = Vertex( v, random.randint(0, 100), random.randint(0, 100)) if 'oriented' in data: graph.oriented = data['oriented'] if 'weighted' in data: graph.weighted = data['weighted'] if 'path' in data: graph.path = data['path'] graph.update() return True
def create_node(self, v_from: str, v_to: str): return Vertex( 'node', self.vertexes_coordinates[v_from].x - (self.vertexes_coordinates[v_from].x - self.vertexes_coordinates[v_to].x) / 2 - random.randint(-50, 50), self.vertexes_coordinates[v_from].y - (self.vertexes_coordinates[v_from].y - self.vertexes_coordinates[v_to].y) / 2 - random.randint(-50, 50))
def __init__(self, tuples): points = [Coordinate(x, y) for x, y in tuples] self.points = points min_y = min([p.y for p in self.points]) min_x = min([p.x for p in self.points]) max_y = max([p.y for p in self.points]) max_x = max([p.x for p in self.points]) center = Coordinate((max_x + min_x) / 2, (max_y + min_y) / 2) self.min_y, self.min_x, self.max_y, self.max_x, self.center = min_y, min_x, max_y, max_x, center self.points = self.order_points(self.points) self.polygon_vertices = [] for point in self.points: self.polygon_vertices.append(Vertex(point=point))
def finish_edge(self, edge): # Start should be a breakpoint start = edge.get_origin(y=2 * (self.min_y - self.max_y), max_y=self.max_y) # TODO: check if this is correct # End should be a vertex end = edge.twin.get_origin(y=self.min_y - self.max_y, max_y=self.max_y) # Get point of intersection point = self.get_intersection_point(end, start) # Create vertex v = Vertex(point=point) v.incident_edges.append(edge) edge.origin = v self.polygon_vertices.append(v) return edge
def generateFullMaze(width, height): g = Graph() for i in range(0, width * height): v = Vertex(i) g.addVertex(v) # Create a fully connected maze graph for i in range(0, width * height): # Connect up if i > width - 1: g.addEdge(g.getVertex(i), g.getVertex(i - width)) # Connect down if i < (width * height) - width: g.addEdge(g.getVertex(i), g.getVertex(i + width)) # Connect right if i % width != (width - 1): g.addEdge(g.getVertex(i), g.getVertex(i + 1)) return g
def add_vertex(self, name: str, x: float = None, y: float = None, *, save: bool = True, shadowed: bool = False): if name not in self.vertexes_coordinates: if x is None: x = random.randint(-500, 500) if y is None: y = random.randint(-500, 500) if save: # данное условие необходимо для того, что не было повторного сохранения при откате # или повторении действия, т.к. оно уже сохранено self.save_action(self.ADD_VERTEX, vertex_name=name, x=x, y=y) self.vertexes_coordinates[name] = Vertex(name, x, y) self.vertexes[name] = {} if not shadowed: self.signals.update.emit()
def __init__(self, name): Vertex.__init__(self, name) self.dist = None # current shortest path distance self.pi = None # parent for shortest path
def reset(self): Vertex.reset(self) self.dist = None self.pi = None
def undo(self): # если нечего отменять выходим if self.__history_counter == 0: return False # достаем данные из истории act_list = self.__history[self.__history_counter - 1] # выполняем действие обратное выполненому if self.ADD_EDGE == act_list[0]: # [action_code, vertex_name, following_vertex_name, weight, node] self.del_edge(act_list[1], act_list[2], act_list[3], act_list[4], False) if not self.oriented: self.del_edge(act_list[2], act_list[1], act_list[3], act_list[4], False) elif self.ADD_VERTEX == act_list[0]: # [action_code, vertex_name, x, y] self.del_vertex(act_list[1], False) elif self.DEL_EDGE == act_list[0]: # [action_code, vertex_name, following_vertex_name, weight, node] self.add_edge(act_list[1], act_list[2], act_list[3], act_list[4], False) elif self.DEL_VERTEX == act_list[0]: # [action_code, vertex_name, x, y, copy(vertex_row), copy(related_vertex)] self.vertexes_coordinates[act_list[1]] = Vertex( act_list[1], act_list[2], act_list[3]) self.vertexes[act_list[1]] = copy(act_list[4]) for v_name, w_list in act_list[5].items(): self.vertexes[v_name][act_list[1]] = copy(w_list) elif self.SET_ALL_EDGES == act_list[0]: # [action_code, vertex_name, following_vertex_name, copy(edges), weight, node] if act_list[3] is not None: self.vertexes[act_list[1]][act_list[2]] = act_list[3] if not self.oriented: self.vertexes[act_list[2]][act_list[1]] = act_list[3] else: self.vertexes[act_list[1]].pop(act_list[2]) if not self.oriented and act_list[1] in self.vertexes[ act_list[2]]: self.vertexes[act_list[2]].pop(act_list[1]) elif self.SET_EDGE == act_list[0]: # [action_code, vertex_name, following_vertex_name, weight, new_weight] self.set_edge(act_list[1], act_list[2], act_list[4], act_list[3], False) if not self.oriented: self.set_edge(act_list[2], act_list[1], act_list[4], act_list[3], False) elif self.DEL_ALL_EDGES == act_list[0]: # [action_code, vertex_name, following_vertex_name, edges] self.vertexes[act_list[1]][act_list[2]] = act_list[3] if not self.oriented: self.vertexes[act_list[2]][act_list[1]] = act_list[3] elif self.CHANGE_ORIENT == act_list[0]: self.change_orient(act_list[2], act_list[1], act_list[3], act_list[4], False) # сдвигаем счетчик действия/состояния на пердыдущее self.__history_counter -= 1 self.signals.update.emit() return True