Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fractal Trees in Python
Fractal patterns are everywhere in nature − a small branch resembles the entire tree, a fern leaf's part looks like the whole leaf. This self-repeating pattern concept is called a fractal tree. Python provides several modules to generate beautiful fractal trees programmatically.
What is a Fractal Tree?
A fractal tree is a mathematical structure that exhibits self-similarity at different scales. Each branch splits into smaller branches following the same pattern, creating a recursive tree-like structure. The recursion continues until reaching a specified depth.
Using pygame Module
The pygame module provides graphics functions to draw fractal trees. We define screen dimensions and recursion depth, then use mathematical functions to calculate branch positions and angles ?
Example
import pygame
import math
# Initialize pygame
pygame.init()
screen = pygame.display.set_mode((750, 650))
pygame.display.set_caption("Fractal Tree")
display = pygame.display.get_surface()
def drawTree(x, y, angle, depth):
if depth:
# Calculate end point of current branch
end_x = x + int(math.cos(math.radians(angle)) * depth * 10.0)
end_y = y + int(math.sin(math.radians(angle)) * depth * 10.0)
# Draw the branch
pygame.draw.line(display, (127, 255, 0), (x, y), (end_x, end_y), 1)
# Recursively draw left and right branches
drawTree(end_x, end_y, angle - 25, depth - 1)
drawTree(end_x, end_y, angle + 25, depth - 1)
def handle_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# Fill screen with black background
screen.fill((0, 0, 0))
# Start drawing from bottom center, pointing upward
drawTree(375, 650, -90, 10)
pygame.display.flip()
# Keep window open
while True:
handle_events()
Output
Running the above code creates a green fractal tree with 10 levels of branching ?
Using Turtle Module
The turtle module offers a simpler approach with built-in drawing functions. The turtle moves forward to draw branches, turns at specific angles, and recursively creates smaller branches ?
Example
import turtle
def draw_tree(length, t):
if length > 10:
# Draw current branch
t.forward(length)
# Turn right and draw right subtree
t.right(25)
draw_tree(length - 15, t)
# Turn left and draw left subtree
t.left(50)
draw_tree(length - 15, t)
# Return to original position and angle
t.right(25)
t.backward(length)
def create_fractal_tree():
# Setup turtle and screen
t = turtle.Turtle()
screen = turtle.Screen()
# Position turtle at bottom, pointing up
t.left(90)
t.penup()
t.backward(100)
t.pendown()
# Set drawing properties
t.color("green")
t.speed(0)
# Draw the fractal tree
draw_tree(85, t)
# Keep window open until clicked
screen.exitonclick()
# Run the program
create_fractal_tree()
Output
This creates an interactive window displaying a green fractal tree. Click the window to close it ?
Comparison
| Method | Complexity | Best For | Interactive |
|---|---|---|---|
| Pygame | Higher | Complex graphics, animations | Custom event handling |
| Turtle | Lower | Educational purposes, simple drawings | Built-in click events |
Conclusion
Fractal trees demonstrate the beauty of recursive patterns in nature and programming. Pygame offers more control for complex graphics, while Turtle provides a beginner-friendly approach for learning fractal concepts.
