【问题标题】:Dragon curve in python using turtle graphicspython中的龙曲线使用乌龟图形
【发布时间】:2018-08-17 22:38:16
【问题描述】:

我尝试使用 L 系统制作龙曲线生成器,但我的生成器自身交叉,这不应该发生在正确的龙曲线中,知道为什么吗?

import turtle
from random import randint

#turtle initialization
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
turtle.delay(0)

#L-system set up
start = "fx"
rules = {'x':'x+yf+','y':'-fx-y','f':'f','+':'+','-':'-'}

while True:

    #makes a random color
    t.pencolor('#%02x%02x%02x' % (randint(0,255),randint(0,255),randint(0,255)))

    #makes a new generation of the L-system
    new = ""
    for i in start:
        new += rules[i]

    #applies the rules from text to graphics
    for i in new:
        if i == '+':
            t.right(90)
        elif i == '-':
            t.left(90)
        elif i == 'f':
            t.forward(5)
    start = new

【问题讨论】:

    标签: python-2.x turtle-graphics fractals


    【解决方案1】:

    审查了其他龙的实现以了解这一点。我的信念是问题在于你的世代不应该是累加的——每一代都是前一代的完全替代品。为了得到你想要的效果,我们需要在继续绘制之前从当前一代的前面剥离上一代:

    from turtle import Turtle, Screen, mainloop
    from random import random
    
    # L-system set up
    START = "fx"
    RULES = {'x':'x+yf+', 'y':'-fx-y', 'f':'f', '+':'+', '-':'-'}
    
    LEVEL = 13
    
    screen = Screen()
    screen.tracer(False)
    
    # turtle initialization
    turtle = Turtle(visible=False)
    
    sub_string = string = START
    
    for _ in range(LEVEL):
    
        # make a random color
        turtle.pencolor(random(), random(), random())
    
        # apply the RULES from text to graphics
        for character in sub_string:
            if character == '+':
                turtle.right(90)
            elif character == '-':
                turtle.left(90)
            elif character == 'f':
                turtle.forward(5)
    
        screen.update()
    
        # make a new generation of the L-system
        full_string = "".join(RULES[character] for character in string)
    
        sub_string = full_string[len(string):]  # only the new information
    
        string = full_string  # the complete string for the next generation
    
    screen.tracer(True)
    turtle.hideturtle()
    
    mainloop()
    

    【讨论】:

    • 谢谢你,你完全理解我想要达到的目标:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多