Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou 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

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

Table of Contents

Koch constructionPlant-like structure
koch-constructionplant-like-structure

Definition of L-systems

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

  • V is thealphabet of the system
  • ω is a nonempty word called theaxiom
  • P is a finiteset of productions

Aproduction is a rewrite rulea→X (indicating thata, the predecessor, can be replaced byX, the successor)

As it is mentioned inthe 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 fromthe 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 inthe book, so many of the symbols you can find in it have the same meaning here (with a few exceptions).

SymbolCommand
FMove forward a step of lengthd
fMove forward a step of lengthd without leaving a trail
+Turn left by angleδ
-Turn right by angleδ
lFl in the book. Move forward
rFr in the book. Move forward
LIgnore, do nothing
RIgnore, 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 distanced 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 iterationsstep distanceangle incrementaxiomproduction #1production #2...production #N

For example:

2490F+F+F+FF->F+f-FF+F+FF+Ff+FF-f+FF-F-FF-Ff-FFFf->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-1Branch-2Branch-3
Branch-4Branch-5Branch-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


[8]ページ先頭

©2009-2025 Movatter.jp