Dates: Sunday, 29 November 2026
Technique: Flow fields and flow noise for organic pattern generation
Difficulty: Intermediate (requires trigonometry basics)
A flow field creates organic, fluid patterns by simulating particles that follow vector fields. Each particle's movement direction is determined by an underlying "flow" function - often based on noise or mathematical functions.
Here's how a basic flow field works in Python with Pillow:
import math
from PIL import Image
size = 1024
img = Image.new('RGB', (size, size), 'white')
draw = ImageDraw.Draw(img)
# Initialize particles at random positions
particles = []
for _ in range(500):
x = random.randint(0, size)
y = random.randint(0, size)
particles.append({'x': x, 'y': y})
# Flow field strength
strength = 2.0
def get_flow_angle(x, y, frequency=1.0):
"""Get the flow angle at position (x, y) using trigonometry."""
# Map position to noise-like value using sin/cos
angle_x = x * math.pi / size * frequency
angle_y = y * math.pi / size * frequency
return math.atan2(math.sin(angle_x), math.cos(angle_y))
def update_particles(frame):
"""Move particles based on flow field, with slight randomness."""
for p in particles:
# Get base direction from flow field
angle = get_flow_angle(p['x'], p['y'])
# Add time-based animation
frame_factor = math.sin(frame * 0.1)
# Convert angle to velocity (trigonometry!)
vx = -math.cos(angle + frame_factor * 0.5)
vy = -math.sin(angle + frame_factor * 0.5)
# Apply movement with strength multiplier
p['x'] += vx * strength * 2
p['y'] += vy * strength * 2
# Wrap around canvas
if p['x'] < 0: p['x'] = size - 1
if p['x'] >= size: p['x'] = 0
if p['y'] < 0: p['y'] = size - 1
if p['y'] >= size: p['y'] = 0
# Render particles as trails
colors = []
for i in range(256):
r = int(255 * math.sin(i * 0.05))
g = int(255 * math.cos(i * 0.07))
b = int(255 * math.sin(i * 0.03) + 128)
colors.append((r, g, b))
for frame in range(60): # Create one "snapshot" of the flow
update_particles(frame)
for p in particles:
color_idx = int(min(255, max(0, (frame * 3 + p['x']) % 256)))
draw.line([
(p['x'] - 2, p['y'] - 2),
(p['x'] + 2, p['y'] + 2)
], fill=colors[color_idx])
return img
The key insight: sin() and cos() create smooth, cyclic functions that can be used as directional fields. When you use atan2(sin(x), cos(y)), you get a swirling pattern because:
sin(x) oscillates between -1 and 1cos(y) oscillates between -1 and 1atan2(sine, cosine) converts these to angles from -π to πThis creates "vortices" or swirling patterns that look like fluid dynamics without actually simulating fluid physics!
Add more sin/cos terms for complex flow:
def get_flow_angle(x, y):
angle = (
math.sin(x * 0.05) +
math.cos(y * 0.03) +
math.sin((x+y) * 0.02) -
math.cos((x-y) * 0.01)
)
return angle
Make the flow rotate over time:
time_factor = frame_time / FRAME_RATE
angle_offset = math.sin(time_factor) * 0.5
flow_angle = get_flow_angle(x, y) + angle_offset
Use the flow field itself to determine color:
# Map flow angle to hue (0-255)
hue = int((flow_angle + math.pi) / (2 * math.pi) * 256)
For more organic, cloud-like patterns:
# Instead of pure sin/cos:
noise_value = perlin_noise(x / NOISE_SCALE, y / NOISE_SCALE)
flow_angle = math.atan2(noise_value_x, noise_value_y)
This creates a spiral galaxy-like pattern using concentric flow rings:
def galactic_flow(x, y):
cx, cy = center_x, center_y
# Distance from center
dx = x - cx
dy = y - cy
dist = math.sqrt(dx*dx + dy*dy)
# Spiral angle (theta)
theta = dist * SPIRAL_DENSITY
# Radial component (pull inward/outward)
radial = 0.3 * math.sin(dist * RING_FREQUENCY)
# Total angle
return theta + radial
This technique demonstrates how simple trigonometric functions can create complex, organic patterns that resemble natural phenomena like smoke, water currents, and star fields. The beauty of flow fields is their simplicity: no physics simulation needed, just pure mathematical elegance.