Post

Pythagorean Tree

A visual and object-oriented implementation of the Pythagorean Tree using Python, showcasing design patterns like Singleton and Factory, and leveraging NumPy and OpenCV for graphical rendering.

Pythagorean Tree

Light mode only Dark mode only

Pythagorean Tree Generator

Overview

This project generates visual scenes containing Pythagorean trees, using OpenCV and NumPy. Each scene can include a sky, earth, sun, moon, and stars, and multiple fractal trees with customizable colors and styles.

Project Structure

1
2
3
4
5
6
7
8
9
10
11
12
13
PythagoreanTree/
├── main.py
├── tree/
│   ├── __init__.py
│   ├── generator.py
│   ├── drawer.py
│   ├── background.py
│   └── scene_generator.py
├──output/
│   ├── output.png
├── requirements.txt
├──README.md
└──.gitignore

Libraries

Requirements

  • Python 3.x
  • Install dependencies with:
1
pip install -r requirements.txt

Usage

To generate the Pythagorean tree, simply run:

1
python main.py

This will generate multiple images with different background

settings and tree designs.

Tree Customization Each tree can be configured using the following parameters in tree_gen.generate(...):

1
generate(x, y, length, angle, branch_angle, depth)

Tree Generation Parameters

  • x, y: starting position of the tree
  • length: length of the base square
  • angle: starting angle (usually π/2)
  • branch_angle: angle between branches (e.g., np.pi / 4)
  • depth: recursion depth (e.g., 10)

Tree Styling

Each tree dictionary also supports:

  • start_color: RGB tuple for the base color
  • end_color: RGB tuple for the top color
  • gradient: one of "linear", "reverse", "constant"

Scene Configuration

Each scene is defined by a dictionary like:

1
2
3
4
5
6
7
{
    "day": True or False,            # Background color (day/night)
    "sun": True or False,            # Show sun
    "moon": True or False,           # Show moon and stars
    "text": "Scene title",           # Text label
    "file": "output_file.png"        # Output file name
}
This post is licensed under CC BY 4.0 by the author.