【问题标题】:Why is this Hilbert curve not drawing?为什么这条希尔伯特曲线不画?
【发布时间】:2019-05-20 16:37:31
【问题描述】:
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

【问题讨论】:

  • 请分享您遇到的错误以及所需的输出 - “为什么这不起作用”还不够
  • 您将a,b,c,d 全部作为None 提供给您的函数,然后将它们作为无操作放入其中...为什么?你声明了def N(),但从不使用它——为什么?

标签: python python-2.7 turtle-graphics fractals


【解决方案1】:

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

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2011-10-16
    • 2017-08-31
    • 1970-01-01
    • 2018-07-17
    • 2021-03-16
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多