Skip to content

A Python implementation of some L-systems described in Chapter 1 of the book «The Algorithmic Beauty of Plants»

License

Notifications You must be signed in to change notification settings

6rampus/lsystems

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

L-systems

A Python implementation of some L-systems described in Chapter 1 of the book The Algorithmic Beauty of Plants.

Table of Contents

Koch construction Plant-like structure
koch-construction plant-like-structure

Definition of L-systems

An L-system is a type of formal grammar that can be defined as a tuple G = ⟨V, ω, P⟩ where

  • V is the alphabet of the system
  • ω is a nonempty word called the axiom
  • P is a finite set of productions

A production is a rewrite rule a→X (indicating that a, the predecessor, can be replaced by X, the successor)

As it is mentioned in the book, the central concept of L-systems is that of rewriting. The initial state of the system is defined by the axiom, a string containing symbols from the alphabet that may or may not be replaceable (variables vs. constants or terminals). From this initial state/string, the rewrite rules or productions are applied iteratively (find symbol as a predecessor in the set of productions, replace in the string by its successor, repeat).

In L-systems, productions are applied in parallel. In other words, each iteration applies as many rewriting rules as possible, which results in all replaceable symbols in the string being replaced in the same iteration. This is the essential difference between L-systems and formal languages generated by formal grammars, which apply only one rule per iteration.

How to use

Python 3 is the only requirement. You can clone the repo and try out some examples that I took from the book like this:

python3 lsystems.py examples/fig-1.24/a.txt

or you can write your own L-systems in a text file with the input format specified below.

Input

This implementation uses the turtle interpretation of strings described in the book, so many of the symbols you can find in it have the same meaning here (with a few exceptions).

Symbol Command
F Move forward a step of length d
f Move forward a step of length d without leaving a trail
+ Turn left by angle δ
- Turn right by angle δ
l Fl in the book. Move forward
r Fr in the book. Move forward
L Ignore, do nothing
R Ignore, do nothing
[ Push the current state of the turtle onto a stack
] Pop a state from the stack and make it the current state of the turtle

A step size or distance d and an angle increment δ are given as parameters

The input should be a .txt file containing just one parameter per line, in this order: number of iterations, step distance, angle increment, axiom, set of productions (one production per line).

number of iterations
step distance
angle increment
axiom
production #1
production #2
...
production #N

For example:

2
4
90
F+F+F+F
F->F+f-FF+F+FF+Ff+FF-f+FF-F-FF-Ff-FFF
f->ffffff

Output

Every execution saves the result in a PostScript file in the folder that contains the input file.

Features

  • Loading L-system parameters (number of iterations, axiom, productions, etc.) from file
  • Loading turtle and screen parameters (trail and background color, starting orientation, etc.) from file
  • 2D turtle interpretation of...
    • deterministic and context-free L-systems (DOL-systems)
    • bracketed OL-systems for branching structures
    • stochastic L-systems
    • context-sensitive L-systems
    • parametric L-systems
  • Modeling in three dimensions
  • Saving results in a PostScript file

Examples

Koch constructions

Quadratic Koch island

Branching structures

Branch-1 Branch-2 Branch-3
Branch-4 Branch-5 Branch-6

About

A Python implementation of some L-systems described in Chapter 1 of the book «The Algorithmic Beauty of Plants»

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages