Alexandre B A Villares 2021-07-17 17:11:58 -03:00
rodzic bf3e388225
commit c16f8ace4a
4 zmienionych plików z 195 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,127 @@
SIN_60 = sqrt(3) * 0.5 # sin(radians(60))
class MockCell:
state = 0
gen = 0
class Cell():
board = dict()
EVN_NBS = ((0, 1), (0, -1), (-1, 0), (1, 0), (-1, -1), (1, -1))
ODD_NBS = ((0, 1), (0, -1), (-1, 0), (1, 0), (-1, 1), (1, 1))
W = 4
H = SIN_60 * W
last_clicked = None
def __init__(self, i, j, rnd=False):
self.board[(i, j)] = self
self.i = i
self.j = j
self.x = i * self.W * 1.5 + self.W
if i % 2 == 0:
self.y = j * self.H * 2 + self.H
else:
self.y = j * self.H * 2 + self.H * 2
if rnd:
self.state = 1 if random(20000 / (1 + j)) < 1 else 0
else:
self.state = 0
self.next_state = self.state
self.gen = 0
self.nbs = 0
def display(self):
# with pushMatrix():
# translate(self.x, self.y)
# noStroke()
# live_nbs = self.calc_live_nbs()
# fill((self.i * 2) % 128, (self.j * 2) % 128, 32 + 128 * self.state)
# fill(255 * self.state)
# circle(0, 0, self.W * 2)
if self.state:
# strokeWeight(self.H * 2 * self.j / 100)
# strokeWeight(4.5 - (abs(self.gen / 5.0) % 4))
if not keyPressed:
moving_gen = (self.gen - frameCount / 2) % (3141 * 5)
sw = self.W + self.W / 2.0 * sin(moving_gen / 5.0)
strokeWeight(sw)
else:
strokeWeight(self.W * 1.5)
stroke(0, 0, 16 * self.nbs)
point(self.x, self.y)
def calc_live_nbs(self):
nbs = self.EVN_NBS if self.i % 2 == 0 else self.ODD_NBS
return sum(self.get_neighbour(i_offset, j_offset).state
for i_offset, j_offset in nbs)
def get_neighbour(self, i_offset, j_offset):
return self.board.get((self.i + i_offset,
self.j + j_offset),
MockCell())
def get_live_nbs(self):
nbs = self.EVN_NBS if self.i % 2 == 0 else self.ODD_NBS
return [self.get_neighbour(i_offset, j_offset)
for i_offset, j_offset in nbs
if self.get_neighbour(i_offset, j_offset).state]
def calc_next_state(self):
self.nbs = nbs = self.calc_live_nbs()
# if nbs == 1:
# self.next_state = 1
# lnbs = self.get_live_nbs()
# if lnbs:
# self.gen = lnbs[0].gen + 1
# elif nbs > 4:
# self.next_state = 0
# else:
# self.next_state = self.state
# # self.gen -= 1
# # muito bom com bordas 1 tb
if nbs == 2 and self.state == 0:
self.next_state = 1
lnbs = self.get_live_nbs()
if lnbs:
self.gen = lnbs[-1].gen + 1
elif nbs > 4:
self.next_state = 0
else:
self.next_state = self.state
# dia 14
# if nbs == 1 and self.state == 0:
# self.next_state = 1
# lnbs = self.get_live_nbs()
# if lnbs:
# self.gen = lnbs[0].gen + 1
# elif nbs > 3:
# self.next_state = 0
# else:
# self.next_state = self.state
def check_click(self):
if (self.mouse_over() and
self.__class__.last_clicked != self):
self.state ^= 1
self.__class__.last_clicked = self
def mouse_over(self):
return dist(self.x, self.y, mouseX, mouseY) < self.H
@staticmethod
def hexagon(w):
h = SIN_60 * w
with beginShape():
vertex(-w, 0)
vertex(-w / 2, -h)
vertex(w / 2, -h)
vertex(w, 0)
vertex(w - w / 2, h)
vertex(-w / 2, h)
vertex(-w, 0)

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 874 KiB

Wyświetl plik

@ -0,0 +1,62 @@
from cell import Cell
play = former_play_state = False
sample_rate = 1
def setup():
global cols, rows, board
size(600, 600)
# noSmooth()
cols = width / int(Cell.W * 1.5)
rows = height / int(Cell.H * 2) - 1
init_board()
stroke(0)
def draw():
# scale(0.95)
# translate(-Cell.W / 2, -Cell.H / 2)
background(240)
for cell in Cell.board.values():
cell.display()
if play and frameCount % sample_rate == 0:
next_board()
def keyPressed():
global play
if key == 'e':
init_board()
elif key == 'r':
init_board(rnd=True)
elif key == ' ':
play = not play
def mouseDragged():
toggle()
def mousePressed():
global play, former_play_state
former_play_state = play
play = False
toggle()
def mouseReleased():
global play
play = former_play_state
Cell.last_clicked = None
def next_board():
for cell in Cell.board.values():
cell.calc_next_state()
for cell in Cell.board.values():
cell.state = cell.next_state
def toggle():
for cell in Cell.board.values():
cell.check_click()
def init_board(rnd=False):
for i in range(cols):
for j in range(rows):
Cell(i, j, rnd)

Wyświetl plik

@ -26,6 +26,12 @@ Here are listed some of the tools I have been using:
---
![sketch_2021_07_17ahex_cells](2021/sketch_2021_07_17ahex_cells/sketch_2021_07_17ahex_cells.gif)
[sketch_2021_07_17ahex_cells](https://github.com/villares/sketch-a-day/tree/master/2021/sketch_2021_07_17ahex_cells) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
---
![sketch_2021_07_16ahex_cells](2021/sketch_2021_07_16ahex_cells/sketch_2021_07_16ahex_cells.gif)
[sketch_2021_07_16ahex_cells](https://github.com/villares/sketch-a-day/tree/master/2021/sketch_2021_07_16ahex_cells) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]