CSCI 110 / Spring 2012

Survey of Computer Science

Lab 7

Due at the end of today's class, except by permission.

Problem 1.

In this problem, you will use a Turtle to make a self-similar drawing, the Sierpinski gasket. A self-similar drawing is a pattern that looks the same at different levels of magnification. The following picture shows a 3-level Sierpinski gasket:

Notice that this Sierpinski gasket is composed of three smaller versions of the Sierpinski gasket, one in each corner of the original triangle. Each of these smaller versions is half the size of the original, and made of up one less level. That is, a 3-level Sierpinski gasket is made up of three half-size 2-level Sierpinski gaskets. And each of those 2-level Sierpinski gaskets is of course made up of three one-quarter-size 1-level Sierpinski gaskets, and so on. A 0-level Sierpinski gasket is just a triangle. Thus, drawing the Sierpinski gasket is a problem that can be solved using recursion.

Write a recursive function, sierpinski(myTurtle, size, N), that draws an N-level Sierpinski whose sides are each of length size. Your function should call itself to draw the smaller versions of the Sierpinski gasket. When your function starts, the turtle will be in some position on the screen. If you have programmed the function correctly, then the turtle will end up in that same position when the function is finished.

Follow these steps to write your function. Note that each step involves exactly one Python statement (i.e. one line of code, one function call, or one if-else conditional statement).

You may use the following skeleton code for your program:


Problem 2.

In this problem, you will modify a small piece of a larger program. Download these two files:

If you run the paint.py program you will be shown a small window in which you can make drawings using the mouse. Try it out.

Your task is to modify the code in paint.py so that it can do a bucket-fill operation with the color blue. The idea is that the user should be able to double-click any white spot on the drawing canvas, and the program should fill that white spot, and all nearby white spots, with blue color. For instance, if the user draws this picture:

then double clicks in the middle part of the drawing, the result should look like this:

The existing program code already calls a function, fillWithColor(canvas, x, y), whenever the user double-clicks on the drawing canvas. This is the function you should modify. If you look in paint.py, you will see that the fillWithColor( ) function currently does not do a bucket-fill with blue paint. Instead, it just prints some silly messages and changes a single pixel at coordinates (x, y) to green. Delete the code in the body of this function and replace it with your own code that does the following:

Note: Each of the steps in the list above corresponds to just one Python statement. The only functions you need to know about (besides the one you are modifying) are:


What To Turn In.

Submit your program files to Moodle. Be sure that your name is in the program prologue's comment section of the program, and that you have put an appropriate comment before each function and made your program easy to read and understand.

Reminder.
Be sure to save a copy of each program on your P:\ drive or gmail, etc.
You are responsible for keeping a copy of every program until the graded assignment is handed back.