Stacks & Recursion

CS 3358

Due: Thursday 10/15/2015 11:55 pm

100 points


Stack implementation - 30 points

Stack implementation notes:

You will be implementing a generic (templetized) linked list-based stack. Here is the class declaration for Stack_3358. You may use list_3358.h and list_3358.cpp to get ideas about how to implement the the methods of the stack. However, note that the provided linked list is not generic. Also, note that the template method implementations will move to the .h file.

Paint Bucket Fill Tool (version 1) - 40 points

This program will simulate the Bucket Fill tool that you can find in popular image editors like Gimp and Photoshop.
Given an input file of the following format (a fake picture):

yyywwbbbbbbbggggg
yyybbbbbbbgggbbbb
ybybybybybwwwwwyy
ybbbbggwwwwwwbbbg
ggggggwwbbbbbbbbb
yyyyyyyyybbbyyyyy
ggggyyyygggggyyyy


that uses characters to represent colors in a picture, you will need to write a function that will "bucket fill" an area with another "color." For example. If I were to bucket fill the pixel at row 0 col 6 ("b") with a "P", I would get this:

yyywwPPPPPPPggggg
yyyPPPPPPPgggbbbb
yPyPyPyPyPwwwwwyy
yPPPPggwwwwwwbbbg
ggggggwwbbbbbbbbb
yyyyyyyyybbbyyyyy
ggggyyyygggggyyyy

Every pixel that has the same color and is connected to the area of the bucket fill is changed to the new color. Two pixels are connected if the are immediate neighbors either horizontally, vertically, or diagonally. A pixel can have up to eight neighbors.

Write a program that reads in a file (provided at the linux prompt) that is at most 25 rows and 25 columns and repeatedly prompts the user for a row and column number, and a "color".  The program will fill that area with the new color, show the new picture and prompt the user again.  The program will end when the user enters -1 for the row or column.

Example Run:

linux prompt> ./bucket_fill fake_picture.txt

yyywwbbbbbbbggggg
yyybbbbbbbgggbbbb
ybybybybybwwwwwyy
ybbbbggwwwwwwbbbg
ggggggwwbbbbbbbbb
yyyyyyyyybbbyyyyy
ggggyyyygggggyyyy

Enter a row: 0
Enter a column: 6
Enter a color: P

yyywwPPPPPPPggggg
yyyPPPPPPPgggbbbb
yPyPyPyPyPwwwwwyy
yPPPPggwwwwwwbbbg
ggggggwwbbbbbbbbb
yyyyyyyyybbbyyyyy
ggggyyyygggggyyyy


Enter a row: 1
Enter a column: 1
Enter a color: G

GGGwwPPPPPPPggggg
GGGPPPPPPPgggbbbb
GPGPyPyPyPwwwwwyy
GPPPPggwwwwwwbbbg
ggggggwwbbbbbbbbb
yyyyyyyyybbbyyyyy
ggggyyyygggggyyyy


Enter a row: -1
Enter a column: 1
Enter a color: G


Use the Stack implemented above to solve this problem. An example pseudocode solution for this problem is given below:
function bucket_fill(x,y)
    push initial pixel (x,y) to the stack
    while (stack is not empty)
        (x,y) = pop_stack
        change (x,y) to new color
        for each neighbor of (x,y) that has the old color
            push neighbor to the stack
        end
    end
end

Paint Bucket Fill Tool (version 2) - 30 points

Solve the previous problem using Recursion instead of a stack.

No loops in the bucket fill algorithm. You can use a loop to read in the original "picture" file and to prompt the user for the input. Obviously the Bucket Fill will have to be implemented in a function that can be called recursively.


Notes:

Turn in:  No hard copy source file turnin.

Submit: using TRACS


Last Updated: 10/6/15