1

I have been working on an implementation of a parametric L-system with python. My case is a 2D tree therefore i am using python turtle library. I wrote this simple code for a non-parametric L-system which it works pretty good. Here is my code:

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

The problem is this code is working for a simple non-parametric L-system but i want to decrease diameter and length of my tree in each step. I have wrote two functions for this, length_function and width_functionbut i don't know where or how to use it. Here is my L-system rules and axiom:

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

The values for r1, r2, q, e are known. also the s0 & w0 are known. I don't want to store the values of these parameters in a string format, i want them in a list or array but i don't know how. Here is a picture of the tree i'm trying to draw vs what my code draws: Parametric Tree enter image description here

shahriar
  • 362
  • 4
  • 17

1 Answers1

0

You correctly store the pensize when you encounter a '[' but you do not reduce it.

Do something like:

if move == "[":
  self.width *= 0.95
  turtle_state.append((T.xcor(), T.ycor(), T.heading(), self.width))
l-w
  • 1