111 lab7

Computer Science 111 – Fundamentals of Programming I

Programming Project 7

Due date: 11:59 PM Friday 6 November

In this project, you will solve some problems using Turtle graphics. You can find documentation for the Turtle graphics system on Python’s Web site or in the fileTurtlegraphicsforTk.pdf in today’s project7 directory.


Download, unzip, and copy the project7 directory to your account. This directory contains the files mondrian.py, turtleexamples.py, and turtle.cfg. The last file is a configuration file for initial settings that you can edit if you want different ones.

Launch idle3 from a terminal window in your project7 directory (right-click in its window). Now, you should be able to import turtle, create a Turtle object, and run methods with that object to draw pictures by running Python statements from the IDLE shell prompts. Some example statements:

from turtle import Turtle
grumpy = Turtle()
grumpy.pencolor(255, 255, 0)
print(grumpy.position(), grumpy.pencolor(), grumpy.width())

Close the turtle window when you are done with this part.

The turtleexamples module contains definitions of the functions discussed in class. View their documentation by importing turtleexamples and runninghelp(turtleexamples).

Then, run the following statements to try out the individual functions:

from turtleexamples import *
sneezy = Turtle()
drawSquare(sneezy, 50)
drawSquare(sneezy, 50)
drawPentagon(sneezy, 50)
drawFlower(sneezy, 50)

Close the turtle window when you are done with this part.
Finally, return to the terminal window from your project window and run the program

mondrian.py at the command prompt, as follows:python3 mondrian.py

You should see a drawing window pop up with a level 1 painting. Now run the same program again, as follows:

python3 mondrian.py 2

The value 2 is a command-line argument, which the program accepts as an optional input. Run it again with the command-line arguments 3, 4, and 6 (you can press the up-arrow key to retrieve the previous terminal command).

To Turn in

  1. The drawSquare and drawPentagon functions in the turtleexamples module exhibit a similar pattern of code. Extract this pattern into a new function, nameddrawShape. This function expects a turtle, the length of a side, and the number of sides as arguments, and draws the corresponding shape. Test your new function by using it to draw a triangle, a square, a pentagon, a hexagon, and an octagon. Be sure to include a docstring with your new function.
  2. The drawFlower function in the turtleexamples module draws a fixed number of squares. Modify this function to give it these two additional capabilities:
    1. Add a parameter, whose default value is 4, that allows the caller to specify the number of squares to draw in the flower.
    2. Add a parameter, whose default value is drawSquare (the function), which allows the caller to specify the shape used to compose the flower.

    Test your modified drawFlower function with different combinations, such as 6 and 10 pentagons, and 4 and 9 octagons.

  3. Add a new function named fillRectangle to the turtleexamples module. This function expects a turtle and the coordinates of the corner points of a rectangle as arguments x1, y1, x2, y2. The function should draw a filled rectangle in a randomly chosen color. Test this function using the appropriate code at the end of the module.
  4. Open the file mondrian.py from IDLE. This program includes a drawRectanglefunction, which draws the rectangles in wire frame style. To draw filled rectangles, use your fillRectangle function from Exercise 1 instead. Just import that function from the turtleexamples module and call it in your mondrian function.
  5. Modify the mondrian function so that it flips a coin to decide which part of the rectangle will be larger after a subdivision. In other words, there will now be two additional options, which place the larger subrectangle to the left or at the top of a vertical or horizontal subdivision, respectively. Hint: The current program uses division by 3 to get the smaller:larger ratio. Think of how to get 2/3 instead of 1/3 as the ratio.
  6. Although the turtle’s speed is set to its fastest value, it takes a long time to paint pictures when the level is greater than 6. You can mitigate this behavior by calling the tracer function to delay the drawing until all of the computations are finished, and then call the update function to force the drawing. You must import these functions from the turtle module, call tracer with an argument of False if the level is greater than 6 (before the top-level call of mondrian), and call update with no arguments if the level is greater than 6 (after the top-level call of mondrian returns). Test your program with larger levels, such as 10 or 15 (be sure your initial window size is quite large).

7. Extra credit (20 points, no help provided). The Koch snowflake is a fractal shape. At level 0, the shape is an equilateral triangle. At level 1, each line segment is split into four equal parts, producing an equilateral bump in the middle of each segment. Here are shots of these fractal shapes at levels 0, 1, and 2:

At the top level, the program uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level. The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. The function drawFractalLine is recursive. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with 1/3 of the given distance and angles that produce the given effect.

Write a program in the file koch.py that draws the Koch snowflake. Be sure to structure your code in a manner similar to that of your other programs, using helper functions where appropriate. For testing with large levels, use the tracer/update mechanism from Exercise 4 and a large window.

To think about (no turnin)

Do the Turtle method names – shape, forward, pencolor, width – follow the linguistic convention we mentioned in class? If not, how might you modify them?