CS8, Summer 2009

lab03: ("lab three")
Test-driven development
meets turtle graphics, and stars are born

Goals for this lab

This lab reinforces some ideas from lab01 and lab02

Step by Step Instructions

Step 0: Get together with your pair partner

You may choose your own pair partner for lab03/lab04 if all of the following are true:

  1. You already registered a pair partner on the forum called M10: Pair Partners for Lab03/Lab04 before midnight on Wednesday
  2. You fill out W05 (html or pdf) with your new partner before arriving at lab
  3. Both of you show up at your assigned lab time, on time, with W05 ready to hand in.

Otherwise, you'll be assigned a new pair partner for Lab03/Lab04.

In that case, please complete Worksheet W05 (html or pdf) with your new pair partner and turn it in to your TA, and make a posting to the M10: Pair Partners for Lab03/Lab04 before proceeding with the rest of the lab.

Step 1: Make sure you both understand the Unit Circle

Both you and your pair partner need to understand the basic concepts of the Unit Circle in this section.

Read through them together, and make sure you do.

In this lab, you'll be working with the unit circle, a concept from trigonometry that will help us in drawing polygons and stars using Turtle Graphics.

I will assume you are familiar with the unit circle, and might just need a refresher course. (If you are truly seeing the unit circle for the first time, you may want to come to your instructors office hours for a crash course.)

Unit Circle Basics

The unit circle is a circle with radius 1—i.e. one unit of measurement, e.g. 1cm, or 1m, or 1inch—hence the name unit circle. (Sometimes, instead, we draw a unit circle with radius r, where r is a variable.)

The unit circle is labeled with angles that range from 0 degrees to 360 degrees (which is all the way around the circle.) However, we typically label the unit circle with angles in radians instead of degrees. This angles range from 0 radians to 2π radians. All the way around the circle is 2π radians.

Unit circle labelled in degrees Unit circle labelled in Radians

The points on the unit circle at 0, π/2, π and 3π/2 are (1,0), (0,1), (-1,0), and (0,-1), respectively, as shown in the left hand figure below. The right hand figure shows that sometimes we draw circle where the radius is given by r, instead of being exactly 1.

Unit circle with four main points Unit circle with four main points and radius r

Finding a point on the unit circle

We can find the (x,y) coordinates any point on the unit circle if we know the angle, by using some simple trigonometry properties—in particular, the first two of the well known SOHCAHTOA formulas:

With these in mind, we can see that in the pictures below:

cos is adjacent/hypotenuse, which is x/r, therefore: cos (θ)  = x/r, and therefore x = r cos (θ)
sin is opposite/hypotenuse, which is y/r, therefore: sin (θ)  = y/r, and therefore y = r sin (θ)    

Angle on unit circle 1 Angle on unit circle 2
Angle on unit circle 3  


Now consider just four more pictures of the unit circle—I promise, these are the ones that will lead us into the Python coding!


3 points distributed evenly around the unit circle 5 points distributed evenly around the unit circle
Unit circle, 3 points Unit circle, 5  points
6 points distributed evenly around the unit circle 8 points distributed evenly around the unit circle
Unit circle, 6  points Unit circle, 8 points


The key points you must understand before going further

To be able to answer those last questions, we will write two Python functions called:

You are now ready to look at some Python code!


Step 4: Decide whose account you are going to work in

Note: DO NOT share passwords with each other. That is a violation of your account agreement, and can result in suspension of your computing privileges at UCSB.

Instead, what you should do is:

Here's how to copy files from one account to another:

Let's suppose that you did all the work in person1's account, and you now want to copy that into person2's account.

  1. Person2 should log in. You can log in on a separate computer, or just use one of the terminal windows and use the command: ssh person2@csil.cs.ucsb.edu
  2. Person2 cd's into their ~/cs8 directory
  3. Person2 types this command
    scp -r person1@csil.cs.ucsb.edu:cs8/lab03 lab03.snapshot.date.time
    • replace person1 with person1's CSIL username (e.g. jsmith)
    • replace date.time with the date and time (e.g. Aug12.1.15pm)
    • don't include any slashes or spaces in the date.time string—it must be a valid part of a filename--periods and dashes are ok though.
    • Example: scp -r fredjones@csil.cs.ucsb.edu:cs8/lab03 lab03.snapshot.aug12.1.15pm
  4. After you type this command, you'll be prompted for a password. Person1 should type his/her password.
    • Don't share the password, just have person1 type it in person2's window.
  5. If it works, there will now be a directory called lab03.snapshot.date.time in person2's directory that is a complete copy of the ~/cs8/lab03 directory from person1's account.
    • Please read the "final thoughts" below so that you understand what the proper use of this process it—it is a powerful command, but it has the potential to be abused. With great power comes great responsibility.

