(tri, welding_edge, fringe, grid, skeleton)
return False
If possible, welds the triangle ``tri`` to ``fringe`` and
        returns ``False``.  If this method encounters a degenerate
        situation in the fringe and corrects it such that a restart of
        the search is required, it returns ``True`` (which means that
        a restart in finding triangle weldings is required).

        A degenerate situation is a situation when an edge listed in
        the fringe does not belong to the visual boundary of the
        diagram.
If possible, welds the triangle ``tri`` to ``fringe`` and
        returns ``False``.  If this method encounters a degenerate
        situation in the fringe and corrects it such that a restart of
        the search is required, it returns ``True`` (which means that
        a restart in finding triangle weldings is required).
If possible welds the triangle tri to fringe and returns False . If this method encounters a degenerate situation in the fringe and corrects it such that a restart of the search is required it returns True ( which means that a restart in finding triangle weldings is required ) .
def _weld_triangle(tri, welding_edge, fringe, grid, skeleton):
        """
        If possible, welds the triangle ``tri`` to ``fringe`` and
        returns ``False``.  If this method encounters a degenerate
        situation in the fringe and corrects it such that a restart of
        the search is required, it returns ``True`` (which means that
        a restart in finding triangle weldings is required).

        A degenerate situation is a situation when an edge listed in
        the fringe does not belong to the visual boundary of the
        diagram.
        """
        a, b = welding_edge
        target_cell = None

        obj = DiagramGrid._other_vertex(tri, (grid[a], grid[b]))

        # We now have a triangle and an edge where it can be welded to
        # the fringe.  Decide where to place the other vertex of the
        # triangle and check for degenerate situations en route.

        if (abs(a[0] - b[0]) == 1) and (abs(a[1] - b[1]) == 1):
            # A diagonal edge.
            target_cell = (a[0], b[1])
            if grid[target_cell]:
                # That cell is already occupied.
                target_cell = (b[0], a[1])

                if grid[target_cell]:
                    # Degenerate situation, this edge is not
                    # on the actual fringe.  Correct the
                    # fringe and go on.
                    fringe.remove((a, b))
                    return True
        elif a[0] == b[0]:
            # A horizontal edge.  We first attempt to build the
            # triangle in the downward direction.

            down_left = a[0] + 1, a[1]
            down_right = a[0] + 1, b[1]

            target_cell = DiagramGrid._choose_target_cell(
                down_left, down_right, (a, b), obj, skeleton, grid)

            if not target_cell:
                # No room below this edge.  Check above.
                up_left = a[0] - 1, a[1]
                up_right = a[0] - 1, b[1]

                target_cell = DiagramGrid._choose_target_cell(
                    up_left, up_right, (a, b), obj, skeleton, grid)

                if not target_cell:
                    # This edge is not in the fringe, remove it
                    # and restart.
                    fringe.remove((a, b))
                    return True
        elif a[1] == b[1]:
            # A vertical edge.  We will attempt to place the other
            # vertex of the triangle to the right of this edge.
            right_up = a[0], a[1] + 1
            right_down = b[0], a[1] + 1

            target_cell = DiagramGrid._choose_target_cell(
                right_up, right_down, (a, b), obj, skeleton, grid)

            if not target_cell:
                # No room to the left.  See what's to the right.
                left_up = a[0], a[1] - 1
                left_down = b[0], a[1] - 1

                target_cell = DiagramGrid._choose_target_cell(
                    left_up, left_down, (a, b), obj, skeleton, grid)

                if not target_cell:
                    # This edge is not in the fringe, remove it
                    # and restart.
                    fringe.remove((a, b))
                    return True

        # We now know where to place the other vertex of the
        # triangle.
        offset = DiagramGrid._put_object(target_cell, obj, grid, fringe)

        # Take care of the displacement of coordinates if a row or
        # a column was prepended.
        target_cell = (target_cell[0] + offset[0],
                       target_cell[1] + offset[1])
        a = (a[0] + offset[0], a[1] + offset[1])
        b = (b[0] + offset[0], b[1] + offset[1])

        fringe.extend([(a, target_cell), (b, target_cell)])

        # No restart is required.
        return False
def _weld_triangle ( tri , welding_edge , fringe , grid , skeleton ) : a , b = welding_edge target_cell = None obj = DiagramGrid . _other_vertex ( tri , ( grid [ a ] , grid [ b ] ) ) # We now have a triangle and an edge where it can be welded to # the fringe.  Decide where to place the other vertex of the # triangle and check for degenerate situations en route. if ( abs ( a [ 0 ] - b [ 0 ] ) == 1 ) and ( abs ( a [ 1 ] - b [ 1 ] ) == 1 ) : # A diagonal edge. target_cell = ( a [ 0 ] , b [ 1 ] ) if grid [ target_cell ] : # That cell is already occupied. target_cell = ( b [ 0 ] , a [ 1 ] ) if grid [ target_cell ] : # Degenerate situation, this edge is not # on the actual fringe.  Correct the # fringe and go on. fringe . remove ( ( a , b ) ) return True elif a [ 0 ] == b [ 0 ] : # A horizontal edge.  We first attempt to build the # triangle in the downward direction. down_left = a [ 0 ] + 1 , a [ 1 ] down_right = a [ 0 ] + 1 , b [ 1 ] target_cell = DiagramGrid . _choose_target_cell ( down_left , down_right , ( a , b ) , obj , skeleton , grid ) if not target_cell : # No room below this edge.  Check above. up_left = a [ 0 ] - 1 , a [ 1 ] up_right = a [ 0 ] - 1 , b [ 1 ] target_cell = DiagramGrid . _choose_target_cell ( up_left , up_right , ( a , b ) , obj , skeleton , grid ) if not target_cell : # This edge is not in the fringe, remove it # and restart. fringe . remove ( ( a , b ) ) return True elif a [ 1 ] == b [ 1 ] : # A vertical edge.  We will attempt to place the other # vertex of the triangle to the right of this edge. right_up = a [ 0 ] , a [ 1 ] + 1 right_down = b [ 0 ] , a [ 1 ] + 1 target_cell = DiagramGrid . _choose_target_cell ( right_up , right_down , ( a , b ) , obj , skeleton , grid ) if not target_cell : # No room to the left.  See what's to the right. left_up = a [ 0 ] , a [ 1 ] - 1 left_down = b [ 0 ] , a [ 1 ] - 1 target_cell = DiagramGrid . _choose_target_cell ( left_up , left_down , ( a , b ) , obj , skeleton , grid ) if not target_cell : # This edge is not in the fringe, remove it # and restart. fringe . remove ( ( a , b ) ) return True # We now know where to place the other vertex of the # triangle. offset = DiagramGrid . _put_object ( target_cell , obj , grid , fringe ) # Take care of the displacement of coordinates if a row or # a column was prepended. target_cell = ( target_cell [ 0 ] + offset [ 0 ] , target_cell [ 1 ] + offset [ 1 ] ) a = ( a [ 0 ] + offset [ 0 ] , a [ 1 ] + offset [ 1 ] ) b = ( b [ 0 ] + offset [ 0 ] , b [ 1 ] + offset [ 1 ] ) fringe . extend ( [ ( a , target_cell ) , ( b , target_cell ) ] ) # No restart is required. return False