You are on page 1of 4

Lets Draw Ourselves Some Right-Angle Triangles Woohoo!

Part 1 How the heck do I draw anything?


We will be using a library called turtle which allows us to draw simple graphics using Python. Use the
template below to get started (you do not need to write the #comments).
1.
2.
3.
4.
5.
6.

import turtle

# This creates our window.
window = turtle.Screen()

# This creates our actual 'turtle' that will be used to move around the screen and draw
# different things. You can name it anything you like but I called it pencil to help
# remember that it is responsible for the drawing.
7. pencil = turtle.Turtle()
8. pencil.forward(100)
9.
10. window.exitonclick()

When you run the program, what happens? IT DRAWS A LINE! Impressive, right? Well lets expand
our drawing skills. The commands below show you some other powers of turtle. Most of the method
names give hints as to what they actually do. Experiment with these methods and the template below.
Work on becoming more confident with each of the methods, youll use them more and more.
1. pencil.forward(25)
2. pencil.backward(50)
3. pencil.left(90)
4. pencil.right(720)
5.
6. pencil.penup()
7. pencil.pendown()
8.
9. pencil.goto(50,-200)
10. pencil.goto(-150,300)
11.
12. pencil.hideturtle()
13. pencil.showturtle()
14.
15. pencil.pensize(10)
16. pencil.speed(5)
17. pencil.color('red')
18.
19. pencil.write("Hello student!", move=False, align="left", font=("Arial", 14, "bold"))
20.
21. #the best one!!!!
22. pencil.shape("turtle")

Feeling pretty confident with each of these commands? Ok! Try these challenges.
Challenge #1A - Draw a blue triangle.
Challenge #1B - Draw a right angle green triangle with legs of length 100 and 60.
Challenge #1C - Draw a red isosceles triangle and label it as an Isosceles Triangle with black text.

Part 2 Bringing in our Trig Ratios!


In Challenge #2 when you drew a right-angle triangle, how did you decide on the correct length for the
hypotenuse? Did you calculate it using the Pythagorean Theorem? Were you able to do this in Python?
If you were then great job! If you were not, dont worry you have not learned how yet.
Without importing any additional libraries, we are able to do many basic mathematical operation in
Python. Have a look and experiment with some of the examples shown below.
1.
2.
3.
4.
5.
6.
7.

print(6+2)
print(6-2)
print(6*2)
print(6/2)
print(6**2)
print(abs(-12))
print(7 % 2)

Can you figure out what each example is doing? Many of them are pretty easy but 7 % 2 might be a little
tricky. Another way of reading this is 7 modulo 2 and it represents the remainder after division. For 7 %
2 the remainder after division is 1 because 2 goes into 7 a total of 3 times with a remainder of 1.
Great! But you might be thinking that you still are not able to apply the Pythagorean Theorem because
you dot not have the ability to apply the square root. Well just like we had to import the turtle library
so that we are able to use all of its methods, we also have to import a library called math (very
appropriate name!) to get access to additional operations and methods.
1. import math
2.
3. #convert from radians to degrees and degrees to radians
4. print(math.degrees(3.14))
5. print(math.radians(360))
6.
7. #trig ratios (must be given in radians)
8. print(math.sin(math.radians(30)))
9. print(math.cos(math.radians(60)))
10. print(math.tan(math.radians(45)))
11.
12. #inverse trig ratios (returns radians)
13. print(math.asin(0.5))
14. print(math.acos(math.sqrt(2)/2))
15. print(math.atan(math.sqrt(3)))
16.
17. #whole bunch of other functions
18. print(math.sqrt(16))
19. print(math.factorial(5))
20. print(math.pow(2, 5))

The documentation for the math module includes all the other functions that are available
(https://docs.python.org/3/library/math.html).

Challenge #2A - See if you can draw a random right-angle triangle with legs (the two sides that are not
the hypotenuse) between 100 and 300 units in length. Label the three sides of the triangle as well as the
angles.
1. import turtle
2. import random
3.
4. window = turtle.Screen()
5.
6. pointer = turtle.Turtle()
7.
8. pointer.forward(random.randint(100,300))
9.
10. window.exitonclick()

Another useful method that might help you:


1.
2.
3.
4.
5.

decimal_value = 132.43414

#you can round off decimal values using this method
print(round(decimal_value, 2))
print(round(decimal_value, 4))

Challenge #2B We are writing many different things to the screen (side lengths and angles) and we
are doing it over and over again. You probably notice that it repeats a lot of the same code. Try to create
a function that lets you write any message you want onto different locations on the screen.
1.
2.
3.
4.
5.
6.
7.
8.

def write_message(writer, x, y, message):


writer.pencolor('black')
writer.penup()
writer.goto(x,y)
writer.pendown()
writer.write(message, move=False, align="left", font=("Arial", 14, "bold"))

write_message(cursor, -180, 0, "This is message.")

Challenge #2C Can you find any other ways to make custom functions that might be useful? What if
we wanted to draw new triangles? Can you make a function called draw_triangle that does exactly what
its name says it draws a triangle with specific side lengths and angles.

Part 3 Turn the program into a problem generator


We now have our program drawing random right-angle triangles. Fantastic!
But how could we turn this program into a simple problem generator? Which of the angles and sides
should we show? And do we need a way to show the answers?
Think about this and brainstorm with your classmates about how this could be done. How many
different types of questions could be generated? Is there a way to randomly choose which type of
question we show the user?
Challenge #3A See if you can figure out how to randomly generate different problems. We want the
program to show the user either 2 of the sides or 1 side and 1 angle. Which sides and angles are shown
should be randomly generated. As a hint, one strategy will involve using if statements the sample code
below might help give you a hint.
1.
2.
3.
4.
5.
6.
7.

question_type = random.randint(0,3)

if question_type == 0:
#make this type of question
elif question_type == 1:
#make this type of question
# and so on and so on

Challenge #3B Have any other ideas for your problem generator? Feel free to challenge yourself and
see what you can come up with.

You might also like