有没有办法获得并节省乌龟的角度?

发布于 2025-02-08 14:01:22 字数 3278 浏览 2 评论 0原文

我正在制作一个程序,用户可以单击屏幕3次,这是一个三角形。然后,它将找到这3行的中间点。接下来,我希望它绘制垂直于组成三角形的每条线(通过中间点)的每条线的3行。

我陷入垂直线。我想尝试保存(就像您保存一个位置或坐标一样)乌龟bob(连接三角形线的一个)的角度,然后将中间行放置海龟(l1l2l3)以该角度倾斜。然后,它会使它向右移动并绘制线路。很抱歉,如果这还不清楚,但是我真的无法想到另一种解释的方式。这是我的代码:

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)

如果我需要更多地解释我的含义,请这样说!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2025-02-15 14:01:22

将您的中点海龟放在他们的位置之后,您可以将它们瞄准三角形的下一个点。

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文