Skip to content
Advertisement

Is there a way to get and save an angle of a turtle for later?

I’m making a program where the user can click on the screen 3 times and it makes a triangle. It will then find the middle points of those 3 lines. Next, I want it to draw 3 lines perpendicular to each of the lines that make up the triangle (through the middle points).

I’m stuck at drawing the perpendicular lines. I wanted to try to save (like you save a position or coordinates) the angles that the turtle bob (the one that connects the lines of the triangle) makes on each line, and then put the middle line turtles (l1, l2, l3) tilt in that angle. It would then make it go right and left and draw the line. I’m sorry if this is unclear, but I can’t really think of another way to explain it. This is my code:

import turtle

tur = turtle.Turtle() #draws the dots
bob = turtle.Turtle() #connects the dots
l1 = turtle.Turtle() #middle point of line 1
l2 = turtle.Turtle() #middle point of line 2
l3 = turtle.Turtle() #middle point of line 3
mid = turtle.Turtle() #point where the lines cross, haven't used this one yet
screen = turtle.Screen()
screen.tracer(0,0)

screen.setup(800,800)

tur.ht()
tur.up()
tur.speed(0)
bob.ht()
bob.up()
bob.speed(0)
screen.update()

l1.up()
l2.up()
l3.up()
mid.up()
mid.ht()

dots = 1

def lines():
    line()
    l1.goto(line_1_mid_cords) #go to middle of lines
    l1.dot()
    l2.goto(line_2_mid_cords)
    l2.dot()
    l3.goto(line_3_mid_cords)
    l3.dot()

def line():
    global line_1_mid_cords, line_2_mid_cords, line_3_mid_cords
    line_1_mid_cords = (( bob_x1 + bob_x2 ) /2, ( bob_y1 + bob_y2 ) /2) #get middle coordinates
    
    line_2_mid_cords = (( bob_x2 + bob_x3 ) /2, ( bob_y2 + bob_y3 ) /2)
    
    line_3_mid_cords = (( bob_x1 + bob_x3 ) /2, ( bob_y1 + bob_y3 ) /2)
    

def dotOnClick(x,y):
    global dots, bob_x1, bob_y1, bob_x2, bob_y2, bob_x3, bob_y3
    
    if dots == 1:
        if x >= -800 and x <= 800 and y >= -800 and y <= 800: #click on screen
            tur.goto(x,y) #go to that point
            bob.goto(tur.pos())#bob goes there too
            bob_x1 = bob.xcor() #saves bobs coordinates
            bob_y1 = bob.ycor()
            tur.dot()
            bob.down()
            screen.update()
            dots = 2 
                
    elif dots == 2:
        if x >= -800 and x <= 800 and y >= -800 and y <= 800: #when second dot is clicked
            tur.goto(x,y) #both tur and bob to to that point and they get connected
            bob.goto(tur.pos())
            bob_x2 = bob.xcor()
            bob_y2 = bob.ycor()
            tur.dot() #draws second dot
            bob.down()
            screen.update()
            dots = 3
    
    elif dots == 3:
        if x >= -800 and x <= 800 and y >= -800 and y <= 800: #same for 3rd point on screen
            tur.goto(x,y)
            bob.goto(tur.pos())
            bob_x3 = bob.xcor()
            bob_y3 = bob.ycor()
            tur.dot()
            bob.down()
            bob.goto(bob_x1,bob_y1) #bob goes to first dot and completes the triangle
            lines() #makes the turtles l1, l2, l3 go to the middle points of the triangle lines
            screen.update()
            dots = 4
       
    elif dots == 4:
        if x >= -800 and x <= 800 and y >= -800 and y <= 800: #resets when you click again
            tur.clear()
            bob.clear()
            screen.update()
            tur.reset()
            bob.reset()
            tur.ht()
            tur.up()
            tur.speed(0)
            bob.ht()
            bob.up()
            bob.speed(0)
            screen.update()
            dots = 1

        
turtle.onscreenclick(dotOnClick,1,True)

If I need to explain more about what I exactly mean, please say so!

Advertisement

Answer

After putting your mid-point turtles in their spot, you can aim them towards the next point on the triangle.

def lines():
    line()
    l1.goto(line_1_mid_cords) #go to middle of lines
    l1.setheading(l1.towards(bob_x2, bob_y2))
    l1.dot()
    l2.goto(line_2_mid_cords)
    l2.setheading(l2.towards(bob_x3, bob_y3))
    l2.dot()
    l3.goto(line_3_mid_cords)
    l3.setheading(l3.towards(bob_x1, bob_y1))
    l3.dot()
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement