Solutions 2.5: Turtle Circles and Arcs

Here are the solution to the exercises in Lesson 2.5: Turtle Circles and Arcs.

Exercise 1

Draw a 7-color rainbow using Python Turtle Graphics.

Solution

Rainbow is one of our favorite arcs. It is always so pretty with beautiful colors. To draw a simple rainbow, we need seven colors – red, orange, yellow, green, blue, indigo, and violet. We first draw the red arc. To draw the orange arc, we need to make the radius smaller by the line width and also move the starting point by line width. We repeat the same thing for the remaining arcs. We also have to make sure that the starting direction of each arc is North.

Here is a program that draws a simple seven-color rainbow. You can store it in file rainbow.py in folder Unit2.

# Get ready to draw
from turtle import *
width = 30
radius = 330
y_start = -radius/2
pensize(width+1)
# Draw red half-circle
penup()
setposition(radius, y_start)
setheading(90)
pendown()
pencolor('red')
circle(radius,180)
# Draw orange half-circle
radius -= width
penup()
setposition(radius, y_start)
setheading(90)
pendown()
pencolor('orange')
circle(radius,180)
# Draw yellow half-circle
radius -= width
penup()
setposition(radius, y_start)
setheading(90)
pendown()
pencolor('yellow')
circle(radius,180)
# Draw green half-circle
radius -= width
penup()
setposition(radius, y_start)
setheading(90)
pendown()
pencolor('green')
circle(radius,180)
# Draw blue half-circle
radius -= width
penup()
setposition(radius, y_start)
setheading(90)
pendown()
pencolor('blue')
circle(radius,180)
# Draw indigo half-circle
radius -= width
penup()
setposition(radius, y_start)
setheading(90)
pendown()
pencolor('indigo')
circle(radius,180)
# Draw violet half-circle
radius -= width
penup()
setposition(radius, y_start)
setheading(90)
pendown()
pencolor('violet')
circle(radius,180)
# Finish
hideturtle()

As you can see, the code is fairly straightforward. You may have noticed that we use a new type of statement, the subtraction-assignment statement: radius -= width. As the name suggests, it combines subtraction and assignment into one statement. It is equivalent to radius = radius – width and can be read as “reduce radius by width“.

We also used a little trick to make the drawing nicer. Even though we make radius smaller by 30 pixels, we draw the arcs 31 pixels wide. This way, the arcs very sightly overlap and there is no white space between the arcs! Experiment with changing the pen width and the radius reduction and see what happens!

Feel free to just copy-and-paste the code above. And don’t forget to modify it and experiment with different values!

Here is the result of running rainbow.py – a beautiful seven-color rainbow:

[cws-ad1]

Improving Rainbow Code

So, how did you like the rainbow code? Did you notice that we keep doing the same thing over and over? And that the code between the comments is nearly identical with the only difference being the color of turtle pen? Remember, good programmers are “lazy” – they don’t write more code than absolutely necessary. And this is way too much code!

So, let us borrow a few concepts from Unit 3: Functions and Conditionals and show you how to make the code shorter, easier to read, and easier to maintain.

Python Functions

Similarly to other high-level programming languages, Python supports user-defined functions. Functions allow us to give a name to a block of code and then call that block of code over and over again using just that name. Remember that, in Python, block of code is just a bunch of indented statements.

To make the rainbow code better, we first define a (user-defined) function draw_arc() which takes color as its parameter and draws one arc of the rainbow. And then, instead of calling the same set of statements seven times, we only call the function seven times. Isn’t it so much easier to read?

Code Maintenance

Now that we wrote such beautiful code, let us talk about how to maintain it. Imagine that, after a while, you are not happy with the way the rainbow looks. Perhaps it looks too round and you want to make the rainbow more like a rectangle. If you use rainbow2.py as your starting point, you only need to replace circle(radius, 180) with drawing a partial rectangle in one place. However, if you use rainbow.py as your starting point, you need to do the replacement 7 times! This is why we say that rainbow2.py is easier to maintain than rainbow.py.

[cws-ad1]

Exercise 2

Modify the code from Exercise 1 to draw a square rainbow.

Solution

This exercise is one of the reasons why we have improved the code in Exercise 1 and used a function instead of repeating the same code seven times. We really did not want to make bad code even worse 🙂

To make a square rainbow, we first save rainbow2.py into a file named rainbow_square.py. Use File > Save As… in the IDLE editor window to do this. To make the rainbow square, we need to replace half-circles with half-squares. The height of each half-square is the same as the radius of the half-circle and the width of the half-square is the same as the diameter of the half-circle. Everything else remains the same as before.

See how easy this was? Imagine doing this with the original code in rainbow.py. You would have to do this change seven times! This is why we say that rainbow2.py is easier to maintain then rainbow.py.

And here is the square rainbow:

[cws-ad1]

Exercise 3

Modify the code from Exercise 2 to remove the little white corners in the square rainbow above.

Solution

You probably figured out by now that we would not be happy with the drawing above – the little white corners are “messing up” the picture! Can we remove them? There are many different ways how do it, we chose to fill the half-square with the same color as the pen. At the end, all we have to do is draw one more arc with white color. The code and the result are below:

You should try to make the changes to rainbow_square.py on your own and see how difficult or easy they are. Can you imagine doing the same adjustments to rainbow.py?

[prev-next-ad1 prev=”https://codewithsara.com/python-with-sara/python-101/u2-python-turtle-graphics/u2s4-coloring-with-turtle/” next=”https://codewithsara.com/python-with-sara/python-101/u3-python-functions-and-conditionals/”]