An L-system or Lindenmayer system is a parallel rewriting system and a type of formal grammar. An L-system consists of an alphabet of symbols that can be used to make strings, a collection of production rules that expand each symbol into some larger string of symbols, an initial "axiom" string from which to begin construction, and a mechanism for translating the generated strings into geometric structures [LINK]
This project is a Python-based rendering or interpretation of L-systems as per the title The Algorithmic Beauty of Plants by Przemyslaw Prusinkiewicz and Aristid Lindenmayer [Book].
-
Clone this repository:
git clone https://github.com/ambron60/l-system-drawing.git cd l-system-drawing -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
- Command-line interface:
python lsystem.py - Web interface:
streamlit run app.py
- Command-line interface:
- Command-line interface: Interactive terminal-based fractal generator
- Web interface: Beautiful Streamlit web app with real-time visualization
- Dragon Curve - Complex space-filling curve (13 iterations)
- Sierpinski Triangle - Classic triangular fractal pattern
- Koch Curve - Snowflake-like fractal curve
- Hilbert Curve - Space-filling curve
- Axial Tree - Realistic plant growth simulation
- Type-safe code with comprehensive type hints
- Robust error handling and input validation
- Memory protection for large sequences
- Preset system with easy selection (numbers or names)
- Real-time complexity metrics in web interface
- Comprehensive logging for debugging
- Unit tests with 25 test cases
- Configurable plot parameters (size, color, line width)
- Statistics display (sequence length, coordinate points)
- High-quality matplotlib rendering
- Export capabilities for generated fractals
L-systems lie at the heart of this project, enabling users to define custom rules for generating fractals. This project supports any fractal pattern that can be represented by iterative rewriting rules, allowing for versatile and creative fractal generation. Users can specify parameters such as step size, angle, and iteration depth, making it easy to explore a wide range of fractal patterns, from plant-like structures to classic shapes such as the Dragon Curve or Sierpiński Triangle.
- Run
streamlit run app.py - Open your browser to the provided URL
- Select a preset from the dropdown or create custom rules
- Adjust parameters in the sidebar
- Click "Generate L-System" to create your fractal
- Run
python lsystem.py - Choose to use a preset or create custom rules
- Follow the interactive prompts
L-System Fractal Generator
==============================
Use preset L-System? (y/n): y
Available presets:
1. dragon_curve
2. sierpinski_triangle
3. koch_curve
4. hilbert_curve
5. axial_tree
Enter preset number (1-5) or name: 5
Using preset: axial_tree
Enter L-System rules in format 'symbol -> replacement' (enter '0' when done):
Enter rule[1]: F -> F[+F]F[-F]F
Enter rule[2]: 0
Enter axiom (starting sequence): F
Enter number of iterations: 5
Enter segment length: 1
Enter initial heading (degrees): 90
Enter angle increment (degrees): 25
Note: The program now includes robust input validation and will guide you through any errors.
| Preset | Axiom | Rules | Iterations | Angle | Description |
|---|---|---|---|---|---|
| Dragon Curve | FX |
X -> X+YF+, Y -> -FX-Y |
13 | 90° | Complex space-filling curve |
| Sierpinski Triangle | F-G-G |
F -> F-G+F+G-F, G -> GG |
6 | 120° | Classic triangular fractal |
| Koch Curve | F |
F -> F+F-F-F+F |
5 | 90° | Snowflake-like fractal curve |
| Hilbert Curve | A |
A -> -BF+AFA+FB-, B -> +AF-BFB-FA+ |
6 | 90° | Space-filling curve |
| Axial Tree | X |
X -> F-[[X]+X]+F[+FX]-X, F -> FF |
6 | 22.5° | Realistic plant growth |
| Figure | Derivation |
|---|---|
| Koch Island | F -> F-F+F+FF-F-F+F, axiom=F-F-F-F, n=2, α₀=90°, angle=90° |
| Koch (1st variation) | F -> FF-F-F-F-F-F+F, axiom=F-F-F-F, n=3, α₀=90°, angle=90° |
| Koch Islands and Lakes | F -> F+f-FF+F+FF+Ff+FF-f+FF-F-FF-Ff-FFF, f -> ffffff, axiom=F+F+F+F, n=2, α₀=90°, angle=90° |
| Quadratic Snowflake | F -> F+F-F-F+F, axiom=-F, n=4, α₀=90°, angle=90° |
| Hexagonal Gosper curve | L -> L+R++R-L--LL-R+, R -> -L+RR++R+L--L-R, axiom=L, n=4, α₀=60°, angle=60° |
The project includes comprehensive unit tests to ensure reliability:
python test_lsystem.pyTest Coverage:
- ✅ Core L-System functionality (derivation, coordinate generation)
- ✅ Rule parsing and validation
- ✅ Configuration management
- ✅ Preset system handling
- ✅ Error handling and edge cases
- ✅ Complex sequence generation
Results: 25 tests covering all major functionality with 100% pass rate.
- Enhanced Code Architecture: Removed global state, added type safety, improved error handling
- Dual Interface: Both command-line and web-based interfaces
- Built-in Presets: 5 classic L-System patterns ready to use
- Robust Input Validation: Prevents crashes from invalid input
- Comprehensive Testing: 25 unit tests with 100% pass rate
- Memory Protection: Prevents excessive memory usage with large sequences
- Improved User Experience: Numbered preset selection, clear error messages
- Type Hints: Complete type annotations for better code maintainability
- Error Handling: Custom exceptions and graceful error recovery
- Logging: Structured logging for debugging and monitoring
- Configuration Management: Dataclass-based parameter management
- Performance: Optimized coordinate generation and sequence handling
For questions or comments:
- Author: Gianni Perez @ skylabus.tech or at [email protected]
