使用海龟图形和递归的希尔伯特曲线 [英] Hilbert curve using turtle graphics and recursion
问题描述
我正在尝试使用 python 海龟图形和递归来实现 L 系统生成的希尔伯特曲线.我的代码似乎适用于递归 n=1 和 n=2 的前两个级别,但除此之外,图形只是纠缠不清(尽管我能够观察其中的更多模块),我似乎无法理解这里可能有什么问题,我是否需要一些中间步骤来重新生成 Hilbert 模块以进行更深层次的递归?请看我下面的代码,它比较简单:
导入海龟def Hilbert_curve(A,rule,t,n):如果n>=1:如果规则:t.left(90)Hilbert_curve(A,not rule,t, n-1)t.forward(A)右(90)Hilbert_curve(A, rule,t, n-1)t.forward(A)Hilbert_curve(A,rule,t, n-1)右(90)t.forward(A)Hilbert_curve(A,not rule,t, n-1)t.left(90)别的:右(90)Hilbert_curve(A,rule,t, n-1)t.forward(A)t.left(90)Hilbert_curve(A,not rule,t, n-1)t.forward(A)Hilbert_curve(A,not rule,t, n-1)t.left(90)t.forward(A)Hilbert_curve(A, rule,t, n-1)右(90)定义主():A=10t=turtle.Turtle()my_win=turtle.Screen()n=2规则=真Hilbert_curve(A,rule,t,n)my_win.exitonclick()主要的()
I'm trying to implement an L-System generated Hilbert curve ,making use of python turtle graphics and recursion. My code seems to be working for the first two levels of recursion n=1 and n=2 but beyond that , the graphics just entangled (although I´m able to observe further modules within them), and I can´t seem to grasp what might be wrong here, do I need some intermediate steps to regenerate the Hilbert modules for deeper levels of recursion? Please see my code below , its relatively simple:
import turtle
def Hilbert_curve(A,rule,t,n):
if n>=1:
if rule:
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
t.right(90)
Hilbert_curve(A, rule,t, n-1)
t.forward(A)
Hilbert_curve(A,rule,t, n-1)
t.right(90)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
else:
t.right(90)
Hilbert_curve(A,rule,t, n-1)
t.forward(A)
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
t.forward(A)
Hilbert_curve(A, rule,t, n-1)
t.right(90)
def main():
A=10
t=turtle.Turtle()
my_win=turtle.Screen()
n=2
rule=True
Hilbert_curve(A,rule,t,n)
my_win.exitonclick()
main()
The problem is with your else
clause. The rule
is already inverted coming in to the function, so you need to treat the rule
the same as the then clause:
else:
t.right(90)
Hilbert_curve(A, not rule, t, n - 1)
t.forward(A)
t.left(90)
Hilbert_curve(A, rule, t, n - 1)
t.forward(A)
Hilbert_curve(A, rule, t, n - 1)
t.left(90)
t.forward(A)
Hilbert_curve(A, not rule, t, n - 1)
t.right(90)
However, if we change rule
from a boolean to a number, parity
, that's either 1 or -1, and then multiply parity
by the angle, we can eliminate one of the two clauses of the orignal if
statement:
from turtle import Screen, Turtle
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)
screen = Screen()
yertle = Turtle()
yertle.speed('fastest') # because I have no patience
hilbert_curve(yertle, 10, 1, 4)
screen.exitonclick()
这篇关于使用海龟图形和递归的希尔伯特曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!