Perlin noise... what is it?
For this I am NOT allowing myself to use any resources other than the Perlin Noise Wiki page.
random_grad(0.3, 23)
nx, ny = (60, 30)
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
xv, yv = np.meshgrid(x, y)
grads = random_grad(xv, yv) # I love numpy - this works just as well as passing individual vals
grads = (grads + 1)/2 # Shift to (0, 1) for viz
im = np.stack([np.zeros((ny, nx)), grads[0], grads[1]], axis=-1)
print(im.shape)
plt.imshow(im)
perlin(0.1, 0.3)
plt.imshow(perlin(xv, yv))
fig, (ax0, ax1, ax2) = plt.subplots(1, 3)
ax0.imshow(perlin_grid(200, 200))
ax1.imshow(perlin_grid(200, 200, x_bounds=(0, 5), y_bounds=(0, 5)))
ax2.imshow(perlin_grid(200, 200, x_bounds=(0, 20), y_bounds=(0, 4)))
n0 = perlin_grid(200, 200, x_bounds=(0, 2), y_bounds=(0, 2))
n1 = perlin_grid(200, 200, x_bounds=(0, 5), y_bounds=(0, 5))
n2 = perlin_grid(200, 200, x_bounds=(0, 50), y_bounds=(0, 50))
plt.imshow(n0+n1+n2)
# Playing with CAIRO
import ipywidgets as widgets
import random
# Set up surface
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512) # Create the surface
ctx = cairo.Context(surface)
widget = widgets.Image(
value=surface.write_to_png(),
format='png',
width=512,
height=512,
)
display(widget)
def draw_turtle(t, ctx, size=512):
x, y = t['x']*size, t['y']*size
ctx.move_to(x, y)
ctx.line_to(x, y+4)
ctx.line_to(x+4, y+4)
ctx.line_to(x+4, y)
ctx.line_to(x, y)
ctx.close_path()
ctx.set_source_rgba(1, 0.5, 0.1, 1)
ctx.fill_preserve()
ctx.set_source_rgb(0, 0, 0)
ctx.set_line_width(1)
ctx.stroke()
turtles = [{'x':random.random(), 'y':random.random()} for _ in range(100)]
for i in range(100):
# Draw
for t in turtles:
draw_turtle(t, ctx)
widget.value = surface.write_to_png()
# Move:
for t in turtles:
angle = perlin(t['x'], t['y']) * 3
t['x'] += np.sin(angle)*0.01
t['y'] += np.cos(angle)*0.01
surface.write_to_png('outputs/perlin_turtles.png') # For later display
from IPython.display import Image
Image('outputs/perlin_turtles.png')
# Set up surface
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1024, 1024) # Create the surface
ctx = cairo.Context(surface)
turtles = [{'x':0.1 + random.random()*0.8, 'y':0.25 * random.random()/2} for _ in range(100)]
x_scale = random.random()*20
y_scale = random.random()*20
for i in range(100):
# Draw
for t in turtles:
draw_turtle(t, ctx, size=1024)
# Move:
for t in turtles:
angle = perlin(t['x']*x_scale, t['y']*y_scale) * 3
t['x'] += np.sin(angle)*0.01
t['y'] += np.cos(angle)*0.01
surface.write_to_png('outputs/perlin_turtles2.png') # For later display
Image('outputs/perlin_turtles2.png')