Alexandre B A Villares


Capa - Pode um robô ser racista?

exemplo e imagem da capa

O código abaixo é a parte principal do código que gera as imagens nas capas deste título da coleção pensando amanhãs do Museu do Amanhã. Cada capa tem um desenho diferente, baseando em uma semente pseudo-aleatória diferente.

import py5  # https://py5coding 
import random

# random seed (semente para os "sorteios" pseudo-aleatórios)
rnd_seed = 1  # este valor muda para cada capa

nodes = {}
unvisited_nodes = []
step = 6
NBS = (
    (-1, -1), (-2,  0),
    (-1,  1), (0, -2),
    (0,  2), (1, -1),
    (2,  0), (1,  1)
)

PALETA = (
    py5.color(200, 0, 200),
    py5.color(0, 200, 0),
    py5.color(0, 200, 200)
)

def setup():
    global w, h, nbs, paleta
    py5.size(1008, 780) 
    w, h = int(py5.width / 2 / step - 5), int(py5.height / 2 / step - 5)
    random.seed(rnd_seed) 
    nbs = list(NBS)
    random.shuffle(nbs)
    paleta = list(PALETA)
    random.shuffle(paleta)
    # pontos iniciais
    x1, y1 = 0, 0
    unvisited_nodes.extend((
        (x1 - 50, y1),
        (x1 + 54, y1 + 7),
        (x1 + 62, y1 + 7),
        (x1 + 50, y1),
        (x1 - 62, y1 - 7),
        (x1 - 54, y1 - 7)))
    # cresce a estrutura 
    nl = -1
    while nl != len(nodes):
        nl = len(nodes)
        unvisited_nodes[:] = grow()
    # desenha a estrutura
    py5.stroke_weight(2)
    py5.background(240)
    py5.fill(0)
    py5.text(f'semente: {rnd_seed}', 35, 12)
    py5.translate(py5.width / 2, py5.height / 2)
    for n, v in nodes.items():
        xa, ya = n
        xb, yb, c, gen = v
        if visible(xa, ya) and visible(xb, yb):
            py5.stroke(paleta[c % len(paleta)])
            py5.line(xa * step, ya * step, xb * step, yb * step)

def grow():
    while unvisited_nodes:
        x, y = unvisited_nodes.pop()
        if not visible(x, y):
            continue
        _, _, c, gen = nodes.get((x, y), (0, 0, len(unvisited_nodes), 0))
        random.seed(gen // 11 + c)
        xnbs = random.sample(nbs, 4)
        for nx, ny in xnbs:
            xnx, yny = x + nx, y + ny
            if (xnx, yny) not in nodes:
                nodes[(xnx, yny)] = (x, y, c, gen + 1)
                yield xnx, yny

def visible(x, y):
    return (abs(x * step) < py5.width / 2 - step * 4 and
            abs(y * step) < py5.height / 2 - step * 4)

py5.run_sketch()