CS303E Homework 13

Instructor: Dr. Bill Young
Due Date: Friday, November 22, 2024 at 11:59pm

Copyright © William D. Young. All rights reserved.

Some Practice with Turtle Graphics

From Wikipedia:
Computer pioneer Seymour Papert added support for turtle graphics to the Logo programming language in the late 1960s to support his version of the turtle robot, a simple robot controlled from the user's workstation that is designed to carry out the drawing functions assigned to it using a small retractable pen set into or attached to the robot's body. The use of turtle geometry mimics the actual movement logic of the turtle robot.

Today, the Python programming language's standard library includes a Turtle graphics module. Like its Logo predecessor, the Python implementation of turtle allows programmers to control one or more turtles in a two-dimensional space. Since the standard Python syntax, control flow, and data structures can be used alongside the turtle module, turtle has become a popular way for programmers learning Python to familiarize themselves with the basics of the language.

Using Turtle graphics is a great example of a pre-defined Python object-oriented library. Turtles are just objects, defined in a class; you define them and interact with them via they interface provided to move them around and change their attributes.

BTW: don't worry too much about memorizing all of the Turtle commands. They won't be on the final exam.

The Assignment

Using Turtle Graphics, draw the flag of Minnesota (see the picture above). Note that you have to actually draw it using Python turtle graphics; you can't just include an image of the flag downloaded from the web. I chose that flag because it's interesting to draw, consisting of several simple shapes: rectangles, a triangle, and a star. But it also requires a bit of ingenuity to get it right.

The flag has a height to width ratio of 3 to 5. I would suggest drawing yours at 300 x 500 pixels, or larger. You can find details about the flag design here: Details of flag. You must do this assignment in a modular fashion. That means defining functions to build a reusable drawing toolkit. For example, you might define a function to draw a rectangle with parameters of the coordinates of the top left corner, width, height, and color. Another function should be to draw a 8-point star or octogram (more on that below). You can decide what functions you need, but you must have some.

The Minnesota flag is just several colored rectangles, one triangle, and a star. Alternatively, it's two 5-sided polygons and a star. It's important to draw things in the right order. A later shape will cover over an earlier one. So you can place the white star and light blue triangle over the blue background.

Hint on drawing a 8-point star: You can't draw the star with 8 lines, as shown to the left. If you do that, it won't give you a properly enclosed area for filling (at least not on all implementations of Turtle graphics); you'll get something like what's shown on the right. Instead, draw the perimeter of the star with 16 line segments, all of equal length. You'll need to figure out the appropriate angles to turn to walk around the perimeter. Each point of the star is 45 degrees. Where one point intersects the next is 90 degrees. One way to think about it is as follows: imagine you were walking around a gigantic star painted on the ground. Walk down each line segment that makes up the star; as you reach the end of that line segment, you'll need to turn some number of degrees right or left. How many degrees? Is it more than 90 degrees or not? Think it through and you should be able to work it out. I found this calculator useful to figure out some of the lengths in a star: 8-point Star Calculator.

Colors: Note that you can specify a Turtle color as an RGB triple representing an encoding of the percentages (out of possible 255) of red, green, and blue in the color. You can find the exact colors of the Minnesota flag on the flag design website linked above. Anywhere you need to refer to a color, you can just use the name, like myBlue. After all, myBlue is just a variable whose value is a tuple of integers. BTW: white and blue are predefined colors in Python but, except for white, are not the exact color on the flag. Make sure you're in the right color mode; some modes use a different encoding.

Saving your picture: You don't have to save the image to a file, but it would be a good idea to learn how to do that, because you'll probably want to save your drawings, given all the time you put into making them. Here is the drawing my program does (drawn 600 x 1000): my flag drawing.

Notice: If you save the drawing, sometimes there are residual "ghost" lines where you moved from one location to another. You don't see them on your actual drawing screen but they show up on the saved version. I really don't know why this happens or how to get rid of them. The only thing I have found that works is to make sure at each step that you leave the turtle on the boundary of the flag or set the pen color to the color of the region you're crossing. You won't be penalized if your drawing has ghost lines, unless they are there because you didn't raise the pen when you should.

Turning In the Assignment:

The program should be in a file named MinnesotaFlag.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment hw13 under the assignments sections by uploading your Python file.

Running your file must bring up a window and render an image of the Minnesota Flag. It must also contain a header with the following format:

# File: MinnesotaFlag.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date:
# Description of Program: 

Programming Tips:

Use Functions: This assignment is a perfect example of the power of functional abstraction. It's possible to just brute force this without defining any functions. But that would be a big mistake. If you think about it, the Minnesota flag is nothing but some polygons, along with some colors to fill them. You could draw it with nothing but polygone; but it seems easier to me to have a richer collection of functions: rectangle, triangle, star, etc.

With a toolkit of a few basic drawing elements, you can create many different pictures. For example, I decided several semesters ago to assign the drawing of a different flag every semester for HW13. Many flags just contain a few standard elements. After you've defined functions for the basic elements, if you should decide to draw, e.g., the Texas Flag, the US flag, the Panama flag, or even the flags of South Africa or Turkey, it's a matter of minutes rather than hours. Because I had already drawn several other flags that utilize the same basic elements, I was able to program the Minnesota flag in about an hour. It's a bit harder than some past flag projects, but not as hard as what my past TA Dewayne suggested I assign: Turkmenistan flag.

In past semesters, classes have drawn the U.S. flag, German flag (too simple), Seychelles flag, Panama flag, Liberian Flag, Suriname Flag, and last semester the UK Flag. With appropriate functional abstraction, it's pretty easy. In one previous semester, Project 3 was to draw something quite a bit more complicated like the UT Tower. Again, it's not that hard after you build up a toolkit of basic shapes. But without that, it's a nightmare.

Without functional abstraction, it would be impossible to build the huge software systems that run the modern world. Learn to use it to your own advantage.

Using Libraries: Using Turtle graphics is a great example of using a Python object-oriented library that someone else defined. Thank goodness we didn't have to define it, because associating the drawing capabilities with the Turtle class requires some pretty sophisticated Python programming. As you undertake more complex Python programming projects, you'll find yourself doing complex things quite frequently: drawing a graph, analyzing a data set, processing a language. Chances are that someone has defined a Python library that does a lot of what you need. Why re-invent the wheel when someone else has already done the work for you! A good step before undertaking a large project is identifying relevant libraries that can help you.

Here are some popular libraries: Popular Python libraries. Of course, in this class we're learning the basics of programming. So except in very limited instances, you shouldn't use libraries unless we've explicitly said you can.