1

我一直在用 python 实现参数 L 系统。我的案例是一棵二维树,因此我使用的是 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众所周知的。我不想以字符串格式存储这些参数的值,我希望它们在列表或数组中,但我不知道如何。这是我正在尝试绘制的树与我的代码绘制的图片: 参数树 在此处输入图像描述

4

1 回答 1

0

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

执行以下操作:

if move == "[":
  self.width *= 0.95
  turtle_state.append((T.xcor(), T.ycor(), T.heading(), self.width))
于 2021-03-11T21:07:15.493 回答