It is only necessary for one of you to submit the work via turnin, as long as both names are in each file.

But, if you are the person whose account is not the one from which the turnin step is done, be SURE that your name appears! You need to work carefully with your pair partner to avoid misunderstandings and problems.

Some final thoughts on this step:

Step 5: Create a directory for lab03 on one of your accounts.

First, create a directory (folder) called lab03, inside your cs8 folder, which is in turn, inside your home directory.

A short way to say that is: create a directory ~/cs8/lab03

There are two ways to do it:

If you've forgotten how, consult the instructions for lab00. If you still can't figure it out, you may ask a classmate, or the TA or instructor for help.

(Note: This is the last time you'll get the detailed version—next lab, you'll just get the instruction "create a ~/cs8/lab04" directory, and you'll be expected to know that to do.)

Reminder, for midterm 2:

For midterm 2: be able to explain the difference between ~pconrad/cs8 and ~/pconrad/cs8
It is explained at the corresponding step to this one in lab02.

Step 6: Bring up IDLE, and a window for function definitions

To bring up IDLE, type this command in the Terminal window, just like we did last week:


When you do, you should see the IDLE program come up. The window that appears should have the Python Command prompt (>>>) in it.

Next, in IDLE, select "File=>New Window" to open a new "untitled" window for Python code.

When it comes up, click and drag the window by its title bar over to the right of your Python Shell window.

Here's what that looks like:

Selecting File=>New Window The "untitled" window appearing Dragging the windows so they are side-by-side
Selecting File => New Window in IDLE New window appears New windows side by side

Once you've opened that file, add a comment to the top of your file like this one (substituting your own name and the current date in the proper spot):

# lab03.py for CS8 lab03,  08/12/2009
# By Agnes Nitt and Jason Ogg, based on functions originally by P. Conrad
# Some functions to draw stars and polygons

Then, save it under the name lab03.py

Note: This is the last time you'll get detailed instructions about this too! Next week, I'll just say: open IDLE, bring up the New Window, save the file (substituting your own name), and start by putting a header comment at the top, with your name(s), date, the name of the assignment, and a one line description of what the file is for.

Now proceed with the next step.

Step 7. Copy my starting point file into your file

The starting point for this week's lab can be found at this link:


Open that link, and copy the contents into the window you opened for your lab03py file.

Adjust the lines at the top

Near the top of the file, there are some lines such as:

import sys; sys.path.append("/Users/Shared/cs8")


import sys; sys.path.append("C:/cs8")

These lines may need to be commented or uncommented depending on whether you are

Read the instructions, and do what is needed before running the file

Step 8. Run the file for the first time

Then, choose Run => Run Module, and see the output.

It should look something like this:

>>> ================================ RESTART ================================
Test ithOfNPointsOnCircleX test 1 failed! expected result: 1 actual result: -1.0
|difference|: -2.0 tolerance: 0.001 Test ithOfNPointsOnCircleX test 2 failed! expected result: 0 actual result: -1.0
|difference|: -1.0 tolerance: 0.001 Test ithOfNPointsOnCircleX test 3 passed (within tolerance of: 0.001) Test ithOfNPointsOnCircleX test 4 failed! expected result: 0 actual result: -1.0
|difference|: -1.0 tolerance: 0.001 Test ithOfNPointsOnCircleX test 5 failed! expected result: 1 actual result: -1.0
|difference|: -2.0 tolerance: 0.001 Test ithOfNPointsOnCircleX test 6 failed! expected result: -0.5 actual result:
-1.0 |difference|: -0.5 tolerance: 0.001 Test ithOfNPointsOnCircleX test 8 passed (within tolerance of: 0.001) Test ithOfNPointsOnCircleY test 1 failed! expected result: 0 actual result: -1.0
|difference|: -1.0 tolerance: 0.001 Test ithOfNPointsOnCircleY test 8 failed! expected result: 1.73205080757 actual
result: -1.0 |difference|: -2.73205080757 tolerance: 0.001 >>>

We see that there are a bunch of tests, and all of them are failing.

So, based on our experience in lab02, we know what to do—we can get right down to work. Here's what to do:

  1. In the file, find the stub of the ithOfNPointsOnCircleX() function.
  2. Directly under it, you'll see some test cases—but not all the test cases are complete. Test case number 7 for the ithOfNPointsOnCircleX needs you to finish it, using your knowledge of the unit circle.
  3. Then, run the file again, and you'll see that ithOfNPointsOnCircleX now has eight test cases. They are still all failing, but you should have a much better idea of how to fill in the formula, now that you've figured out one of the test cases for yourself.
  4. So, now, replace the stub of ithOfNPointsOnCircleX with the correct formula. Now all eight test cases should pass.
  5. Do the same for ithOfNPointsOnCircleY: first fix up all the test cases that need fixing up.
  6. Then, and only then, replace the stub with the correct formula. Now that all your test cases are passing, you are almost ready for the graphics part of this lab!
  7. Before you move on, though, take a moment to clean up any @@@ type comments between the start of the file, and the end of the test cases for ithOfNPointsOnCircleY. You may like to use the Edit / Find menu option to look for @@@. This can make finding these a lot easier.

We are ready for Step 9.

Step 9: Finishing up the drawPolygon function

Now, locate the drawPolygon() function. As discussed in lecture, this function is almost, but not quite complete.

First take a moment to read over the function and understand how it works

Just below the drawPolygon function, there is a tryIt() function. This is a function that starts out with every line commented out except for the one that creates a Turtle named Sheila.

You can "uncomment" one line at a time to see various polygons being drawn. Uncomment one of the lines, run the file and try typing tryIt() at the Python Shell Prompt (>>>).

You'll notice that you get a polygon that is missing one side.

Now, fix the drawPolygon function. There are at least two ways to go about it:

Once drawPolygon is working, move on to drawStar. Try uncommenting the lines in tryIt() one at a time that call drawStar(). You should that the call when n=3 does nothing, but the call when n=5 or n=6 will give you a five or six pointed star.

Once that is all working, you are ready for step 10

Step 10: Generalizing the drawStar function

The drawStar function has one drawback: although (0,0) doesn't exactly appear in the function, in a sense the "idea" that the star is drawn centered at (0,0) is nevertheless hard coded in the drawStar function. To make the function more general, what we need to do is add parameters x and y, and then add those values in, every time we make a call to the "goto" function of the Turtle.

Find the place in the file where there is a comment indicating you should add the function drawStarAtXY().

Read the comment, and then follow the instructions there to create this function.

Once you've coded it, you can test it by running the function:


If it works, you should see the stars that appear in this picture:

testDrawStarsAtXY output


When you get this, you may move on to the next step—but first, do a visual inspection of your code to remove any remaining @@@ comments in the part you've finished.

Step 10: Generalizing the drawPolygon function

Now, do exactly the same thing for drawPolygon that you did for drawStar. You'll find comments for a drawPolygonAtXY() function, and a testDrawPolygonAtXY() function waiting for you in the file.

The finished product should look like this:

testDrawPolygonAtXY output

When you get this, you may move on to the next step—but first, do a visual inspection of your code to remove any remaining @@@ comments in the part you've finished.

Step 13: Uncomment the call to the go() function

You'll see a go() function near the bottom of the file that just calls the two test functions you've already been working with. There is a function call to go() already there. Just uncomment and test one last time. You should get this as your output:

Finished product

Step 14: Check over your program to see if it is ready to submit

Before you submit your assignment, check these things:

Step 15 : Submit your assignment using the turnin program on CSIL

To submit your assignment, you need to bring up a terminal window on CSIL.

Each member of a pair (or trio) should submit the same file—you can use email to transmit the file between your accounts.

Later, I'll show you a more effective way: using the scp command to copy files directly from one account to another.

Evaluation and Grading Rubric (250 pts)

Due Date: Thursday, August 26, 4:30pm

But: you are encouraged to finish it sooner!
In particular, you are encouraged to finish it before the 2nd midterm exam on Tuesday August 24th.

Completing this lab before Midterm 2 is one of the most important things you can do to study for the midterm.

Copyright 2010, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.