Friday, December 12, 2025

x̄ - > Python fractal tree

Fractal Tree

from turtle import *
from colorsys import hsv_to_rgb
from random import random

# Make drawing faster by reducing screen updates
tracer(10)

# Set background to black (tree glows against it)
bgcolor('black')

# Point turtle upwards and move to bottom of screen
left(90)
up()
goto(0, -200)
down()

def draw_tree(length):
    # Stop recursion when the branch is too small
    if length < 5:
        return
    else:
        # Branch color using a gradient from green → brown
        h = 0.3 - (length / 200) * 0.3
        r, g, b = hsv_to_rgb(h, 1, 1)

        # Set branch color and thickness
        pencolor(r, g, b)
        pensize(max(1, length / 12))

        # Draw trunk segment
        forward(length)

        # LEAVES:
        # When branches are short, add small colored dots
        if length < 25:
            for _ in range(3):
                leaf_h = random()
                lr, lg, lb = hsv_to_rgb(leaf_h, 0.8, 1)
                pencolor(lr, lg, lb)
                dot(7)  # round leaf

        # Right branch
        right(25)
        draw_tree(length * 0.7)

        # Left branch
        left(50)
        draw_tree(length * 0.7)

        # Restore angle
        right(25)

        # Move back to original position after drawing branch
        pencolor(r, g, b)
        backward(length)

# Initial trunk length (start recursion)
draw_tree(100)

done()
  

🌿 Brief, Clear Explanation

Think of this code as a patient gardener carving a tree from pure geometry — a quiet, recursive dance of branches.

1. Turtle Setup

The turtle faces upward and is placed near the bottom of the screen. tracer(10) speeds up the drawing by reducing screen refreshes.

2. Color Magic (HSV → RGB)

Instead of flat RGB colors, the tree uses shifting hues:
• Long branches lean brown
• Shorter ones glow green This gradient gives the tree a natural, lifelike feel.

3. Recursion — the Heartbeat

draw_tree() calls itself twice: once for the right branch, once for the left. Each child branch is 70% of its parent, creating the fractal structure.

4. Leaves

When a branch becomes small, the code sprinkles colorful leaf dots using random hues — giving the tree a sense of blooming.

5. Returning Home

After each branch is drawn, the turtle walks backward along the same branch to its starting point. This ensures the geometry remains correct as new branches sprout.

6. One Seed → An Entire Tree

draw_tree(100) is the seed from which the whole tree grows. A single value blossoms into an entire structure through recursion.

No comments:

Meet the Authors
Zacharia Maganga’s blog features multiple contributors with clear activity status.
Active ✔
πŸ§‘‍πŸ’»
Zacharia Maganga
Lead Author
Active ✔
πŸ‘©‍πŸ’»
Linda Bahati
Co‑Author
Active ✔
πŸ‘¨‍πŸ’»
Jefferson Mwangolo
Co‑Author
Inactive ✖
πŸ‘©‍πŸŽ“
Florence Wavinya
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Esther Njeri
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Clemence Mwangolo
Guest Author

Followers

Support This Blog
Tap Donate now here to donate or go to donate on top menu to scan QR and support this site.
Donate Now