简体   繁体   English

为什么我的程序没有生成龙曲线?

[英]Why doesn't my program generate the dragon curve?

I've written a program that uses an L-System to draw fractals.我编写了一个使用 L 系统绘制分形的程序。 It appears to work for the Sierpinski triangle, but not for the dragon curve.它似乎适用于谢尔宾斯基三角形,但不适用于龙曲线。

I'm aware there are a few questions on the site about using turtle graphics to create a dragon curve but none of them seem to be especially applicable so I've opened my own question.我知道网站上有一些关于使用海龟图形创建龙曲线的问题,但似乎没有一个特别适用,所以我提出了自己的问题。 The string my code generates, as the turtle instructions, appears to be correct.作为海龟指令,我的代码生成的字符串似乎是正确的。 I believe the issue is with how turtle is interpreting that string.我相信问题在于乌龟如何解释该字符串。 Why it should work for the Sierpinski triangle and not for the dragon curve is puzzling and led me to think the rules I'd input were wrong, however I've checked multiple sources and they seem correct.为什么它适用于谢尔宾斯基三角形而不适用于龙曲线令人费解,让我认为我输入的规则是错误的,但是我检查了多个来源,它们似乎是正确的。

from tkinter import *
import turtle 

Width=500
Height=500


def process_string(string):
    return string.translate(str.maketrans({'X':'X+YF+','Y':'−FX−Y'}))

def createSystem(seed,depth):
    string=seed
    for i in range(depth):  
        string=process_string(string)
        print(string)
    return(string)

def draw(string):

    t = turtle.RawTurtle(canvas) 
    t.penup()
    t.goto(-0.25*Width,0.25*Height)
    t.pendown()
    t.shape("circle")
    t.speed("fastest")

    for char in string:

        if char=="F":
            t.forward(5)
        elif char=="+":
            t.right(90)
        elif char=="-":
            t.left(90)


root=Tk()
canvas=Canvas(width=Width, height=Height)
canvas.pack()    
draw(createSystem("FX",10))
print("COMPLETE")
root.mainloop()

I would expect to see the dragon curve , however the program just produces a squiggly curve made of lines and boxes.我希望看到龙曲线,但是该程序只是产生了由线条和框组成的波浪形曲线。

In this line:在这一行:

 return string.translate(str.maketrans({'X':'X+YF+','Y':'−FX−Y'}))

You're using the Unicode character "−" for the left turns.您正在使用 Unicode 字符“-”表示左转。 But in this line:但在这一行:

elif char=="-":

You're using a basic ASCII hyphen/minus for the left turn.您在左转时使用的是基本的 ASCII 连字符/减号。 Get these to agree on hyphen/minus and you should release your dragon!让这些在连字符/减号上达成一致,你应该释放你的龙!

Although you have a tkinter wrapper around your embedded turtle, this can also be done using just standalone turtle:尽管您在嵌入的海龟周围有一个 tkinter 包装器,但这也可以仅使用独立的海龟来完成:

from turtle import Screen, Turtle

TRANSLATION = str.maketrans({'X': "X+YF+", 'Y': "-FX-Y"})

def process_string(string):
    return string.translate(TRANSLATION)

def createSystem(string, depth):

    for _ in range(depth):
        string = process_string(string)

    return string

def draw(string):

    for character in string:
        if character == 'F':
            turtle.forward(5)
        elif character == '+':
            turtle.right(90)
        elif character == '-':
            turtle.left(90)

turtle = Turtle()
turtle.shape('circle')
turtle.shapesize(0.2)
turtle.speed('fastest')

draw(createSystem("FX", 10))

turtle.hideturtle()

screen = Screen()
screen.exitonclick()

在此处输入图片说明

I assume you're going to use tkinter to add some controls to your program.我假设您将使用 tkinter 向您的程序添加一些控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM