使用python实现参数化L系统

时间:2019-01-23 09:00:49

标签: python python-3.x l-systems

我一直在用python实现参数L系统的实现。我的情况是2D树,因此我正在使用python turtle库。我为一个非参数L系统编写了这个简单的代码,它工作得很好。这是我的代码:

import turtle as T


class LSystem:
    def __init__(self, axiom, width, length, angle):
        self.axiom = axiom
        self.state = axiom
        self.width = width
        self.length = length
        self.angle = angle
        self.rules = {}
        T.pensize(self.width)

    def add_rules(self, *rules):
        for arg in rules:
            self.rules[arg[0]] = arg[-1]

    def single_generation(self):
        next_state = ""
        for char in self.state:
            if char in self.rules:
                next_state += self.rules[char]
            else:
                next_state += char
        self.state = next_state

    def set_turtle(self, my_tuple):
        T.up()
        T.goto(my_tuple[0], my_tuple[1])
        T.seth(my_tuple[2])
        T.down()

    def length_function(self, r):
        self.length *= r

    def width_function(self, q, e):
        self.width *= q**e

    def draw_turtle(self):
        turtle_state = []
        # ***************
        T.tracer(0, 0)
        T.down()
        T.ht()
        T.speed(0)
        T.seth(90)
        # ***************
        for move in self.state:
            if move == "[":
                turtle_state.append((T.xcor(), T.ycor(), T.heading(), T.pensize()))
            elif move == "]":
                xcor, ycor, head, wid = turtle_state.pop()
                self.set_turtle((xcor, ycor, head))
                self.width = wid
            elif move == 'F':
                T.fd(self.length)
                T.pensize(self.width)
            elif move == '+':
                T.left(self.angle)
            elif move == '-':
                T.right(self.angle)
        T.getscreen()
        T.done()


if __name__ == '__main__':
    my_axiom = "A"
    my_width = 10
    my_length = 60
    my_angle = 33.5
    LSys = LSystem(my_axiom, my_width, my_length, my_angle)
    my_rule = ("A", "F[+A][-A]")
    LSys.add_rules(my_rule)
    LSys.single_generation()
    LSys.single_generation()
    LSys.single_generation()
    LSys.single_generation()
    LSys.single_generation()
    LSys.draw_turtle()

问题是此代码适用于简单的非参数L系统,但我想在每一步中减小树的直径和长度。我为此编写了两个函数length_functionwidth_function,但我不知道在哪里或如何使用它。这是我的L系统规则和公理:

A(s,w) ------> F(s,w)[+A(s*r1, w* q^e)][-A(s*r2, w* (1-q)^e]
axiom = "A(s0, w0)"

r1, r2, q, e的值是已知的。 s0 & w0也是已知的。我不想以字符串格式存储这些参数的值,我希望将它们存储在列表或数组中,但我不知道如何。这是我要绘制的树与我的代码绘制的图片: Parametric Tree enter image description here

1 个答案:

答案 0 :(得分:0)

当您遇到“[”时,您正确地存储了笔尺寸,但没有减少它。

做类似的事情:

if move == "[":
  self.width *= 0.95
  turtle_state.append((T.xcor(), T.ycor(), T.heading(), self.width))
相关问题