Forgot to post last week but here we go. I decided on Python🐍 to add to my skillset. Primarily back-end (Flask, Django) but may branch out in the future. I’m following the guide posted by /u/TravisJungroth on reddit. I’m currently going through Runestone Interactive and the
NOTE: You should be familiar with Python basics such as variables, for loop, and functions. Also, the Turtle library.
This is the pattern we will create:
At first this pattern looks very complex, but if we break it down into small chunks, its manageable. Looking for shapes, we see can see, a pie slice:
If we look further, we notice a square:
Further examination, we see a grid of four squares:
Let’s start small and work are way to a solution. First, let’s import the turtle library and setup our canvas.
import turtle tess = turtle.Turtle() wn = turtle.Screen() tess.speed(10) tess.pensize(2) tess.color('blue') wn.bgcolor('lightgreen')
Let’s examine each line:
This imports the Turtle library.
tess = turtle.Turtle()
tess our turtle object.
wn = turtle.Screen()
wn creates the screen object where are pattern will be drawn.
This adjusts the speed of the pen draw.
1 is slow and
10 is fast.
This adjust the size of the line drawn.
This sets the pen color to blue.
This sets the canvas background color to lightgreen.
Now all the setup code is done, let’s tackle the problem.
Let’s start with creating a
def drawSquare(t, sz): for i in range(4): t.fd(sz) t.left(90)
Let’s go through each line:
def drawSquare(t, sz):
This declares the
drawSquare function that takes two parameters.
t represents a turtle object and
sz the size of a square side length.
for i in range(4):
We create a
for loop that iterates four times: 0,1,2,3.
This moves the turtle object forward
This rotates the turtle
Let’s try our function by calling it:
Now that we know our
drawSquare works, we can start making the grid. Something to keep in mind. Back in elementary school, I used a compass to draw unique shapes or circles. Furthermore, I centered the compass to draw the shapes. This knowledge will help us later on. Let’s build the
def drawGrid(t, sz): for i in range(4): drawSquare(t, sz) t.left(90)
def drawGrid(t, sz):
We declare the
drawGrid function which takes two parameters.
t is a turtle object and
sz is the size of the square side length in the grid.
for i in range(4):
Again, we create a
for loop that iterates four times: 0,1,2,3. This accounts for the four squares in the grid.
This calls the
drawSquare function with two parameters.
t is the turtle object and
sz is the size of the square.
This will turn the turtle left
Now that our
drawGrid function is complete, let’s test it out.
drawGrid with two arguments.
tess is the turtle object and
100 is the square length size.
NOTE: Arguments and parameter have different meaning. Arguments is data passed to function and parameters are what the function accepts.
Notice where the turtle is currently. It's at position (0, 0). This is the origin for drawing the squares and the grid. This is needed so the grid does not get distorted when rotating them, as we will see later on.
We are almost done. Now we need to create the pie slices in the circle. Earlier, if you looked at the pretty pattern we're making, you might of noticed something:
It’s a stack of grids that rotate slightly. Another observation, there are five pie slices in each square of the grid. Let’s wrap this up:
def main(): for i in range(5): drawGrid(tess, 100) tess.left(18) main()
Line by line:
We declare a
main function to hold our code. This isn’t required but it keeps the code organized.
for i in range(5):
Remember the five pie slices? Yes, we iterate that many times, which creates five grids. This
for loop iterates five times: 0,1,2,3,4.
drawGrid with two arguments.
tess as the turtle object and
100 as the square side length.
After a grid is drawn we turn
18 degrees left. You might wonder, where does the number
18 comes from? Remember, we turn
90 degrees after drawing each square. Since we are drawing five grids we divide that to get the turn angle after each grid is drawn.
90 // 5 = 18.
# pretty-picture.py import turtle tess = turtle.Turtle() wn = turtle.Screen() tess.speed(10) tess.pensize(2) tess.color('blue') wn.bgcolor('lightgreen') def drawSquare(t, sz): for i in range(4): t.fd(sz) t.left(90) def drawGrid(t, sz): for i in range(4): drawSquare(t, sz) t.left(90) def main(): for i in range(5): drawGrid(tess, 100) tess.left(18) main()
We could refine this further and move the turtle setup code to the
main function. I hope this tutorial helped you in problem solving. Although the problem looked difficult at first, changing it to small bits reduced its complexity. For more information, checkout the resources below:
Cover Photo by Iva Muškić from Pexels