使用乌龟图形的python中的龙曲线

时间:2018-08-17 22:38:17

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

我尝试使用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

1 个答案:

答案 0 :(得分:1)

为了了解这一点,还回顾了其他Dragon实现。我认为问题在于,您的子孙后代不应再累加了-每个人都可以完全替代前几代人。为了获得想要的效果,我们需要在继续绘制之前将前一代从当前一代的前面去除:

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

enter image description here