How to draw shapes with python | |
Hello, today we are going to learn how to draw shapes such as circles, squares as well as triangles in Python. The turtle module is the one we will use to perform this task. | |
We are going to present in detail the Turtle library, its characteristics, as well as the different functionalities it offers. | |
This article does not require any prerequisites apart from knowledge of the mathematical characteristics of the shapes that we are going to present. | |
1. Turtle the drawing module of Python | |
Turtle is a Python module which has the role of a drawing board, indeed, the principle is simple. This library has a set of features that allow you to command a turtle to draw the shape you want. For example, turtle.forward() and turtle.right() allow the turtle to move. | |
Several methods are used to draw in the turtle module. Among these are: | |
forward(x) moves the pen forward by x units. | |
backward(x) moves the pen backwards by x units. | |
right(x) rotation of the pen clockwise by an angle x. | |
left(x) rotation of the pen counterclockwise by an angle x. | |
penup() stop the drawing turtle. | |
pendown() the turtle starts drawing | |
2. Draw a circle with turtle | |
In this first section of the article, we will discover how to draw a circle in Python and how to modify the different characteristics of the latter. | |
Example | |
In order to concretize the task, we will start with a simple and basic example. Now, to draw a circle using turtle , we will use a predefined function in "turtle". | |
Syntax | |
# Program Python # Draw cicle import turtle # Initialise turtle turtle = turtle.Turtle() rayon = 20 turtle.circle(rayon) |
We first start by importing the turtle module, then we initiate an instance of the latter, here we will name the instance turtle. Thereafter, we choose the radius of the circle that we want to draw then we call on the function circle() which draws a circle of the defined radius by considering the position of the turtle as the center. | |
Execution result | |
2.1. Tangent circles | |
A tangent is a line that touches the circumference of a circle from the outside provided that the extension of this line does not produce an intersection with the circle. Tangent circles are a group of circles that have a common tangent. | |
Example | |
In this example, we are going to implement a program that draws tangent circles using a loop. | |
Syntax | |
# Program Python # Draw tangent circles # Import turtle module import turtle # Initialise turtle tur = turtle.Turtle() # Radius of the smallest circle rayon = 10 # Number of circles nb = 10 # Loop to draw the tangent circles for i in range(1, nb + 1, 1): tur.circle(rayon*i) |
Execution Result | |
2.2. spiral circles | |
The spiral is a similar shape to a normal circle, the difference is that the radius of the spiral increases after each increment. | |
Example | |
Here is a program that draws spiral circles using a for loop. | |
Syntax | |
# Python program # Draw spiral circles # import the turtle module import turtle tur = turtle.Turtle() # take the size of the initial radius rayon = 10 # Loop for printing the spiral circle for i in range(100): tur.circle(rayon + i, 45) |
We start by importing the turtle module and then we initiate an instance of turtle . Next, we define our initial radius of size 10. Finally, we define a for loop for printing the spiral circles, as you can see the radius of the circle increases after each iteration. | |
The second argument to the circle() method helps draw an arc, so it controls the measure of the central angle. Here we passed 45 as the argument for the central angle. This command is repeated 100 times to obtain concentric circles. | |
Execution result | |
2.3. Concentric circles | |
Concentric circles are a set of circles that have the same center and whose radius increases proportionally with each iteration. | |
Example | |
Syntax | |
import turtle tur = turtle.Turtle() # Circle radius radius = 10 # boucle pour dessiner les cercles cocentriques for i in range(20): tur.circle(radius * i) tur.up () trusty ((radius* i)*(-1)) tur.down () |
After drawing a circle, we took the turtle pen and set the y coordinate of the turtle pen to -1 times radius*i. Then we put the pen back on the canvas. This process is repeated 50 times to obtain concentric circles. | |
Execution result | |
3. Draw squares and rectangles with the turtle module | |
In this second section of the tutorial, we will discover how to draw squares and rectangles with the turtle module. | |
Two functions that are useful for us to draw square and rectangle are- forward() and left(). Before drawing any of these shapes, we need to know its basic properties. Let's start with a square. All sides of a square are equal and the angle between two adjacent sides is 90°. Thus, the opposite sides are parallel to each other. | |
Now that we know the main characteristics of the square, we can proceed to draw it: | |
Example | |
Here is a basic example of drawing a square using turtle's forward() and left() functions. | |
Syntax | |
#Program to draw a square with turtle import turtle tur = turtle.Turtle() tur.forward(100) #Forward turtle of 100 units tur.left(90) #90 degree turtle rotation tur.forward(100) tur.left(90) tur.forward(100) tur.left(90) tur.forward(100) tur.left(90) |
We start by importing the turtle module. Next, we created a turtle drawing board instance and assigned it to an object named tur . | |
Then we moved the turtle forward 100 units since the side of a square is 100 . Next, we rotated 90 degrees because the angle between adjacent sides is 90 degrees. These two instructions are used to draw one side of the square. The same steps are repeated 3 times until a final square is obtained. | |
Execution result | |
3.1. Using loops to draw a square with turtle | |
As you can see in the last example, we used the same forward(100) and left(90) functions four times. It is therefore preferable to make a loop instead of rewriting the same instruction several times. | |
Example | |
We will use the same example as the last one, the only difference is the use of a loop in this example. | |
Syntax | |
# Using a loop to draw a square with turtle # We import the turtle module import turtle tur = turtle.Turtle() # Start of loop for i in range(4):# the loop will rotate 4 times tur.forward(100) # Advances 100 steps forward tur.left(90) # rotation 90 degrees |
We start by importing the turtle module and initiating the designer. Then, we declare a loop that will turn 4 times to draw our square. | |
Execution result | |
The execution result is similar to the last example: | |
3.2. Draw rectangles with turtle | |
A rectangle is characterized by its equal opposite sides and the angle between two adjacent sides of a rectangle is 90 degrees. Knowing these properties, we can draw the rectangle thanks to the functions of the turtle module. | |
Example | |
Suppose the length of the rectangle is 150 units and its width is 80 units. Run the code below to get the desired rectangle. | |
Syntax | |
#Program to draw a rectangle with the turtle module import turtle tur = turtle.Turtle() tur.forward(150) # Move the turtle 150 units forward tur.left(90) # rotate the turtle 90 degrees tur.forward(80) # Move the turtle 80 units forward tur.left(90) # rotate the turtle 90 degrees tur.forward(150) # Move the turtle 150 units forward tur.left(90) # rotate the turtle 90 degrees tur.forward(80) # Move the turtle 80 units forward tur.left(90) # rotate the turtle 90 degrees |
We moved the turtle forward 150 units since the length of a rectangle is 150 units. Next, we rotated the turtle 90° because the angle between adjacent sides is 90°. Besides, we sent the turtle 80 units and turned it 90°. This completes the second side of the rectangle. The same statements are repeated once more to draw the remaining two sides. | |
Execution result | |
3.3. Using loops to draw a rectangle with turtle | |
Based on the method used to draw a square using a loop, we will do the same for the rectangle. In this case we will loop forward(150), left(90), forward(80) and left(90) and run it 2 times. | |
Example | |
Syntax | |
#Program to draw a rectangle with the turtle module import turtle tur = turtle.Turtle() tur.forward(150) # Move the turtle 150 units forward tur.left(90) # rotate the turtle 90 degrees tur.forward(80) # Move the turtle 80 units forward tur.left(90) # rotate the turtle 90 degrees tur.forward(150) # Move the turtle 150 units forward tur.left(90) # rotate the turtle 90 degrees tur.forward(80) # Move the turtle 80 units forward tur.left(90) # rotate the turtle 90 degrees |
Execution result | |
4. Draw triangles with turtle | |
In this last section of the article , we will discover how to draw triangles using the different functions of the turtle module. | |
We'll start by defining the functions using in this section: | |
Turtle() method to create a turtle object | |
Onscreenclick() This turtle function that sends the current coordinate to the function that uses it to form a triangle, 1 is for left click and 3 is for right click. | |
Speed() increase or decrease the speed of the sketcher. | |
penup() This function is built into the turtle library to draw the line. | |
pendown() This function is built into the turtle library to draw in line. | |
forward () allows to advance the designer forward according to the pixel given in input. | |
left() rotates the turtle to the left according to the rotation angle given as input. | |
Example | |
Here is a simple example of drawing an equilateral triangle. | |
Syntax |
# We import the turtle module import turtle # Initiation of the draftsman tur = turtle.Turtle() tur. forward(100) # draw base tur.left (120) tur.forward (100) tur.left (120) tur.forward (100) tur.done() |
Execution result | |
Example | |
In this second example, we want to draw a triangle with a right angle. | |
Syntax | |
import turtle tur = turtle.Turtle() tur.forward(100) # base du dessin tur.left(90) tur.forward(100) tur.left(135) tur.forward(142) turtle.done() |
Execution result | |
Example | |
Drawing a star shape using two identical isosceles triangles . | |
Syntax: | |
import turtle tur = turtle.Turtle() # first triangle of the star tur. forward(100) # draw base tur.left(120) tur.forward(100) tur.left(120) tur.forward(100) tur.penup() tur.right (150) tur.forward (50) # second star triangle tur.pendown() tur.right(90) tur.forward(100) tur.right(120) tur.forward(100) tur.right(120) tur.forward(100) turtle.done() |
Execution result | |
Example | |
In this last example, we will implement a function triangle () which will allow to draw a triangle with the coordinates as soon as the user clicks on the designer. | |
Syntax | |
import turtle aff = turtle.Screen() # Create tur object tur=turtle.Turtle() def triangle(x,y): # draw line tur.penup() # move the cursor to the position of the x and y coordinates tur.goto(x,y) tur.pendown() for i in range(3): # move the slider 100 units forward tur.forward(100) # rotate cursor 120 degrees left tur.left(120) # Another time, move the slider 100 units tur.forward(100) # special function to send the current position of the cursor on the triangle turtle.onscreenclick(triangle,1) turtle.listen() turtle.done() |
Execution result | |
We have come to the end of this article, now you know how to draw basic shapes thanks to Python's Turtle module. You can now move on to more complex shapes like polygons for example. | |
We also advise you to discover all the features of turtle drawings such as the color of the drawing, the type of lines... | |
We wish you good luck and see you in a future article! |