我无法理解递归。该main()
函数对齐海龟。该tree()
函数被调用branchLen = 75
。因此,它通过了“if”条件并上升。根据我的理解,乌龟应该连续右转5次,长度依次递减为75、60、45、30、15。此后,它将不再满足“if”条件。代码只会运行到第 5 行(第一次递归调用)。因此,应该显示一条倾向于 RHS 的单行。不应该有任何左转弯。但这并没有发生,一个完全对称的树被制作出来了。请解释如何。
有关问题的更多信息,请参阅链接。
谢谢!
https://interactivepython.org/runestone/static/pythonds/Recursion/pythondsintro-VisualizingRecursion.html
def tree(branchLen,t):
if branchLen > 5:
t.forward(branchLen)
t.right(20)
tree(branchLen-15,t)
t.left(40)
tree(branchLen-15,t)
t.right(20)
t.backward(branchLen)
def main():
t = turtle.Turtle()
myWin = turtle.Screen()
t.left(90)
t.up()
t.backward(100)
t.down()
t.color("green")
tree(75,t)
myWin.exitonclick()
main()
这不是真的,它没有向左旋转。请注意如何始终使用乌龟的同一实例进行绘制,因此乌龟总是从每次调用之前的位置继续。
def tree(branchLen,t, direction="straight"):
if branchLen > 5:
print branchLen, t.pos(), direction #debug info
t.forward(branchLen) #go forward branchlen
t.right(20) #rotate right 20
tree(branchLen-15,t) #call first branch of recursion
t.left(40) #rotate left 40
tree(branchLen-15,t) #call second branch of recursion
t.right(20) #rotate right 20
t.backward(branchLen) #go back branchlen - it is now reset to the original position before this call of tree
print "reset to previous", t.pos()
所以基本上发生的是:
您必须注意,当它出现时,它开始使用branchLen
调用此分支的函数的值,而 t 保持不变。这是调试信息的结果:
branchlen, position, direction called
75 (-0.00,-100.00) straight
60 (-0.00,-25.00) right
45 (20.52,31.38) right
30 (49.45,65.85) right
15 (75.43,80.85) right
0 (90.20,83.46) right # 0 does not draw
0 (90.20,83.46) left
reset to previous (75.43,80.85) #after this it will resurface 1 level, and repeat
15 (75.43,80.85) left
0 (85.07,92.34) right
0 (85.07,92.34) left
reset to previous (75.43,80.85)
reset to previous (49.45,65.85) #here it resurfaces twice
30 (49.45,65.85) left
15 (59.71,94.04) right
0 (69.35,105.54) right
0 (69.35,105.54) left
reset to previous (59.71,94.04)
15 (59.71,94.04) left
0 (59.71,109.04) right
0 (59.71,109.04) left
reset to previous (59.71,94.04)
reset to previous (49.45,65.85)
reset to previous (20.52,31.38)
45 (20.52,31.38) left
30 (20.52,76.38) right
15 (30.78,104.57) right
0 (40.42,116.06) right
0 (40.42,116.06) left
reset to previous (30.78,104.57)
15 (30.78,104.57) left
0 (30.78,119.57) right
0 (30.78,119.57) left
reset to previous (30.78,104.57)
reset to previous (20.52,76.38)
30 (20.52,76.38) left
15 (10.26,104.57) right
0 (10.26,119.57) right
0 (10.26,119.57) left
reset to previous (10.26,104.57)
15 (10.26,104.57) left
0 (0.62,116.06) right
0 (0.62,116.06) left
reset to previous (10.26,104.57)
reset to previous (20.52,76.38)
reset to previous (20.52,31.38)
reset to previous (0.00,-25.00)
60 (0.00,-25.00) left
45 (-20.52,31.38) right
30 (-20.52,76.38) right
15 (-10.26,104.57) right
0 (-0.62,116.06) right
0 (-0.62,116.06) left
reset to previous (-10.26,104.57)
15 (-10.26,104.57) left
0 (-10.26,119.57) right
0 (-10.26,119.57) left
reset to previous (-10.26,104.57)
reset to previous (-20.52,76.38)
30 (-20.52,76.38) left
15 (-30.78,104.57) right
0 (-30.78,119.57) right
0 (-30.78,119.57) left
reset to previous (-30.78,104.57)
15 (-30.78,104.57) left
0 (-40.42,116.06) right
0 (-40.42,116.06) left
reset to previous (-30.78,104.57)
reset to previous (-20.52,76.38)
reset to previous (-20.52,31.38)
45 (-20.52,31.38) left
30 (-49.45,65.85) right
15 (-59.71,94.04) right
0 (-59.71,109.04) right
0 (-59.71,109.04) left
reset to previous (-59.71,94.04)
15 (-59.71,94.04) left
0 (-69.35,105.54) right
0 (-69.35,105.54) left
reset to previous (-59.71,94.04)
reset to previous (-49.45,65.85)
30 (-49.45,65.85) left
15 (-75.43,80.85) right
0 (-85.07,92.34) right
0 (-85.07,92.34) left
reset to previous (-75.43,80.85)
15 (-75.43,80.85) left
0 (-90.20,83.46) right
0 (-90.20,83.46) left
reset to previous (-75.43,80.85)
reset to previous (-49.45,65.85)
reset to previous (-20.52,31.38)
reset to previous (0.00,-25.00)
reset to previous (0.00,-100.00)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句