We will use the 5 pixel grid to trace out this image. The image of this is shown below:
- Set the pen size to 40 pixels
- Draw the outer square
- Draw all the lines working from the upper left quadrant
Using Turtle Graphics
We will use the template.py file and rename it to nhwimu.py.
The code for the first step of the algorithm is shown below:
turtle.pensize(40)
To draw the outer square, we can use the drawSquare function. The size of our square is 340 pixels. The code for this is given below:
drawSquare(340)
The generated image is shown below:
Thankfully, 2 of each lines repeat in the shape so just have to identify which lines repeat and use the lengths of the lines.
To draw a line, we need to get its coordinates. Then we find the length of the line. We know that the angle between the lines is a multiple of 45 degrees so knowing its coordinates help us to know the distance between the lines.
The first line extends from (-120, 150) to (150, -120). To get the distance between the two points, we use the coordinateDistance function and assign the value it returns to a variable called firstLineLength. The code to do this is shown below:
firstLineLength = coordinateDistance(-120, 150, 150, -120)
To draw the line, we need to lift up the pen and move it to the point (-120, 150). Then we set the heading of the turtle to -45 degrees. We must reduce the pensize to 5 pixels and then we draw the first line. The code to do this is shown below:
turtle.penup()
turtle.setposition(-120, 150)
turtle.setheading(-45)
turtle.pensize(5)
turtle.pendown()
turtle.forward(firstLineLength)
The generated image is shown below:
Line 2 extends from (-140, 150) to (150, -140). The code to draw the line is given below:
turtle.penup()
secondLineLength = coordinateDistance(-140, 150, 150, -140)
turtle.setposition(-140, 150)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(secondLineLength)
Line 3 starts at (-150, 140). Its length is the same as that of the second line so we don’t need to calculate the length of this line. Our code will now be as shown below:
turtle.penup()
turtle.setposition(-150, 140)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(secondLineLength)
Line 4 starts at (-150, 120). Its lenght is the same as that of the first line so we don’t need to calculate its length. Our code will now be as shown below:
turtle.penup()
turtle.setposition(-150, 120)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(firstLineLength)
The generated image is shown below:
To draw the fifth line, we note where it starts and where it ends. Once we know its length, we can then proceed to draw it.
The fifth line goes from (-150, 0) to (0, 150). The code to draw the line is given below:
turtle.penup()
fifthLineLength = coordinateDistance(-150, 0, 0, 150)
turtle.setposition(-150, 0)
turtle.setheading(45)
turtle.pendown()
turtle.forward(fifthLineLength)
The sixth line goes from (-150, -20) to (20, 150). The code to draw the line is given below:
turtle.penup()
sixthLineLength = coordinateDistance(-150, -20, 20, 150)
turtle.setposition(-150, -20)
turtle.setheading(45)
turtle.pendown()
turtle.forward(sixthLineLength)
The seventh line goes from (-150, -40) to (40, 150). The code to draw the line is given below:
turtle.penup()
seventhLineLength = coordinateDistance(-150, -40, 40, 150)
turtle.setposition(-150, -40)
turtle.setheading(45)
turtle.pendown()
turtle.forward(seventhLineLength)
The eight line goes from (-150, -60) to (60, 150). The code to draw the line is given below:
turtle.penup()
eightLineLength = coordinateDistance(-150, -60, 60, 150)
turtle.setposition(-150, -60)
turtle.setheading(45)
turtle.pendown()
turtle.forward(eightLineLength)
The generated image is shown below:
Line nine extends from (-150, -20) to (-20, -150). The code to draw the line is shown below:
turtle.penup()
ninthLineLength = coordinateDistance(-150, -20, -20, -150)
turtle.setposition(-150, -20)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(ninthLineLength)
Line ten extends from (-150, -40) to (-40, -150). The code to draw the line is shown below:
turtle.penup()
tenthLineLength = coordinateDistance(-150, -40, -40, -150)
turtle.setposition(-150, -40)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(tenthLineLength)
Line eleven extends from (-150, -60) to (-60, -150). The code to draw the line is shown below:
turtle.penup()
eleventhLineLength = coordinateDistance(-150, -60, -60, -150)
turtle.setposition(-150, -60)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(eleventhLineLength)
Line twelve extends from (-150, -80) to (-80, -150). The code to draw the line is shown below:
turtle.penup()
twelvethLineLength = coordinateDistance(-150, -80, -80, -150)
turtle.setposition(-150, -80)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(twelvethLineLength)
The generated image is shown below:
Line thirteen runs from (-80, -150) to (150, 80). The code to draw the line is shown below:
turtle.penup()
thirteenLineLength = coordinateDistance(-80, -150, 150, 80)
turtle.setposition(-80, -150)
turtle.setheading(45)
turtle.pendown()
turtle.forward(thirteenLineLength)
Line fourteen starts at (-60, -150). It has the same length as line eight so we use it. The code to draw this line is shown below:
turtle.penup()
turtle.setposition(-60, -150)
turtle.setheading(45)
turtle.pendown()
turtle.forward(eightLineLength)
Line fifteen starts at (-40, -150). It has the same length as line seven so we use it. The code to draw this line is shown below:
turtle.penup()
turtle.setposition(-40, -150)
turtle.setheading(45)
turtle.pendown()
turtle.forward(seventhLineLength)
Line sixteen starts at (-20, -150). It has the same length as line six so we use the length of line six. The code to draw this line is shown below:
turtle.penup()
turtle.setposition(-20, -150)
turtle.setheading(45)
turtle.pendown()
turtle.forward(sixthLineLength)
The generated image is shown below:
To draw line seventeen, we must establish where it starts from and where it ends in order to get its distance. The line starts at (150, 20) and ends at (20, 150).
turtle.penup()
seventeenthLineLength = coordinateDistance(150, 20, 20, 150)
turtle.setposition(150, 20)
turtle.setheading(135)
turtle.pendown()
turtle.forward(seventeenthLineLength)
Line eighteen starts at (150, 40) and ends at (40, 150). The code to draw the line is shown below:
turtle.penup()
eighteenthLineLength = coordinateDistance(150, 40, 40, 150)
turtle.setposition(150, 40)
turtle.setheading(135)
turtle.pendown()
turtle.forward(eighteenthLineLength)
Line nineteen starts at (150, 60) and ends at (60, 150). The code to draw the line is shown below:
turtle.penup()
nineteenLineLength = coordinateDistance(150, 60, 60, 150)
turtle.setposition(150, 60)
turtle.setheading(135)
turtle.pendown()
turtle.forward(nineteenLineLength)
Line twenty starts at (150, 80) and ends at (80, 150). The code to draw the line is shown below:
turtle.penup()
twentiethLineLength = coordinateDistance(150, 80, 80, 150)
turtle.setposition(150, 80)
turtle.setheading(135)
turtle.pendown()
turtle.forward(twentiethLineLength)
The generated image is shown below:
At the end of this program, we have drawn the Nkyimu shape.
No comments:
Post a Comment