Exemple #1
0
    def draw(stack):
        '''Draws an image of a stack. Stacks are drawn vertically and top-down.

        Args:
            stack: The stack to be drawn.

        Returns:
            A PIL image representing the current state of the stack.
        '''
        bg_color        = '#FFFFFF'
        fg_color1       = '#000000'
        fg_color2       = '#000000'
        padding_x       = 25
        padding_y       = 25
        text_padding    = 20
        cell_count      = len(stack)
        cell_width      = 100
        cell_height     = 60
        arrow_size      = 35
        right_gap        = 100
        width           = (padding_x * 2) + cell_width + right_gap
        height          = (padding_y * 2) + (cell_height * cell_count) + text_padding

        image = Image.new('RGB', (width, height), bg_color)
        draw = ImageDraw.Draw(image)

        ## Draw header
        util.draw_text_centered(
            draw,
            stack.name,
            [0, width, 0, padding_y + text_padding],
            color=fg_color1,
            bold=True
        )

        if stack.is_empty():
            return image

        ## Draw cells
        xy = [
            padding_x,
            padding_y + text_padding,
            padding_x + cell_width,
            padding_y + cell_height + text_padding
        ]
        for value in stack:
            util.draw_rectangle_with_text(
                draw,
                str(value),
                xy
            )
            xy[1] += cell_height
            xy[3] += cell_height

        ## Draw arrow pointing to top of stack
        tail = (width - padding_x - 40, padding_y + text_padding + (cell_height // 2))
        util.draw_arrow(draw, tail, 50, 'left')
        util.draw_text(draw, 'TOP', (tail[0]+10, tail[1]-9))

        return image
Exemple #2
0
    def draw(tree):
        '''Draws an image of an AVL Tree.

        Args:
            tree: The tree to be drawn.

        Returns:
            A PIL image representing the current state of the tree.
        '''
        width, height = AVLTreeDrawer.WIDTH, AVLTreeDrawer.HEIGHT
        bg_color = AVLTreeDrawer.BACKGROUND_COLOR
        image = Image.new('RGB', (width, height), bg_color)
        draw = ImageDraw.Draw(image)

        header_height = AVLTreeDrawer.HEADER_HEIGHT

        ## Draw header
        util.draw_text_centered(
            draw,
            tree.name,
            [0, width, 0, header_height],
            color='#000000',
            bold=True
        )

        if tree.is_empty():
            return image

        ## Draw nodes



        return image
Exemple #3
0
    def draw(deque):
        '''Draws an image of a double ended queue.

        Args:
            deque: The deque to be drawn.

        Returns:
            A PIL image representing the current state of the deque.
        '''
        bg_color = '#FFFFFF'
        fg_color1 = '#000000'
        fg_color2 = '#000000'
        padding_x = 25
        padding_y = 25
        text_padding = 20
        cell_count = len(deque)
        cell_width = 70
        cell_height = 60
        bottom_gap = 50
        width = (padding_x * 2) + (cell_width * cell_count)
        height = (padding_y * 2) + cell_height + text_padding + bottom_gap

        image = Image.new('RGB', (width, height), bg_color)
        draw = ImageDraw.Draw(image)

        ## Draw header
        util.draw_text_centered(draw,
                                deque.name,
                                [0, width, 0, padding_y + text_padding],
                                color=fg_color1,
                                bold=True)

        if deque.is_empty():
            return image

        ## Draw cells
        xy = [
            padding_x, padding_y + text_padding, padding_x + cell_width,
            padding_y + cell_height + text_padding
        ]
        for value in deque:
            util.draw_rectangle_with_text(draw, str(value), xy)
            xy[0] += cell_width
            xy[2] += cell_width

        ## Draw arrow pointing to front of queue
        tail = (padding_x + (cell_width // 2),
                height - padding_y - (text_padding // 2))
        util.draw_arrow(draw, tail, 30, 'up')
        util.draw_text(draw, 'FRONT', (tail[0] - 20, tail[1] + 5))

        ## Draw arrow pointing to rear of quuee
        if len(deque) > 1:
            tail = (width - (padding_x + (cell_width // 2)),
                    height - padding_y - (text_padding // 2))
            util.draw_arrow(draw, tail, 30, 'up')
            util.draw_text(draw, 'REAR', (tail[0] - 20, tail[1] + 5))

        return image
        '''
Exemple #4
0
    def draw(heap):
        '''Draws an image of a heap.

        Args:
            heap: The heap to be drawn.

        Returns:
            A PIL image representing the current state of the heap.
        '''
        width, height = HeapDrawer.WIDTH, HeapDrawer.HEIGHT
        bg_color = HeapDrawer.BACKGROUND_COLOR
        image = Image.new('RGB', (width, height), bg_color)
        draw = ImageDraw.Draw(image)

        header_height = HeapDrawer.HEADER_HEIGHT

        ## Draw header
        util.draw_text_centered(draw,
                                heap.name, [0, width, 0, header_height],
                                color='#000000',
                                bold=True)

        if heap.is_empty():
            pass

        ## Draw nodes

        return image
Exemple #5
0
    def draw(tree):
        '''Draws an image of a binary search tree.

        Args:
            tree: The tree to be drawn.

        Returns:
            A PIL image representing the current state of the tree.
        '''
        bg_color = '#FFFFFF'
        fg_color1 = '#000000'
        fg_color2 = '#000000'
        padding_x = 25
        padding_y = 25
        text_padding = 20
        tree_width = BSTDrawer.calc_width(tree)
        tree_height = BSTDrawer.calc_height(tree)
        node_width = 60
        node_height = 60
        level_height = 120
        width = (padding_x * 2) + (tree_width * 100)
        height = (padding_y * 2) + (level_height * tree_height)

        image = Image.new('RGB', (width, height), bg_color)
        draw = ImageDraw.Draw(image)

        ## Draw header
        util.draw_text_centered(draw,
                                tree.name,
                                [0, width, 0, padding_y + text_padding],
                                color=fg_color1,
                                bold=True)

        ## Draw nodes
        start_coords = (200, 100)
        BSTDrawer.draw_node(draw, tree.get_root(), start_coords, level_height,
                            0)

        return image
Exemple #6
0
    def draw(linked_list):
        '''Draws an image of a singly linked list.

        Args:
            linked_list: The linked list to be drawn.

        Returns:
            A PIL image representing the current state of the linked list.
        '''
        bg_color = '#FFFFFF'
        fg_color1 = '#000000'
        fg_color2 = '#000000'
        padding_x = 25
        padding_y = 25
        text_padding = 20
        cell_count = len(linked_list)
        cell_width = 100
        cell_height = 50
        node_width = 50
        next_width = 30
        gap_width = 20
        arrow_size = 35
        width = (padding_x * 2) + (cell_width *
                                   cell_count) + next_width + gap_width
        height = (padding_y * 2) + cell_height + text_padding

        image = Image.new('RGB', (width, height), bg_color)
        draw = ImageDraw.Draw(image)

        ## Draw header
        util.draw_text_centered(draw,
                                linked_list.name,
                                [0, width, 0, padding_y + text_padding],
                                color=fg_color1,
                                bold=True)

        ## Cease drawing if linked list is empty
        if linked_list.is_empty():
            return image

        ## Draw nodes
        xy = [
            padding_x, padding_y + text_padding, padding_x,
            padding_y + text_padding + cell_height
        ]
        for value in linked_list:
            ## Draw node with data
            xy[2] += node_width
            util.draw_rectangle_with_text(draw,
                                          str(value),
                                          xy,
                                          fill_color=bg_color,
                                          outline_color=fg_color1,
                                          text_color=fg_color2)
            ## Draw next box with arrow
            xy[0] += node_width
            xy[2] += next_width
            draw.rectangle(xy, fill=bg_color, outline=fg_color1)
            arrow_y = (xy[1] + xy[3]) // 2
            arrow_x = xy[0] + (next_width // 2)
            util.draw_arrow(draw, (arrow_x, arrow_y), arrow_size, 'right',
                            fg_color2)
            xy[0] += next_width
            xy[0] += gap_width
            xy[2] += gap_width
            xy[0] += cell_width - (node_width + next_width + gap_width)
            xy[2] += cell_width - (node_width + next_width + gap_width)

        ## Draw NULL cell
        null_width = xy[0] + (gap_width // 2)
        null_height = (xy[1] + xy[3]) // 2 - 10
        util.draw_text(draw,
                       'NULL', (null_width, null_height),
                       color=fg_color1)

        return image