예제 #1
0
    def two_edges_are_one(self, edge_1, edge_2):
        """
        Checks if the two edges are actually a single edge.
        :return: True if edges are 100% the same one
        """

        eq1 = LineEquation(slope=edge_1[3].slope,
                           const=edge_1[3].const,
                           edge1=edge_1[0],
                           edge2=edge_1[1])
        eq2 = LineEquation(slope=edge_2[3].slope,
                           const=edge_2[3].const,
                           edge1=edge_2[0],
                           edge2=edge_2[1])


        # Check collision point
        collision_point = LineEquation.get_equation_collision_point(eq1, eq2)
        GLogger.log(logging.DEBUG, Utils.format_log_msg("Found collision point of both edges", point=collision_point, eq1=eq1, eq2=eq2))
        if collision_point == LINES_ALWAYS_MEET:
            # Lines have the same slope + const. Big change they are the same one.
            if LineEquation.check_collision_point(eq1, eq2):
                GLogger.log(logging.DEBUG, "Lines meet and intersect with each other - They are the same line")
                return True
            else:
                GLogger.log(logging.DEBUG, "Lines have the same parameters but we are not sure if they meet")
                return False
        return False
예제 #2
0
    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