递归龟图
Turtle Graphics with Recursion
我需要使用递归和海龟图形绘制形状。
我更像是一个循环者,很少使用递归,所以在这里提供一些帮助会很好。甚至不知道从哪里开始。
造型只要好看就可以了
没有任何参数化,这里是一个开始:
import time
from turtle import *
def recurse(n):
if n>0:
left(10)
forward(5)
recurse(n-1)
recurse(20)
time.sleep(5)
当你创建递归函数时,你需要有一个停止标准来有效地保证你的程序会在某个时刻退出。
天哪!多么有趣的问题:)
既然你是自称'looper',我会像你会循环一样考虑递归。
在for
循环中,执行循环体直到满足for
循环条件。现在,递归非常相似。您一直调用该函数,直到该函数的参数不再遇到递归情况。他们取而代之的是一个基本情况,然后 returns 一个递归可以建立的值。
那么递归这么想,我们来想一想怎么画一个正方形。您需要首先确定代码的哪些部分被重复了(即 for
循环的主体中试图做同样的事情)。然后,确定您希望此重复何时停止(即我如何知道 for
循环何时退出)。
在画正方形时,我可以想到至少重复 4 次的两件主要事情。海龟前进一定数量的步数,然后转 90 度(或 270 度,具体取决于方向)。所以这将是我们在递归案例中详细说明的内容。
现在,让我们考虑一下基本情况。嗯,我们知道正方形有4条边,所以乌龟画完4条边后,我们想让它停下来。
最后,让我们考虑一下函数声明以及这两个部分(递归情况和基本情况)如何发挥作用。函数声明可以采用以下形式(在 Python 中):
def draw_square_recursive(turn_deg=90, side_len, sides=4):
"""
Function draws a square with turtle graphics recursively
:param turn_deg: an int, the number of degrees a turtle should turn
:param side_len: an int, the length of a side of the square
:param sides: an int, the number of sides in our square
"""
turn_deg
和 side_len
对我们的递归情况很重要,因为它们定义了乌龟应该如何转弯以及应该转多远 'walk'。 sides
是一个有趣的参数,我们可以用它来决定是继续循环还是停止。如果每次画边的时候sides
减1,我们就知道需要在sides == 0
的时候停止重复,一个base case!
因此,每当我们再次调用我们的函数时,我们将调用它,draw_square_recursive(side_len, sides-1):
总的来说,函数的结构如下:
def draw_square_recursive(turn_deg=90, side_len, sides=4):
"""
Function draws a square with turtle graphics recursively
:param turn_deg: an int, the number of degrees a turtle should turn
:param side_len: an int, the length of a side of the square
:param sides: an int, the number of sides in our square
"""
if sides == 0:
# base case!
else:
# recursive case!
请注意,此函数名为 draw_square_recursive
,但它可以更广泛地推广到其他形状。你看怎么样?
抱歉,如果这是一个冗长的回答!希望对你有帮助;p
编辑多于答案,但递归是这样的:
def recurse(n):
if n>0:
left(10)
forward(5)
recurse(n-1)
哪个写成迭代更好:
for n in range(2):
left(10)
forward(5)
类似于有人问,"How can I count the number of elements in a list using recursion?" 同上使用递归绘制正方形。
我知道目标是了解递归,但似乎迷失了方向,有时递归会带来美妙的事情,有时它只会减慢您的程序。分形是一个通过递归实现奇迹的机会:
import sys
from turtle import Turtle, Screen
def hilbert_curve(n, turtle, angle=90):
if n <= 0:
return
turtle.left(angle)
hilbert_curve(n - 1, turtle, -angle)
turtle.forward(1)
turtle.right(angle)
hilbert_curve(n - 1, turtle, angle)
turtle.forward(1)
hilbert_curve(n - 1, turtle, angle)
turtle.right(angle)
turtle.forward(1)
hilbert_curve(n - 1, turtle, -angle)
turtle.left(angle)
depth = int(sys.argv[1])
size = 2 ** depth
screen = Screen()
screen.setworldcoordinates(0, 0, size, size)
yertle = Turtle('turtle')
yertle.speed('fastest')
yertle.penup()
yertle.goto(0.5, 0.5)
yertle.pendown()
hilbert_curve(depth, yertle)
yertle.hideturtle()
screen.exitonclick()
用法
% python3 hilbert.py 5
(部分)输出
我不是在挑其他的答案,我建议你想大一点(或者至少超越 "just needs to be cool looking"。)
我需要使用递归和海龟图形绘制形状。
我更像是一个循环者,很少使用递归,所以在这里提供一些帮助会很好。甚至不知道从哪里开始。
造型只要好看就可以了
没有任何参数化,这里是一个开始:
import time
from turtle import *
def recurse(n):
if n>0:
left(10)
forward(5)
recurse(n-1)
recurse(20)
time.sleep(5)
当你创建递归函数时,你需要有一个停止标准来有效地保证你的程序会在某个时刻退出。
天哪!多么有趣的问题:)
既然你是自称'looper',我会像你会循环一样考虑递归。
在for
循环中,执行循环体直到满足for
循环条件。现在,递归非常相似。您一直调用该函数,直到该函数的参数不再遇到递归情况。他们取而代之的是一个基本情况,然后 returns 一个递归可以建立的值。
那么递归这么想,我们来想一想怎么画一个正方形。您需要首先确定代码的哪些部分被重复了(即 for
循环的主体中试图做同样的事情)。然后,确定您希望此重复何时停止(即我如何知道 for
循环何时退出)。
在画正方形时,我可以想到至少重复 4 次的两件主要事情。海龟前进一定数量的步数,然后转 90 度(或 270 度,具体取决于方向)。所以这将是我们在递归案例中详细说明的内容。
现在,让我们考虑一下基本情况。嗯,我们知道正方形有4条边,所以乌龟画完4条边后,我们想让它停下来。
最后,让我们考虑一下函数声明以及这两个部分(递归情况和基本情况)如何发挥作用。函数声明可以采用以下形式(在 Python 中):
def draw_square_recursive(turn_deg=90, side_len, sides=4):
"""
Function draws a square with turtle graphics recursively
:param turn_deg: an int, the number of degrees a turtle should turn
:param side_len: an int, the length of a side of the square
:param sides: an int, the number of sides in our square
"""
turn_deg
和 side_len
对我们的递归情况很重要,因为它们定义了乌龟应该如何转弯以及应该转多远 'walk'。 sides
是一个有趣的参数,我们可以用它来决定是继续循环还是停止。如果每次画边的时候sides
减1,我们就知道需要在sides == 0
的时候停止重复,一个base case!
因此,每当我们再次调用我们的函数时,我们将调用它,draw_square_recursive(side_len, sides-1):
总的来说,函数的结构如下:
def draw_square_recursive(turn_deg=90, side_len, sides=4):
"""
Function draws a square with turtle graphics recursively
:param turn_deg: an int, the number of degrees a turtle should turn
:param side_len: an int, the length of a side of the square
:param sides: an int, the number of sides in our square
"""
if sides == 0:
# base case!
else:
# recursive case!
请注意,此函数名为 draw_square_recursive
,但它可以更广泛地推广到其他形状。你看怎么样?
抱歉,如果这是一个冗长的回答!希望对你有帮助;p
编辑多于答案,但递归是这样的:
def recurse(n):
if n>0:
left(10)
forward(5)
recurse(n-1)
哪个写成迭代更好:
for n in range(2):
left(10)
forward(5)
类似于有人问,"How can I count the number of elements in a list using recursion?" 同上使用递归绘制正方形。
我知道目标是了解递归,但似乎迷失了方向,有时递归会带来美妙的事情,有时它只会减慢您的程序。分形是一个通过递归实现奇迹的机会:
import sys
from turtle import Turtle, Screen
def hilbert_curve(n, turtle, angle=90):
if n <= 0:
return
turtle.left(angle)
hilbert_curve(n - 1, turtle, -angle)
turtle.forward(1)
turtle.right(angle)
hilbert_curve(n - 1, turtle, angle)
turtle.forward(1)
hilbert_curve(n - 1, turtle, angle)
turtle.right(angle)
turtle.forward(1)
hilbert_curve(n - 1, turtle, -angle)
turtle.left(angle)
depth = int(sys.argv[1])
size = 2 ** depth
screen = Screen()
screen.setworldcoordinates(0, 0, size, size)
yertle = Turtle('turtle')
yertle.speed('fastest')
yertle.penup()
yertle.goto(0.5, 0.5)
yertle.pendown()
hilbert_curve(depth, yertle)
yertle.hideturtle()
screen.exitonclick()
用法
% python3 hilbert.py 5
(部分)输出
我不是在挑其他的答案,我建议你想大一点(或者至少超越 "just needs to be cool looking"。)