缩放乌龟绘图 python

我正在尝试编写一个 python 脚本,它需要 n 并根据该顺序绘制一条希尔伯特曲线。我的算法工作正常,它绘制曲线并在更改窗口大小时重新调整大小。但是,我的绘图没有居中并且可能超出范围。我想在没有太多空白空间或超出范围的情况下使用屏幕缩放曲线


这是我的代码:


import sys

import turtle

from turtle import Turtle, Screen



#Drawing the hilbert curve using recursion.

#Var: turtle if for the Turtle, A is the length of the lines, parity is for inverting the direction, and n is for the order

def hilbert_curve(turtle, A, parity, n):


if n < 1:

    return


turtle.left(parity * 90)

hilbert_curve(turtle, A, - parity, n - 1)

turtle.forward(A)

turtle.right(parity * 90)

hilbert_curve(turtle, A, parity, n - 1)

turtle.forward(A)

hilbert_curve(turtle, A, parity, n - 1)

turtle.right(parity * 90)

turtle.forward(A)

hilbert_curve(turtle, A, - parity, n - 1)

turtle.left(parity * 90)




def main():

    #Rescale the drawing when changing the window size

    def onResize(x=0, y=0):

     width = my_win.window_width()

     hight = my_win.window_height()

     my_win.setworldcoordinates(-width-1, -hight-1, width-1, hight-1)

     my_win.ontimer(onResize,100)



#initilize the turtle.

turtle = Turtle()

#initilize the screen.

my_win = Screen()

w = my_win.window_width()

h = my_win.window_height()

A = 20

onResize()





rule = 1

my_win.tracer(False)

if len(sys.argv) < 2:

    print("Please declare the order after calling the program name")

    return

n = int(sys.argv[1])

hilbert_curve(turtle,A,rule,n)


my_win.update()

my_win.mainloop()



main()

**如果有人能解决我的问题,我将不胜感激,谢谢**


慕运维8079593
浏览 185回答 1
1回答

慕村225694

我的算法工作正常,它绘制曲线并在更改窗口大小时重新调整大小。不,它没有。您的绘图永远不会缩放,它保持相同的大小。并且main()设置缩放代码的函数永远不会被调用,因为它遵循mainloop()将控制权移交给 tkinter 事件处理程序的调用:my_win.mainloop()main()使用事件计时器是解决此问题的错误方法。然而,由于 turtle 没有暴露底层的 tkinter 窗口调整大小事件,让我们一起玩这个模型,而不是下降到 tkinter 层。我会这样做:from turtle import Turtle, Screendef hilbert_curve(turtle, A, parity, n):&nbsp; &nbsp; '''&nbsp; &nbsp; Draw the hilbert curve using recursion.&nbsp; &nbsp; Arguments:&nbsp; &nbsp; &nbsp; &nbsp; turtle is for the Turtle,&nbsp; &nbsp; &nbsp; &nbsp; A is the length of the lines,&nbsp; &nbsp; &nbsp; &nbsp; parity is for inverting the direction,&nbsp; &nbsp; &nbsp; &nbsp; and n is for the order&nbsp; &nbsp; '''&nbsp; &nbsp; if n < 1:&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; turtle.left(parity * 90)&nbsp; &nbsp; hilbert_curve(turtle, A, - parity, n - 1)&nbsp; &nbsp; turtle.forward(A)&nbsp; &nbsp; turtle.right(parity * 90)&nbsp; &nbsp; hilbert_curve(turtle, A, parity, n - 1)&nbsp; &nbsp; turtle.forward(A)&nbsp; &nbsp; hilbert_curve(turtle, A, parity, n - 1)&nbsp; &nbsp; turtle.right(parity * 90)&nbsp; &nbsp; turtle.forward(A)&nbsp; &nbsp; hilbert_curve(turtle, A, - parity, n - 1)&nbsp; &nbsp; turtle.left(parity * 90)def main():&nbsp; &nbsp; order = 4&nbsp; &nbsp; parity = 1&nbsp; &nbsp; length = 100 / (4 * order - 1)&nbsp; &nbsp; def onResize():&nbsp; &nbsp; &nbsp; &nbsp; # Rescale drawing when changing window size (the hard way)&nbsp; &nbsp; &nbsp; &nbsp; turtle.reset()&nbsp; &nbsp; &nbsp; &nbsp; screen.setworldcoordinates(0, 0, 100, 100)&nbsp; &nbsp; &nbsp; &nbsp; hilbert_curve(turtle, length, parity, order)&nbsp; &nbsp; &nbsp; &nbsp; screen.update()&nbsp; &nbsp; &nbsp; &nbsp; screen.ontimer(onResize, 1000)&nbsp; &nbsp; screen = Screen()&nbsp; &nbsp; screen.tracer(False)&nbsp; &nbsp; turtle = Turtle()&nbsp; &nbsp; onResize()&nbsp; &nbsp; screen.mainloop()main()即无论窗口大小如何都保持恒定的虚拟坐标,并重新绘制曲线以适应当前窗口大小。顺便说一句,我不是写了这个希尔伯特曲线代码吗?确保从哪里获得它!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python