Lesson 2.5: Turtle Circles and Arcs
Up until now, we used Python Turtle to draw only straight lines. In this lesson, we will show you how to draw circles and partial circles. All we need is function circle(). Function circle() takes two parameters – radius and an optional parameter angle. Let’s discuss both in more detail.
Statement circle(50) is the same as statement circle(50, 360) and draws a circle of radius 50. Statement circle(-50) also draws a circle of radius 50. So what’s the difference? circle(50) draws a circle on turtle’s left. circle(-50) draws a circle on turtle’s right. You can think of circle(50) as starting in the direction that turtle is facing and then keeps turning left little-by-little and drawing tiny lines until it comes back to the starting point. With circle(-50), turtle also starts in the direction it’s facing but keeps turning right.
With arcs, it’s even more complicated. In order to draw an arc/partial circle, you use variable angle to specify how big a portion of the circle you want to draw. For full circle, angle = 360, for half-circle, angle = 180, etc. So, circle(50, 90) draws a quarter of a circle of radius 50.
Both radius and angle can be positive or negative. When radius is positive, arc is a portion of the left circle. When radius is negative, arc is a portion of the right circle. Any circle or arc always starts at the current position. If angle is positive, turtle starts moving forward. If angle is negative, turtle starts moving backward.
Let’s illustrate this with an example. Create a file circles_and_arcs.py and type in the following code. We have purposely lowered the speed of the drawing (by calling function speed()), so that you can observe, how turtle moves and turns.
Here is the result of running the code above. It is really important that you run the program above yourself and see how turtle moves and turns and how the circles are drawn. We’d also like to encourage you to modify parameters of the circle() function and see how the drawing changes.
The code below produces a smiley face by drawing one filled circle, 2 arcs, and 2 dots. Feel free to modify it to your liking. You can make lines thinner or thicker, make the smile smaller or larger (by changing the angle parameter), or change the smiley face to a frowning face (by making radius negative and shifting the mouse up). You can also experiment with different sizes by changing the radius.
The two new functions – heading() and dot() are self-explanatory. The statement heading(0) turns turtle to face East (3 o’clock). The statement heading(90) makes turtle to face North (12 o’clock). The statement dot(10) draws a circular dot of diameter 10.
And here is the result of the code above.
Draw a 7-color rainbow using Python Turtle Graphics.
Hint: Use the following seven colors – red, orange, yellow, green, blue, indigo, and violet. Draw each arc separately, stacking them next to each other. Set pen size to 30.
Modify the code from Exercise 1 to draw a square rainbow. See picture below.
Modify the code from Exercise 2 to remove the little white corners in the square rainbow above.
Hint: Draw the rainbow starting with the red arc. For each arc, fill the half-square being drawn using the same fill color as the pen color. To remove the violet fill from the very last square draw one more arc with white color.
Congratulations!!! You have completed all the lessons in Unit 2: Python Turtle Graphics. Look how far you have come! At the beginning of this unit, you had no idea what Python Turtle Graphics was, now you are an advanced Turtle user! Look how many amazing and colorful pictures we have drawn! Would you ever think that after just a few lessons you would be able to write a Python program that can draw pretty much anything?
Remember to proudly show your programs to your family and friends. We are very certain that they will be really impressed – you’ve come a long way!