为什么未绘制此希尔伯特曲线?

时间:2019-05-20 16:37:31

标签: python python-2.7 turtle-graphics fractals

import turtle as t
t.setup(500,500)
t.setworldcoordinates(0,0,500,500)
t.pu()
t.goto(0,0)
t.pd()
t.seth(0)
def N():
    t.pu()
    t.pd()
def B(c,d):
    t.right(90)
    c
    t.forward(5)
    t.left(90)
    d
    t.forward(5)
    d
    t.left(90)
    t.forward(5)
    c
    t.right(90)
def A(a,b):
    t.left(90)
    b
    t.forward(5)
    t.right(90)
    a
    t.forward(5)
    a
    t.right(90)
    t.forward(5)
    b
    t.left(90)      
t.seth(0)
A(A(None,None),B(None,None))

我正在尝试制作希尔伯特曲线,但是它不起作用。 我正在使用L-system

1 个答案:

答案 0 :(得分:0)

您的代码正在将函数传递给其他函数,但未正确调用它们(您需要括号和参数。)我在修补此代码时的最佳猜测如下:

from turtle import Screen, Turtle

def A(a, b, n):

    if n == 0:
        return

    turtle.left(90)
    b(a, b, n - 1)
    turtle.forward(5)
    turtle.right(90)
    a(a, b, n - 1)
    turtle.forward(5)
    a(a, b, n - 1)
    turtle.right(90)
    turtle.forward(5)
    b(a, b, n - 1)
    turtle.left(90)

def B(c, d, n):

    if n == 0:
        return

    turtle.right(90)
    c(c, d, n - 1)
    turtle.forward(5)
    turtle.left(90)
    d(c, d, n - 1)
    turtle.forward(5)
    d(c, d, n - 1)
    turtle.left(90)
    turtle.forward(5)
    c(c, d, n - 1)
    turtle.right(90)

screen = Screen()
screen.setup(500, 500)
screen.setworldcoordinates(0, 0, 500, 500)

turtle = Turtle()

A(A, B, 7)

screen.exitonclick()

enter image description here