def check_collisions(x_location, y_location, graph, node_size, extra_space): """ :param x_location: :param y_location: :param graph: :param node_size: :param extra_space: :return: """ temp_node = NodeObject('0', {'x': x_location, 'y': y_location}, node_size) collision = False for node in graph.node_list: if temp_node.distance(node) < node_size + node.size + extra_space * 2: collision = True return collision
def test_distance_between_nodes(self): node1 = NodeObject(serial=1, location={'x': 1, 'y': 3}, size=1) node2 = NodeObject(serial=2, location={'x': 5, 'y': 6}, size=1) actual_distance = 5 assert node1.distance(node2) == actual_distance
def get_partly_visible_edge(self, edge, top, bottom, left, right, node, edge_equation): """ :param edge: an edge that can be seen onscreen but where at least one node is not visible :param top: equation representing the top border of the screen :param bottom: equation representing the bottom border of the screen :param left: equation representing the left border of the screen :param right: equation representing the right border of the screen :param node: the visible node connected to the edge, or None if no node is visible :param edge_equation: the equation of the checked edge :return: A tuple containing two NodeObjects, each representing a one of the edge's nodes, the edge's slope and the edge's equation. If one of the edge's nodes is not onscreen, the x,y coordinates represent the intersection between the edge and the screen, a new serial is created, size is set to 0 and real is set to False. """ first_node = None second_node = None if node: first_node = self.original_graph.get_node_by_serial(node.serial) # check if edge collides with top border if LineEquation.check_collision_point(edge_equation, top): col_point = LineEquation.get_equation_collision_point( edge_equation, top) location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)} if first_node is not None: second_node = NodeObject(serial=get_serial(), location=location, size=0) second_node.real = False else: first_node = NodeObject(serial=get_serial(), location=location, size=0) first_node.real = False # check if edge collides with bottom border if LineEquation.check_collision_point(edge_equation, bottom): col_point = LineEquation.get_equation_collision_point( edge_equation, bottom) location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)} if first_node is not None: second_node = NodeObject(serial=get_serial(), location=location, size=0) second_node.real = False else: first_node = NodeObject(serial=get_serial(), location=location, size=0) first_node.real = False # check if edge collides with left border if LineEquation.check_collision_point(edge_equation, left): col_point = LineEquation.get_equation_collision_point( edge_equation, left) location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)} if first_node is not None: second_node = NodeObject(serial=get_serial(), location=location, size=0) second_node.real = False else: first_node = NodeObject(serial=get_serial(), location=location, size=0) first_node.real = False # check if edge collides with right border if LineEquation.check_collision_point(edge_equation, right): col_point = LineEquation.get_equation_collision_point( edge_equation, right) location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)} if first_node is not None: second_node = NodeObject(serial=get_serial(), location=location, size=0) second_node.real = False else: first_node = NodeObject(serial=get_serial(), location=location, size=0) first_node.real = False if second_node is None: if first_node is None: return None else: raise Exception( "Only One viable node for onscreen edge: {}".format( edge.print_by_serial())) min_dist = edge.node1.get_radius() / 2 if first_node.distance(second_node) < min_dist: return None if first_node.x < second_node.x: curr_edge = (first_node, second_node, edge.slope, edge_equation) else: curr_edge = (second_node, first_node, edge.slope, edge_equation) return curr_edge