sketch-a-day/s051/s051.pyde

86 wiersze
2.7 KiB
Plaintext
Czysty Zwykły widok Historia

2018-02-20 10:45:27 +00:00
"""
sketch 51 180220 - Alexandre B A Villares
https://abav.lugaralgum.com/sketch-a-day
"""
import random as rnd
2018-02-20 11:01:42 +00:00
CEL_SIZE = 64
2018-02-20 10:45:27 +00:00
HALF_CEL = CEL_SIZE / 2
2018-02-20 11:01:42 +00:00
MARGIN = 100
GRADE_PONTOS = [] # lista de tuplas (x, y)
DESENHO = [] # lista de elementos
2018-02-20 10:45:27 +00:00
def setup():
size(712, 712)
h, w = height - 2 * MARGIN, width - 2 * MARGIN
noFill()
n_rows, n_cols = int(h / CEL_SIZE), int(w / CEL_SIZE)
for r in range(n_rows):
for c in range(n_cols):
x, y = HALF_CEL + c * CEL_SIZE,\
HALF_CEL + r * CEL_SIZE
2018-02-20 11:01:42 +00:00
GRADE_PONTOS.append((x + MARGIN, y + MARGIN)) # acrescenta ponto
2018-02-20 10:45:27 +00:00
2018-02-20 11:01:42 +00:00
novo_desenho()
2018-02-20 10:45:27 +00:00
println("'s' to save, and 'n' for a new drawing")
2018-02-20 11:01:42 +00:00
def novo_desenho():
DESENHO[:] = [] # esvazia a lista de setas e linhas
2018-02-20 10:45:27 +00:00
for _ in range(30):
2018-02-20 11:01:42 +00:00
x, y = rnd.choice(GRADE_PONTOS)
DESENHO.append((
2018-02-20 10:45:27 +00:00
x, # x
y, # y
rnd.choice([10, 20, 30]), # size
rnd.choice([2, 4, 6]), # strokeWeight
2018-02-20 11:01:42 +00:00
rnd.choice([True, False]), # arrow (se é seta)
list() # other nodes (para onde aponta)
2018-02-20 10:45:27 +00:00
))
2018-02-20 11:01:42 +00:00
for node in DESENHO: # para cada elemento do desenho
rnd_node = rnd.choice(DESENHO) # sorteia outro elemento
2018-02-20 10:45:27 +00:00
x1, y1, x2, y2 = node[0], node[1], rnd_node[0], rnd_node[1]
2018-02-20 11:01:42 +00:00
if (x1, y1) != (x2, y2): # se não for no mesmo ponto
node[-1].append(rnd_node) # "aponta" para este elemento
# pode acontecer de um elemento não apontar para ninguém
2018-02-20 10:45:27 +00:00
def seta(x1, y1, x2, y2, shorter=12, head=12):
2018-02-20 11:01:42 +00:00
"""
O código para fazer as setas, dois pares (x, y),
um parâmetro de encurtamento: shorter
e para o tamanho da cabeça da seta: head
"""
2018-02-20 10:45:27 +00:00
L = dist(x1, y1, x2, y2)
with pushMatrix():
translate(x2, y2)
angle = atan2(x1 - x2, y2 - y1)
rotate(angle)
offset = -shorter * .6
line(0, offset, 0, -L - offset)
line(0, offset, -head / 3, -head + offset)
line(0, offset, head / 3, -head + offset)
def draw():
background(200)
2018-02-20 11:01:42 +00:00
# para cada elemento do desenho, pegue as coordenandas e atributos
for x1, y1, d1, sw, arrow, points_to in DESENHO:
strokeWeight(sw)
for other in points_to:
x2, y2 = other[0], other[1]
if arrow: # se for arrow == True, desenhe seta preta
2018-02-20 10:45:27 +00:00
stroke(0)
2018-02-20 11:01:42 +00:00
# x1, y1, x2, y2, circle offset, arrow head size
seta(x1, y1, x2, y2, d1, sw * 5)
else: # senhão desenhe linha branca e círculo branco
stroke(255)
line(x1, y1, x2, y2)
ellipse(x1, y1, d1, d1)
2018-02-20 10:45:27 +00:00
def keyPressed():
if key == 's':
saveFrame("####.png")
if key == 'n':
2018-02-20 11:01:42 +00:00
novo_desenho()