CS303E Homework 13

Instructor: Dr. Bill Young
Due Date: Friday, April 18, 2025 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 Colorado (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 circles. But it also requires a bit of ingenuity to get it right.

The flag has a height to width ratio of 2 to 3. I would suggest drawing yours at 400 x 600 pixels, or larger. You can find details about the flag design, including the colors, here: Colorado Flag Specification

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 filled circle. Finally, you'll need a filled triangle. You can decide exactly what functions you need, but you must have at least two, other than your main() function.

The Colorado flag is just several colored rectangles, one triangle, and two circles. Alternatively, you could draw the large C as some circular arcs and lines, but I think that is much, much harder. It's important to draw things in the right order. A later shape will cover over an earlier one. So you can draw the red circle, the white triangle to notch it, and then the gold circle, in that order. Of course, you should draw the background rectangles first. By the way, the white triangle is an isosceles triange with leftmost point at the center of the circle, an angle of 45 degrees at that point, and arms sufficient of clear the radius of the red circle.

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 Colorado flag on the flag design website linked above. Anywhere you need to refer to a color, you can just use the name, like myBlue, assuming you define myBlue as a tuple of three integers. BTW: white, red, blue, and gold 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 400 x 600): 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 ColoradoFlag.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 Colorado Flag. It must also contain a header with the following format:

# Assignment: HW13
# File: ColoradoFlag.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 Colorado flag is nothing but simple geometric shapes, along with some colors to fill them. You could draw it with nothing but polygons and circles; 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 Colorado flag in about an hour. Here's my drawing: Colorado Flag It's a bit harder than some past flag projects and easier than others. (But it's not nearly as hard as what 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, UK Flag (pretty tricky), and last semester the Minnesota 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.