# Solutions 2.4: Coloring with Turtle

Below are the solutions to the exercises from Lesson 2.4: Coloring with Turtle. Please remember to try to solve the exercises on your own before checking the solutions here.

### Exercise 1

Draw two side-by-side squares that are filled with 4 different colors like on the picture below.

Looking at the blue/aqua triangle (we call it a half-square) drawn by square_and_a_half.py, you can imagine that if we (carefully) drew four such half-squares, we should get the same drawing as above. And that’s exactly what the solutions does. You only need to remember to switch colors after drawing each two sides and to stop and start color fill after each half-square. And don’t forget to set the line width and the final colors and heading for the turtle.

### Exercise 2

Draw the 72-point red-and-blue star from the end of Unit 2: Python Turtle Graphics  page.

#### Solution

##### Version 1

Let’s use the code from generic_star_even.py discussed in the solution to Exercise 2 in Solutions 2.3 as a starting point. We want to draw 2n edges, half of them colored red, the other half colored blue. Notice how turtle draws a 2n-point star. In the first half of the drawing, it draws an (almost complete) n-point star and in the second half of the drawing, it interleaves the remaining n vertices in-between the ones that are already there. This suggests that we can just draw the first n vertices with color red and second n vertices with color blue and we should get the desired effect of alternating red-and-blue vertices. Let’s store the code in file red_and_blue_star.py.

``````from turtle import *
hideturtle()
penup()
backward(200)
pendown()
n = 36
angle = 180 - 180 / n
# Draw the red portion of the star
color('red')
for i in range(n):
forward(400)
right(angle)
# Draw the blue portion of the star
color('blue')
for i in range(n):
forward(400)
right(angle)``````
##### Version 2

The star above looks very nice but if you look carefully, there are couple vertices that have both a blue edge and a red edge (look at the horizontal edges). To avoid that, we can try to switch the colors not at the vertex of the star but rather at the center (and adjust the start and end as well). In other words, we want to start drawing the star from the center, draw half an edge, then n-1 edges, then half an edge again in color red. And then repeat the same process for blue edges. The resulting code will look something like this:

``````from turtle import *
hideturtle()
n = 8
angle = 180 - 180 / n
penup()
setposition(0,50)
pendown()
# Draw the red portion of the star
color('red')
forward(200)
right(angle)
for i in range(n - 1):
forward(400)
right(angle)
forward(200)
# Draw the blue portion of the star
color('blue')
forward(200)
right(angle)
for i in range(n - 1):
forward(400)
right(angle)
forward(200)``````

We actually thought that this should work but, upon closer look, the vertices are not correctly alternating! In one place, we have two consecutive red vertices, and opposite to that are two consecutive blue vertices. We changed n to 8, so that you can see the problem more easily.

##### Version 3

Since the above did not quite produce an alternating red-and-blue star, let’s analyze the drawing more carefully and see if we can fix it. Let’s label the vertices from 1 to 16 and see if we can figure out how to make the colors alternate.

If the colors were truly alternating, the red vertices would all have to be even and blue vertices would all have to be odd (or vice versa). We have quite the opposite – half of the blue vertices are odd, half are even, the same for red. Can we fix it?

Notice how each edge goes from odd to even or from even to odd number. This suggests that one half of each edge needs to be red and the other half blue. So consider this algorithm:

1. Set the position near the center of the star.
2. Set the color to red.
3. Draw half edge, turn right the required angle, draw half edge.
4. Change color to blue.
5. Draw half edge, turn right the required angle, draw half edge.
6. Repeat 2-5 until the entire star is completed.

Will this work? Let’s trace the drawing above pretending that our finger is the turtle. We start at the center, drawing the first red edge to vertex 4 and then back to center. The we draw a blue edge to vertex 11 and back to center. So vertex 4 is red and vertex 11 is blue. Next, we make vertex 2 red. Then vertex 9 blue. 16 is red, 7 is blue, 14 is red, 5 is blue, etc. As you trace the star, you can see that with this new algorithm, all even vertices are red and all odd vertices are blue. Exactly what we wanted!

Here is the code for the algorithm above:

``````from turtle import *
hideturtle()
n = 8
angle = 180 - 180 / n
penup()
setposition(0,50)
pendown()
for i in range(n):
# Draw red vertex
color('red')
forward(200)
right(angle)
forward(200)
# Draw blue vertex
color('blue')
forward(200)
right(angle)
forward(200)``````

And here is the drawing of the 16-point star. Can you see how nicely alternating the colors are?

##### Final Solution

Here is a final solution for the 72-point red-and-blue star:

### Conclusion

The above was a great example of how to solve real problems: First, come up with a draft solution that may or may not work and is most likely incomplete. Next, try to improve your solution by analyzing its deficiencies and considering simpler cases (in our case we reduced the number of vertices), which makes your analysis easier. Then, solve the simplified problem. And, finally, extend your solution to the original problem. Simple, isn’t it 🙂