简体   繁体   English

为什么此L系统仅画一条直线?

[英]Why is this L-System only drawing a straight line?

I'm trying to write a program that generates strings, and then draws certain lines based on what character in the string is next. 我正在尝试编写一个生成字符串的程序,然后根据字符串中的下一个字符绘制某些线条。 I believe it is generating the strings correctly as I've checked a few iterations by hand, but the turtle I'm using doesn't seem to be working correctly. 我相信它已经正确生成了字符串,因为我已经手动检查了几次迭代,但是我正在使用的乌龟似乎无法正常工作。

For example, the code below should generate the Sierpinski triangle, but only draws a straight line. 例如,下面的代码应生成Sierpinski三角形,但只能绘制一条直线。

I've checked with other L-Systems (such as the dragon curve) and whilst it doesn't produce a horizontal line the results are still incorrect. 我已经检查了其他L系统(例如龙形曲线),虽然它不产生水平线,但结果仍然不正确。 The strings appear to be correct so I think the problem is with how the turtle module is interpreting my instructions. 字符串似乎正确,所以我认为问题出在乌龟模块解释我的指令的方式上。 It's my first time using the module so I wouldn't be surprised if I'd gotten something very basic wrong. 这是我第一次使用该模块,因此如果遇到一些非常基本的错误,我不会感到惊讶。

from tkinter import *
import turtle 

Width=500
Height=500

def process_string(string):
    return string.translate(str.maketrans({'A':'B-A-B','B':'A+B+A'}))


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.4*Width,0.4*Height) #this translation fits more of the curve 
                                   on the screen
    t.pendown()
    t.shape("circle")
    t.speed("fastest")


    for char in string:

        if char=="A" or char=="B":
            t.forward(10)
        elif char=="+":
            t.right(60)
        elif char=="-":
            t.left(60)



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

As before, this example should produce the Sierpinski triangle, but just produces a horizontal line. 如前所述,该示例应产生Sierpinski三角形,但仅产生一条水平线。

As a final quick question that I don't think merits its own post, the turtle documentation says that speed("fastest") should remove all animation, however this isn't the case, any ideas? 作为最后一个快速问题,我认为不应该提出自己的建议,乌龟文档说speed(“ fastest”)应该删除所有动画,但是不是这样,有什么主意吗? Thanks for your time all! 感谢您的时间!

EDIT: I've updated the code with user suggestions and whilst this provides the correct result for the triangle it still gives an incorrect image for the dragon curve. 编辑:我已经用用户建议更新了代码,尽管这为三角形提供了正确的结果,但仍然为龙曲线提供了不正确的图像。 The information for the dragon curve is 龙曲线的信息是

"variables : XY constants : F + − start : FX rules : (X → X+YF+), (Y → −FX−Y) angle : 90° Here, F means "draw forward", − means "turn left 90°", and + means "turn right 90°". X and Y do not correspond to any drawing action and are only used to control the evolution of the curve." “变量:XY常数:F +-开始:FX规则:(X→X + YF +),(Y→-FX-Y)角度:90°在这里,F表示“向前拉”,-表示“向左转90° “和,+表示“向右旋转90°”。X和Y与任何绘制动作都不对应,仅用于控制曲线的演变。”

Problem is 问题是

if char=="A" or "B":

which means 意思是

if (char == "A") or "B":

so it compares char only with "A" and it gives 因此它只将char"A"进行比较,并给出

if True or "B": 

or 要么

if False or "B": 

First gives 首先给

if True:

second gives 第二个给

if "B": 

but this works like 但这就像

if bool("B"): 

which gives 这使

if True: 

So finnally if char=="A" or "B": works like if True: so this part of code is always executed. 最后, if char=="A" or "B":就像if True:一样工作,所以这部分代码总是被执行。


It has to be 它一定要是

if char == "A" or char == "B": 

or 要么

if char in ("A", "B"): 

or 要么

if char in "AB":   

As @cdlane mention in comment you can also use set() 正如@cdlane在评论中提到的那样,您也可以使用set()

if char in {"A", "B"}: 

which need constant time to check char in any size set. 需要恒定的时间来检查任何大小的字符集。 But for small set you will no see difference in time. 但是对于小场景,您将看不到时间上的差异。

Change: 更改:

if char=="A" or "B":

to: 至:

if char=="A" or char=="B":

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

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