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
Barnsley Fern in Python
The Barnsley Fern is a fractal pattern created by mathematician Michael Barnsley that resembles the shape of a natural fern. It is generated using four mathematical transformations known as an Iterated Function System (IFS), where each transformation has different probabilities of being selected.
Mathematical Foundation
The Barnsley Fern uses four affine transformations, each with the general form ?
The four transformations have different coefficients and probabilities ?
| Transformation | a | b | c | d | e | f | Probability | Description |
|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0.16 | 0 | 0 | 1% | Stem |
| 2 | 0.85 | 0.04 | -0.04 | 0.85 | 0 | 1.6 | 85% | Leaflets |
| 3 | 0.2 | -0.26 | 0.23 | 0.22 | 0 | 1.6 | 7% | Left leaflet |
| 4 | -0.15 | 0.28 | 0.26 | 0.24 | 0 | 0.44 | 7% | Right leaflet |
Implementation in Python
import matplotlib.pyplot as plt
import random
# Initialize coordinates with starting point
x_coords = [0]
y_coords = [0]
# Generate 100,000 points for a detailed fern
for i in range(1, 100000):
# Get current coordinates
x_current = x_coords[i-1]
y_current = y_coords[i-1]
# Generate random number to select transformation
rand_num = random.randint(1, 100)
# Apply transformation based on probability
if rand_num == 1:
# Stem (1% probability)
x_new = 0
y_new = 0.16 * y_current
elif 2 <= rand_num <= 86:
# Main leaflets (85% probability)
x_new = 0.85 * x_current + 0.04 * y_current
y_new = -0.04 * x_current + 0.85 * y_current + 1.6
elif 87 <= rand_num <= 93:
# Left leaflet (7% probability)
x_new = 0.2 * x_current - 0.26 * y_current
y_new = 0.23 * x_current + 0.22 * y_current + 1.6
else:
# Right leaflet (7% probability)
x_new = -0.15 * x_current + 0.28 * y_current
y_new = 0.26 * x_current + 0.24 * y_current + 0.44
# Add new coordinates to lists
x_coords.append(x_new)
y_coords.append(y_new)
# Create the plot
plt.figure(figsize=(8, 10))
plt.scatter(x_coords, y_coords, s=0.1, color='green', alpha=0.7)
plt.title('Barnsley Fern Fractal')
plt.axis('equal')
plt.tight_layout()
plt.show()
How the Algorithm Works
The algorithm starts with a single point (0, 0) and iteratively applies one of four transformations based on weighted random selection. Each transformation contributes to different parts of the fern ?
- Transformation 1 (1%) ? Creates the stem by mapping points to nearly the origin
- Transformation 2 (85%) ? Generates the main body and leaflets through scaling and rotation
- Transformations 3 & 4 (7% each) ? Create the left and right smaller leaflets
Optimized Version with Color Coding
import matplotlib.pyplot as plt
import numpy as np
def generate_barnsley_fern(num_points=50000):
# Store points and their transformation types
x_coords = np.zeros(num_points)
y_coords = np.zeros(num_points)
colors = np.zeros(num_points)
x, y = 0, 0
for i in range(1, num_points):
rand = np.random.random()
if rand < 0.01:
# Stem
x, y = 0, 0.16 * y
colors[i] = 1
elif rand < 0.86:
# Main leaflets
x, y = 0.85*x + 0.04*y, -0.04*x + 0.85*y + 1.6
colors[i] = 2
elif rand < 0.93:
# Left leaflet
x, y = 0.2*x - 0.26*y, 0.23*x + 0.22*y + 1.6
colors[i] = 3
else:
# Right leaflet
x, y = -0.15*x + 0.28*y, 0.26*x + 0.24*y + 0.44
colors[i] = 4
x_coords[i] = x
y_coords[i] = y
return x_coords, y_coords, colors
# Generate fern
x, y, c = generate_barnsley_fern(75000)
# Plot with different colors for each transformation
plt.figure(figsize=(10, 12))
scatter = plt.scatter(x, y, c=c, s=0.1, cmap='Greens', alpha=0.8)
plt.colorbar(scatter, label='Transformation Type')
plt.title('Barnsley Fern with Transformation Coloring', fontsize=14)
plt.axis('equal')
plt.tight_layout()
plt.show()
Key Parameters
- Number of iterations ? More points create finer detail (50,000-100,000 recommended)
- Point size ? Smaller values (0.1-0.2) show more intricate patterns
- Color and transparency ? Green colors with alpha blending enhance the natural appearance
Conclusion
The Barnsley Fern demonstrates how simple mathematical rules can create complex, natural-looking patterns. By using weighted random selection of affine transformations, we can generate a fractal that closely resembles a real fern, showcasing the beauty of mathematical modeling in nature.
