我真的不知道如何解决保持递归,我正在研究海龟库,但在他们要求使用 matplotlib 的签名中,所以我不太确定如何使用它
from turtle import Turtle
def hilbert_curve(turtle, A, parity, n):
if n == 1:
hilbert_curve_draw(turtle, A, parity)
else:
turtle.right(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.left(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.left(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.right(parity * 90)
def hilbert_curve_draw(turtle, A, parity):
turtle.right(parity * 90)
turtle.forward(A)
turtle.left(parity * 90)
turtle.forward(A)
turtle.left(parity * 90)
turtle.forward(A)
turtle.right(parity * 90)
yertle = Turtle()
yertle.hideturtle()
yertle.penup()
yertle.goto(-200, 200)
yertle.pendown()
yertle.speed('fastest')
hilbert_curve(yertle, 60, 1, 3)