We will use the 5 pixel grid to trace out this image. The image of this is shown below:
The plan to draw this symbol is as follows:
- Lift the turtle
- Move it to the position that is 5 squares from the centre of the symbol
- Place the turtle down
- Set its pensize to 4 squares
- Draw a circle of a radius of 5 squares
- Lift up the pen
- Move it back to the centre of the symbol which is at the origin
- Set the pensize to 3 squares
- Set the heading of the turtle to 90 degrees
- Move to the position that is 5 squares from the centre of the circle in the direction of the heading of the turtle
- Place the turtle down
- Draw a line by moving forward by 8 squares
- Set the orientation of the turtle based on the formula (heading - 90)
- Set the pensize to 1
- Draw a filled circle from the point the line stops
- Go back to step 6 but each time you get to step 9, increase the heading by 40 until you get to 410
Using Turtle Graphics
We will use the template.py file and rename it to fofoo.py. The size of each square on our grid is 10 pixels. We shall use this as our multiplication factor.
From our first step, we need to lift up the pen. The code to do this is shown below:
turtle.penup()
turtle.setposition(0, -50)
To place the turtle down. The code to place the turtle down is shown below:
turtle.pendown()
To set the pensize to 4 squares, we will multiply this by 10 to get 40. The code to do this is shown below:
turtle.pensize(40)
We need to draw a circle of radius 5 squares. We use our multiplication factor and the value becomes 50. The code to do this is shown below:
turtle.circle(50)
The image that is generated is shown below:
turtle.penup()
turtle.home()
turtle.pensize(30)
turtle.setheading(90)
turtle.forward(50)
turtle.pendown()
turtle.forward(80)
The generated image is shown below:
turtle.setheading(0)
turtle.pensize(1)
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
The generated image is shown below:
Now it would be easier to just use a simple while loop. So delete all the code for step 6 to step 15 and just enter this:
heading = 90
while (heading <= 410):
turtle.penup()
turtle.home()
turtle.pensize(30)
turtle.setheading(heading)
turtle.forward(50)
turtle.pendown()
turtle.forward(80)
turtle.setheading(heading - 90)
turtle.pensize(1)
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
heading = heading + 40
If you look carefully, you will see all the steps from steps 6 to 16 in this code. The only thing different about this code is the introduction of a variable called heading.
The generated image is shown below:
At the end of this section, we have successfully used Python turtle to draw the Fofoo symbol.
The use of a loop made the drawing of this symbol easier than if we had drawn each part individually.
No comments:
Post a Comment