How to create polygons with Python
We are going to learn how to create polygons with Python, using the Turtle module.
We presume that you already know the basics about this library, otherwise you can look at this post about how to draw a square with python. We have several posts in our blog on Python and Turtle you may find interesting.
Poligons with Python code
If we want to create polygons with Python, the first thing we will do is to import the turtle module, with the reserved keyword ‘import‘. This will give us access to the functions and objects of this module.
Then we access the constructor turtle.Turtle() which we assign to a variable called t.
Py
#import Turtle module
import turtle
#create the turtle object
t = turtle.Turtle()
Then we will create the function that will allow us to create polygons with Python.
This function will have a series of parameters such as t -> the turtle object , d -> the number of pixels of the sides , n -> the number of sides that the polygon will have, and finally screen_reset -> reset the screen after each figure has been drawn.
Py
# polygon function
def polygon(t,d,n,screen_reset):
# reset the screen
if screen_reset == True:
reset()
# angle to indicate the number of sides
angle = 360 / n
# Create the polygon with range()
for i in range(n):
t.fd(d)
t.lt(angle)
# reset function
def reset():
turtle.resetscreen()
Basically the code above inside the polygon functions tells us that if the variable screen_reset is True, then every time a polygon is drawn, the screen will be cleared before the next one can be drawn.
However, if the value of screen_reset is False, then we will be able to see several polygons at the same time together.
We also have the variable angle which will divide the polygon into the number of sides according to the angle established. To do that, we divide 360 by the number of sides of the polygon and it will give us the correct angle.
Finally we actually create the polygon with an iteration for and range(). The instruction t.fd(d) tells us that turtle(t) moves forward . It moves the amount of pixels of the side of the polygon (d) and t.lt (angle) tells us that turtle moves to the left the angle indicated above.
Py
# call the function three times
polygon(t,100,6,False)
polygon(t,120,6,False)
polygon(t,150,6,False)
We have called the function three times so we can see three polygons, as the screen_reset variable is False. Otherwise we would have just seen the last polygon drawn.
How to create a star with Python
Now we will create a star with Python code, as it is one of the most popular geometric shapes.
We will follow the code steps as we did previously. We will also introduce a new parameter to the star function, such as color. We also have to import the math module in conjunction with the turtle module.
Py
import turtle,math
# create the turtle object
t = turtle.Turtle()
# reset function
def reset():
turtle.resetscreen()
# star function
def star(t,d,screen_reset,color):
# reset
if screen_reset == True:
reset()
# establish the angles
angle = 180 / 5
angle = 180 - angle
# color of the star
t.color(color)
# create the star with for and range()
for i in range(5):
t.td(d)
t.lt(angle)
Once we import the turtle and math modules, we create the turtle – t object.
We also define the reset( ) screen function as we did in the previous example.
As for the star function itself, we pass the parameters of the object -> t , distance in pixels -> d , the option to reset screen -> screen_reset and the colour of the figure -> color.
The angles of the star are defined with the variable angle. In this case, as we want a 5-pointed star, we divide 180 / 5.
We indicate the colour in t.colour(color) and the rest as in the previous geometric figure.
Py
# call the function three times
star(t,250,5,False,'red')
star(t,150,5,False,'green')
star(t,50,5,False,'blue')
Draw geometric figures with Python and also with the Turtle module, is an exercise that Python students usually come across in their initial projects. If you wish to learn more about this language, here are some ebooks to learn Python code.
Creatuwebpymes is a web design company based in Lanzarote – Canary Islands. We ❤️ programming in Python, hence the reason for this post.