sketch-a-day/s125/s125.pyde

139 wiersze
3.7 KiB
Plaintext
Czysty Zwykły widok Historia

2018-05-05 23:50:56 +00:00
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s125" # 180505
add_library('serial') # import processing.serial.*;
add_library('arduino') # import cc.arduino.*;
add_library('gifAnimation')
from gif_exporter import *
from inputs import *
def setup():
2018-05-06 00:37:57 +00:00
global input, GIF_EXPORT
2018-05-05 23:50:56 +00:00
size(600, 600)
rectMode(CENTER) # retângulos desenhados pelo centro
2018-05-06 00:37:57 +00:00
textAlign(CENTER, CENTER)
2018-05-05 23:50:56 +00:00
noFill() # sem contorno
frameRate(30)
strokeWeight(3)
2018-05-06 01:28:49 +00:00
Cell.CELLS = []
2018-05-05 23:50:56 +00:00
GIF_EXPORT = False
# Ask user for Arduino port, uses slider if none is selected`
input = Input(Arduino, slider_pins=[1, 2])
create_grid()
def draw():
2018-05-06 01:28:49 +00:00
background(127, 100, 127) # fundo cinza claro
2018-05-05 23:50:56 +00:00
2018-05-06 01:28:49 +00:00
for cell in Cell.CELLS:
2018-05-06 00:37:57 +00:00
cell.draw_()
2018-05-05 23:50:56 +00:00
# uncomment next lines to export GIF
global GIF_EXPORT
2018-05-06 01:28:49 +00:00
if not frameCount % 16 and GIF_EXPORT:
2018-05-05 23:50:56 +00:00
GIF_EXPORT = gif_export(GifMaker,
frames=1000,
2018-05-06 01:28:49 +00:00
delay=400,
2018-05-05 23:50:56 +00:00
filename=SKETCH_NAME)
# Updates reading or draws sliders and checks mouse dragging / keystrokes
input.update()
def keyPressed():
global GIF_EXPORT
if key == 'p': # save PNG
saveFrame("####.png")
if key == 'g': # save GIF
GIF_EXPORT = True
if key == 'h':
input.help()
input.keyPressed()
2018-05-06 00:37:57 +00:00
create_grid()
2018-05-05 23:50:56 +00:00
def keyReleased():
input.keyReleased()
def rnd_choice(collection):
i = int(random(len(collection)))
return collection[i]
def item_at_x_y(x, y, collenction, width_):
return collection[x + y * width_]
def pointy_hexagon(x, y, r):
with pushMatrix():
translate(x, y)
rotate(radians(30)) # pointy, comment out for "flat_hexagon()"
beginShape()
for i in range(6):
sx = cos(i * TWO_PI / 6) * r
sy = sin(i * TWO_PI / 6) * r
vertex(sx, sy)
endShape(CLOSE)
2018-05-06 00:37:57 +00:00
def calculate_neighbours():
pass
2018-05-05 23:50:56 +00:00
def create_grid():
2018-05-06 00:37:57 +00:00
global GRID_SIDE, RAND_SIZE, SPAC_SIZE
2018-05-05 23:50:56 +00:00
# seize inputs
2018-05-06 00:37:57 +00:00
GRID_SIDE = int(input.analog(1) / 16) # 0 a 63 linhas e colunas na grade
2018-05-06 01:28:49 +00:00
#RAND_SIZE = int(input.analog(2) / 16) # escala a randomização do tamanho
randomSeed(int(input.analog(2)) / 4)
2018-05-05 23:50:56 +00:00
# espaçamento entre os elementos
2018-05-06 00:37:57 +00:00
SPAC_SIZE = int(width / (GRID_SIDE + 0.01))
2018-05-05 23:50:56 +00:00
# empty list
2018-05-06 01:28:49 +00:00
Cell.CELLS[:] = []
2018-05-06 00:37:57 +00:00
v = SPAC_SIZE * 1.5
h = SPAC_SIZE * sqrt(3)
2018-05-05 23:50:56 +00:00
for _ in range(1):
2018-05-06 00:37:57 +00:00
for ix in range(GRID_SIDE): # um x p/ cada coluna
2018-05-05 23:50:56 +00:00
# um y p/ cada linha
2018-05-06 00:37:57 +00:00
for iy in range(GRID_SIDE):
2018-05-05 23:50:56 +00:00
if iy % 2:
x = ix * h + h / 4
else:
x = ix * h - h / 4
y = iy * v
2018-05-06 01:28:49 +00:00
Cell.CELLS.append(Cell(x, y))
2018-05-06 00:37:57 +00:00
class Cell():
2018-05-06 01:28:49 +00:00
CELLS = []
def __init__(self, x, y):
2018-05-06 00:37:57 +00:00
self.x = x
self.y = y
2018-05-06 01:28:49 +00:00
self.status = int(random(2))
def update_nc(self):
self.nc = self.neighbours()
def get_color(self):
return map(self.nc, 0, 6, 0, 255)
def get_size(self):
return (SPAC_SIZE / 6) * (1 + self.nc)
2018-05-06 00:37:57 +00:00
def draw_(self):
2018-05-06 01:28:49 +00:00
self.update_nc()
2018-05-06 00:37:57 +00:00
if dist(self.x, self.y, mouseX, mouseY) < SPAC_SIZE * 2:
2018-05-06 01:28:49 +00:00
fill(255)
text(str(self.nc), self.x, self.y)
fill(0, 16)
2018-05-06 00:37:57 +00:00
else:
noFill()
if self.status:
2018-05-06 01:28:49 +00:00
stroke(self.get_color())
pointy_hexagon(self.x, self.y, self.get_size())
2018-05-06 00:37:57 +00:00
def neighbours(self):
count = 0
2018-05-06 01:28:49 +00:00
for cell in Cell.CELLS:
2018-05-06 00:37:57 +00:00
if cell is not self and cell.status:
if dist(self.x, self.y, cell.x, cell.y) < SPAC_SIZE * 2:
2018-05-06 01:28:49 +00:00
count += 1
2018-05-06 00:37:57 +00:00
return count