缩放乌龟绘图 python
Scaling a turtle drawing python
我正在尝试编写一个 python 脚本,该脚本采用 n 并根据该顺序绘制希尔伯特曲线。我的算法工作正常它绘制曲线并在更改 window 大小时重新调整大小。但是,我的绘图没有居中并且可能超出范围。我想用屏幕缩放曲线而没有太多空白 space 或让它越界
这是我的代码:
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()
**如果有人能解决我的问题,我将不胜感激**
my algorithm work fine it draws the the curve and rescale the size
when changing the window size.
不,不是。您的绘图 从未 缩放过,它保持相同的大小。设置缩放代码的 main()
函数是 never 调用,因为它遵循 mainloop()
调用,将控制权移交给 tkinter 事件处理程序:
my_win.mainloop()
main()
使用事件计时器是解决此问题的错误方法。然而,由于 turtle 不暴露底层的 tkinter window resize 事件,让我们一起玩这个模型,而不是下降到 tkinter 层。我会这样做:
from turtle import Turtle, Screen
def hilbert_curve(turtle, A, parity, n):
'''
Draw the hilbert curve using recursion.
Arguments:
turtle is for the Turtle,
A is the length of the lines,
parity is for inverting the direction,
and n is for the order
'''
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():
order = 4
parity = 1
length = 100 / (4 * order - 1)
def onResize():
# Rescale drawing when changing window size (the hard way)
turtle.reset()
screen.setworldcoordinates(0, 0, 100, 100)
hilbert_curve(turtle, length, parity, order)
screen.update()
screen.ontimer(onResize, 1000)
screen = Screen()
screen.tracer(False)
turtle = Turtle()
onResize()
screen.mainloop()
main()
即无论 window 大小如何,都保持恒定的虚拟坐标并重新绘制曲线以适应当前的 window 大小。顺便说一句,didn't I write this Hilbert curve code?确保为您从哪里获得它投票!
我正在尝试编写一个 python 脚本,该脚本采用 n 并根据该顺序绘制希尔伯特曲线。我的算法工作正常它绘制曲线并在更改 window 大小时重新调整大小。但是,我的绘图没有居中并且可能超出范围。我想用屏幕缩放曲线而没有太多空白 space 或让它越界
这是我的代码:
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()
**如果有人能解决我的问题,我将不胜感激**
my algorithm work fine it draws the the curve and rescale the size when changing the window size.
不,不是。您的绘图 从未 缩放过,它保持相同的大小。设置缩放代码的 main()
函数是 never 调用,因为它遵循 mainloop()
调用,将控制权移交给 tkinter 事件处理程序:
my_win.mainloop()
main()
使用事件计时器是解决此问题的错误方法。然而,由于 turtle 不暴露底层的 tkinter window resize 事件,让我们一起玩这个模型,而不是下降到 tkinter 层。我会这样做:
from turtle import Turtle, Screen
def hilbert_curve(turtle, A, parity, n):
'''
Draw the hilbert curve using recursion.
Arguments:
turtle is for the Turtle,
A is the length of the lines,
parity is for inverting the direction,
and n is for the order
'''
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():
order = 4
parity = 1
length = 100 / (4 * order - 1)
def onResize():
# Rescale drawing when changing window size (the hard way)
turtle.reset()
screen.setworldcoordinates(0, 0, 100, 100)
hilbert_curve(turtle, length, parity, order)
screen.update()
screen.ontimer(onResize, 1000)
screen = Screen()
screen.tracer(False)
turtle = Turtle()
onResize()
screen.mainloop()
main()
即无论 window 大小如何,都保持恒定的虚拟坐标并重新绘制曲线以适应当前的 window 大小。顺便说一句,didn't I write this Hilbert curve code?确保为您从哪里获得它投票!