From Simple Rules to Complex Patterns
Circle packing is a beautiful demonstration of how simple local rules can generate complex, emergent patterns. Each circle grows until it touches another circle or the boundary—no global planning required.
The Basic Algorithm
Step 1: Start with empty space
Create a canvas (typically dark background). Place several circles at random positions and radii throughout the space.
Step 2: Grow each circle iteratively
For each circle, check if it can grow more:
- If distance to another circle > sum of their radii, they haven't touched yet
- If distance to boundary > 10 pixels, there's room to grow toward the edge
- If either condition is false, stop growing that circle
Step 3: Grow by a random amount
Add 2-8 pixels to the radius. This randomness prevents perfect symmetry and adds organic variety.
Step 4: Repeat until stable
Continue for 50 iterations or until most circles have stopped growing.
The Code (Simplified)
def check_if_can_grow(circle, all_circles):
for other in all_circles:
if circle == other:
continue
# Distance between centers
dx = circle['x'] - other['x']
dy = circle['y'] - other['y']
distance = sqrt(dx**2 + dy**2)
# If circles are close enough to touch, stop growing
max_combined_radius = circle['radius'] + other['radius']
boundary_distance = min(circle['x'] - canvas_size,
canvas_size - circle['x'],
circle['y'] - canvas_size,
canvas_size - circle['y'])
if distance <= max_combined_radius or boundary_distance <= 10:
return False
return True
The Python script used for this experiment:
#!/usr/bin/env python3
"""Circle packing experiment - circles grow until they touch something.
Each circle expands until it hits another circle or the boundary.
"""
import numpy as np
from PIL import Image
import random
import sys
def create_circle_mask(radius, center_x, center_y, canvas_size):
"""Create a circular mask."""
y, x = np.ogrid[:canvas_size, :canvas_size]
dist = np.sqrt((x - center_x)**2 + (y - center_y)**2)
return (dist <= radius).astype(np.uint8)
def create_circle_packing(canvas_size=1024, seed=None, num_circles=15):
"""Create circle packing where circles grow until they touch something."""
random.seed(seed)
np.random.seed(seed)
canvas = np.ones((canvas_size, canvas_size), dtype=np.uint8) * 20 # Dark background
circles = []
for i in range(num_circles):
center_x = random.randint(50, canvas_size - 50)
center_y = random.randint(50, canvas_size - 50)
radius = random.randint(30, 80)
circles.append({'x': center_x, 'y': center_y, 'radius': radius})
# Grow each circle until it touches something
for iteration in range(50):
new_circles = []
for c in circles:
can_grow = True
for other in circles:
if c == other:
continue
dx = c['x'] - other['x']
dy = c['y'] - other['y']
distance = np.sqrt(dx**2 + dy**2)
max_combined_radius = c['radius'] + other['radius']
boundary_distance = min(c['x'] - canvas_size,
canvas_size - c['x'],
c['y'] - canvas_size,
canvas_size - c['y'])
if distance <= max_combined_radius or boundary_distance <= 10:
can_grow = False
break
if can_grow:
growth = random.randint(2, 8)
c['radius'] += growth
return Image.fromarray(result)
Variations to Try
- Different growth rates: Change the random range from (2-8) to (1-3) for slower, more controlled packing
- More circles: Start with 20-30 circles instead of 15
- Different colors: Modify the color mapping function to use gradients or random hues
- Asymmetric growth: Make some circles grow faster than others based on their starting radius
- Hole filling: After packing, fill small gaps with smaller circles
Why This Works
This is a form of emergent behavior. No circle knows about the global pattern—it only responds to its immediate neighbors. Yet the collective effect is a beautiful, space-filling mosaic that feels organic rather than algorithmic.
The same principle applies to many generative art techniques: flow fields (lines follow velocity vectors), particle systems (each particle has simple motion rules), and cellular automata (each cell checks its neighbors).
Tools You Can Use
- Python + NumPy: Great for mathematical simulations like this
- Processing/py5: Excellent for visual arts with built-in graphics libraries
- P5.js: Web-based version of Processing, runs in browsers
- TouchDesigner: Visual programming for real-time generative art
Remember: the most interesting patterns often come from adding small amounts of randomness to simple rules. That's what makes emergent systems feel alive rather than mechanical.