CS303E Homework 13

Instructor: Dr. Bill Young
Due Date: Friday, April 19, 2024 at 11:59pm

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 Union Jack, flag of the United Kingdom (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. In fact, if you do it right, it only really requires two functions. But it also requires a bit of ingenuity to get it right.

You can find details about the flag design by Googling "flag of UK," but the main facts are these: The flag has a height to width ratio of 3 to 5. I would suggest drawing yours at 300 x 500 pixels, or larger. The details of the flag can be found on the following specification sheet.

One thing that's not completely clear from this sheet is that the diagonal axis is at an angle of 30.96 degrees from the horizontal axis. With that, you can figure out the rest of the dimensions, assuming your trigonometry isn't too rusty. OK, sorry to scare you! I don't assume you know trigonometry. So here are some lengths you might find helpful. Consider where the white and red diagonal stripe intersects the upper left corner of the flag. The intersection with the vertical border is approximately 3.5 and with the horizontal border is approximately 5.8 (using the units of the diagram above, which is 30 x 50). If you know that the stripe has width 6 (with components of 3 white, 2 red, 1 white), you should be able to figure out the rest. Ask if you have trouble. BTW: the red diagonals aren't continuous; that is, they are two separate pieces, not a complete bar.

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. You can decide what functions you need, but you must have some.

And if you think about it, the UK flag is just a bunch of colored polygons (rectangles, triangles, and other odd shapes). I did my drawing by defining basically two functions (in addition to main):

  drawPolygon( turtleName, listOfVertices )
  fillPolygon( turtleName, listOfVertices, color )
The first draws a closed polygon where the vertices are given as a list of pairs. Remember to return to the first pair on the list. The second function just calls the first, filling the polygon with the color provided.

It's much easier to draw the UK flag if you draw the components in the proper sequence. For example, the UK flag has a blue background. You could either draw that as eight small blue triangles (hard), or you can draw one large blue rectangle (easy). If you draw the red and white elements after you draw the blue background, they'll hide the parts of the background that you don't want to show. The other elements are much easier if you think carefully about the order of placing the white and red components. For example, you don't have to think nearly as carefully about the vertices of the diagonal red stripes if you realize that the inner portions will be occluded (covered) by the vertical and horizontal white stripes. So why not draw them all the way to the center point of the flag; that's a lot easier to plot than the visible corners of each red stripe. Here's what that looks like (prior to the white and red vertical and horizontal stripes): Partial flag drawing. Notice that all four have as a common point the center point of the diagram.

Colors: Note that you can specify a Turtle color as an RGB triple representing an encoding of the percentages of red, green, and blue in the color. For example, here are the colors of the UK flag (sources varied slightly on these):

myBlue  = (0, 36, 125)
myRed   = (207, 20, 43)
myWhite = (255, 255, 255)
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, blue and red are predefined colors in Python but, except for white, are probably not the exact colors of the flag. Make sure you're in the right color mode; some modes use a different encoding.

One problem that you may encounter is that you'll get extraneous black lines outlining your various polygons. To deal with this, set the pencolor when drawing the polygon to be the same as the fill color of the polygon you're drawing.

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. Then set the pencolor to white and move to the next position along the boundary. Then, any ghost lines will at least be white lines along the boundary. 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 UKFlag.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 UK Flag. It must also contain a header with the following format:

# File: UKFlag.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 UK flag is nothing but some polygons, along with some colors to fill them.

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: polygons, stars (though those are just polygons too), circles, and a few others. 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 UK flag in about an hour. It's a bit harder than some past flag projects, but not as hard as what 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, and the Suriname 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.

BTW: notice that you can almost entirely do the current assignment with a single makePolygon function. You could define separate functions to draw triangles, squares, rectangles, stars, etc. But all of those are just special cases of polygons. This illustrates the value in finding the right abstraction. Often, by putting in a bit of thought before you start coding, you can save yourself a lot of work. And more important, you can generate code that is easier to understand and maintain. If you need a function for rectangles or triangles, for example, you can always code those as special cases of makePolygon.

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.