Skip to content

Instantly share code, notes, and snippets.

@benkiel
Created March 23, 2021 04:15
Show Gist options
  • Save benkiel/bbf06e478e445896b12f0c31f9e4d7a2 to your computer and use it in GitHub Desktop.
Save benkiel/bbf06e478e445896b12f0c31f9e4d7a2 to your computer and use it in GitHub Desktop.
# from https://tylerxhobbs.com/essays/2020/flow-fields
import math
import random
# You need to pip install this in Drawbot
# (i.e., go to Python > Install Python Packages and copy in perlin_noise and select install)
from perlin_noise import PerlinNoise
def remap(v, min, max, newMin, newMax):
return ((v-min)/(max-min))*(newMax-newMin)+newMin
size(1000,1000)
left_x = int(width() * -0.5)
right_x = int(width() * 1.5)
top_y = int(height() * -0.5)
bottom_y = int(height() * 1.5)
resolution = int(height() * 0.01)
noise = PerlinNoise()
num_columns = (right_x - left_x) // resolution
num_rows = (bottom_y - top_y) // resolution
grid = []
for x in range(num_rows):
column = []
sangle = random.uniform(0, pi)
for y in range(num_columns):
scaled_x = x * 0.005
scaled_y = y * 0.005
noise_val = noise((scaled_x, scaled_y))
angle = remap(noise_val,0.0,1.0,0.0,2.0*pi)
column.append(angle)
grid.append(column)
def draw(x,y, steps, step_length, color):
path = BezierPath()
path.moveTo((x,y))
for n in range(0,steps):
if n != 0:
path.lineTo((x,y))
x_offset = x - left_x
y_offset = y - top_y
column_index = int(x_offset / resolution)
row_index = int(y_offset / resolution)
if column_index >= len(grid):
column_index = len(grid)-1
if column_index < 0:
column_index = 0
if row_index >= len(grid[0]):
row_index = len(grid[0])-1
if row_index < 0:
row_index = 0
grid_angle = grid[column_index][row_index]
x_step = step_length * cos(grid_angle)
y_step = step_length * sin(grid_angle)
x = x + x_step
y = y + y_step
stroke(color)
fill(None)
strokeWidth(2)
path.endPath()
drawPath(path)
step_length = 10
xrange = range(0,width())
yrange = range(0,height())
for s in range(600):
x = choice(xrange)
y = choice(yrange)
steps = choice(range(int(height()/20),height()))
color = remap(s,0,599,.1,.9)
draw(x,y,steps,step_length, color)
saveImage("~/Desktop/flowpath.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment