Пример #1
0
    def draw(self):
        steps = int(lerp(1, 1024, self.step))
        x_density = int(lerp(self.w / 256, self.w / 4, self.param_a))
        y_density = int(lerp(self.h / 256, self.h / 4, self.param_a))
        padding = lerp(0, 0.75, self.param_b)
        line_width = lerp(0.1, 8.0, self.param_c)
        conv = lerp(1.0, 8.0, self.param_d)

        # set wanderer starting positions
        grid = Grid(0, 0, 1, 1, 12, 12, padding=padding)
        starts = grid.centers()

        # wander points through field
        speed = 0.0005
        paths = []
        for start in starts:
            path = empty([steps, 2])
            path[0] = start
            for i in range(1, steps):
                # last position
                x, y = path[i - 1]
                vec = self.vector_at((x, y), mult=conv)
                # new position
                nx = x + vec[0] * speed
                ny = y + vec[1] * speed
                path[i] = [nx, ny]
            paths.append(path)

        # DEBUG: display flow field (interpolated points)
        if self.option_d:
            self.set_color(self.color_d)
            rad = (self.max_x - self.min_x) / x_density / 3
            for y in linspace(self.min_y, self.max_y, x_density):
                for x in linspace(self.min_x, self.max_x, y_density):
                    vec = self.vector_at((x, y))
                    a = array((x, y))
                    b = a + vec * 0.1
                    self.set_color(self.sample_layer_in((x, y)))
                    self.circle(a, rad, fill=True)

            self.set_line_width(60 / x_density)
            self.set_color([1, 1, 1, 1])
            for y in linspace(self.min_y, self.max_y, x_density):
                for x in linspace(self.min_x, self.max_x, y_density):
                    vec = self.vector_at((x, y))
                    a = array((x, y))
                    b = a + vec * 0.1
                    self.line(a, b)

        # draw wandering paths
        self.set_color(self.color_a)
        self.ctx.set_line_cap(LINE_CAP_ROUND)
        self.set_line_width(line_width)
        for path in paths:
            self.path(path)
Пример #2
0
def render():
    # size = 120
    size = 8
    grid = Grid(-1, -1, 2, 2, rows=size, cols=size)

    ctx.clear([0, 0, 0, 1])
    ctx.set_range(-1, 1)

    for xy in grid.centers():
        x, y = xy

        print(x, y)
        color = ctx.sample_point(x, y)
        ctx.set_color(color)

        ctx.circle(x, y, 0.1, fill=True)
Пример #3
0
# triangle to wedge
# from geom.circle import Circle
# circ = Circle()
# tri = circ.as_polygon(n=3)
# tri = tri.rotate(pi / 2)
# tri.draw(ctx)
# x, y = tri.center()
# ctx.circle(x, y, 0.02)

# ptsA = tri.points
# ptsB = roll(ptsA, 1, axis=0)

# arcs = []
# for a, b in zip(ptsA, ptsB):
    # arc = Arc.arcFrom2Points(b, a, 1.8)
    # arc.draw(ctx)

from geom.grid import Grid
grid = Grid(-0.25, -0.25, count, count, count, count)
for pt in grid.centers():
    x, y = pt
    ctx.circle(x, y, 0.01)

grid = Grid(0.25, 0.25, count, count, count, count)
ctx.set_color([1, 1, 1, 1])
for pt in grid.centers():
    x, y = pt
    ctx.circle(x, y, 0.01)

ctx._ctx
Пример #4
0
line_width_a = lerp(0.0005, 0.05, param_c)
fill_a = option_b

show_grid_b = option_c
count_b = int(lerp(1, 128, param_d))
size_b = lerp(0.0005, 0.05, param_e)
line_width_b = lerp(0.0005, 0.05, param_f)
fill_b = option_d


def draw_grid(grid, r, fill):
    print(grid, r, fill)
    for pt in grid.centers():
        x, y = pt
        ctx.circle(x, y, r, fill=fill)


if show_grid_a:
    x_off, y_off = point_a
    grid = Grid(x_off - 1, y_off - 1, 2, 2, count_a, count_a)
    ctx.set_color(color_a)
    draw_grid(grid, size_a, fill_a)

if show_grid_b:
    x_off, y_off = point_b
    grid = Grid(x_off - 1, y_off - 1, 2, 2, count_b, count_b)
    ctx.set_color(color_b)
    draw_grid(grid, size_b, fill_b)

# ctx._ctx
Пример #5
0
#%%
from numpy.random import choice
from numpy import empty, append
from numpy import cumsum
from numpy import column_stack

from geom.grid import Grid

# create grid of center points
size = 400
rows = 4
cells = Grid(size, size, rows, rows).cells()
centers = array(list(map(lambda cell: cell.center(), cells)))
centers = centers.flatten()
centers = centers.reshape(32, 1)
centers.shape

# generate _n_ random walks per center
n = 120000
count = rows * rows * n * 2
choices = choice([-1, 0, +1], count)
choices = choices.reshape((rows * rows * 2, n))
choices.shape

# offset each x, y choice row by center x, y 
choices = centers + choices

# reshape array
from numpy import array, concatenate
xys = []
for cell in list(choices.reshape((rows * rows,2,n))):
Пример #6
0
#%%
from geom.grid import Grid

grid_size = 64
cells = Grid(1, 1, grid_size, grid_size, padding=0.05)
centers = cells.centers()
centers

from modules.perlin import generate_perlin_noise_2d
pnoise = generate_perlin_noise_2d((512, 512), (8, 8))
pnoise

from geom.circle import Circle
from numpy import linspace
from numpy import interp
from math import sin, cos, pi
from numpy.random import rand

large_rad = (1.0 / grid_size / 2.0) * 0.9
small_rad = large_rad * 0.333
circle_count = 9
rads = linspace(large_rad, small_rad, circle_count)
def circle_stack(pos):
    noise = pnoise[int(pos[0] * 255), int(pos[1] * 255)]
    theta = interp(noise, [-1.0, 1.0], [0, 2 * pi])
    scale = rand() * large_rad * 0.666
    
    x_off = linspace(0, sin(theta) * scale, circle_count)
    y_off = linspace(0, cos(theta) * scale, circle_count)
    
    stack = []
Пример #7
0
# perlin noise -> vector flow field
from perlin import generate_perlin_noise_2d
import numpy as np
np.random.seed(0)
noise_density = 512
noise = generate_perlin_noise_2d((noise_density, noise_density), (16, 8))
noise.shape
xs = np.sin(noise)
ys = np.cos(noise)
# xs.shape
flow_field = np.column_stack((xs, ys)).reshape(noise_density, noise_density, 2)

# 3 x 3 rectangular grid cells
from geom.grid import Grid
grid_size = 5
cells = Grid(1, 1, grid_size, grid_size, padding=0.002).cells()

# shrinking nested rectangles
from numpy import linspace, array

scale = linspace(1, 0.3, 32)
resamp_num = 64
polys = []
for cell in cells:
    # outer rect never mutates
    # polys.append(cell.as_polygon())

    # build inner rects
    for s in scale:
        p = cell.scale(s, s)
        p = p.resample(num=resamp_num)
Пример #8
0
#%%
from geom.grid import Grid
from geom.line import Line
from numpy.random import random

count = 16
grid = Grid(1, 1, count, count, padding=-0.001)
lines = []
for rect in grid.cells():
    poly = rect.resample(num=40)
    c = rect.center() + ((random(2) - 0.5) * 0.03)
    for vert in poly.vertices():
        lines.append(Line(c, vert))

# len(lines)
    
%load_ext helpers.ipython_cairo
from modules.render import Render
from modules.color import hex_to_rgba

CANVAS_SQUARE_SIZE = 2400
PIXEL = 1.0 / CANVAS_SQUARE_SIZE
BACKGROUND = hex_to_rgba("#EEAA22")
FOREGROUND = hex_to_rgba("#000000")

render = Render(CANVAS_SQUARE_SIZE, BACKGROUND, FOREGROUND)
render.remap(domain=(0, 1), range=(0, 1))
render.set_line_width(1.6 * PIXEL)

for line in lines:
    a, b = line.vertices()