main
Alexandre B A Villares 2022-05-09 10:25:39 -03:00
rodzic a582b585b3
commit 26e597b9c1
4 zmienionych plików z 195 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,119 @@
from math import sqrt
from py5 import *
from functools import cache
SIN_60 = sqrt(3) * 0.5 # sin(radians(60))
class mock_cell:
state = 1
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 = 6
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(5000 / (1 + j)) < 1 else 0
else:
self.state = 0
self.next_state = self.state
self.gen = 0
self.nbs = 0
def display(self):
if self.state:
with push_matrix():
translate(self.x, self.y)
stroke_weight(self.W - (abs(self.gen / 5.0) % 4))
d = (4 + 4 * sin(self.gen / 5.0))
stroke(d * 16, 200, 200) #42 * self.nbs)
#no_stroke()
point(0, 0)
#self.hexagon(Cell.W)
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),
mock_cell())
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 or 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
# muito bom
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
# 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, mx, my):
if (self.mouse_over(mx, my) and
self.__class__.last_clicked != self):
self.state ^= 1
self.__class__.last_clicked = self
def mouse_over(self, mx, my):
return dist(self.x, self.y, mx, my) < self.H
@staticmethod
def hexagon(w):
h = SIN_60 * w
with begin_shape():
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: 48 KiB

Wyświetl plik

@ -0,0 +1,68 @@
# based on sketch_2021_07_14ahex_cells
from cell import Cell
play = former_play_state = False
sample_rate = 1
def setup():
global cols, rows, board
size(600, 600)
color_mode(HSB)
cols = int(width / Cell.W * 1.5)
rows = int(height / Cell.H * 2) - 1
init_board()
stroke(0)
def draw():
background(0)
for cell in Cell.board.values():
cell.display()
if play and frame_count % sample_rate == 0:
next_board()
def key_pressed():
global play
if key == 'e':
init_board()
elif key == 'r':
init_board(rnd=True)
elif key == ' ':
play = not play
def mouse_dragged():
toggle()
def mouse_pressed():
global play, former_play_state
former_play_state = play
play = False
toggle()
def mouse_released():
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(mouse_x, mouse_y)
def init_board(rnd=False):
for i in range(cols):
for j in range(rows):
Cell(i, j, rnd)

Wyświetl plik

@ -27,6 +27,14 @@ Here are listed some of the tools I have been using:
---
### sketch_2022_05_09ahex_cells
![sketch_2022_05_09ahex_cells](2022/sketch_2022_05_09ahex_cells/sketch_2022_05_09ahex_cells.png)
[sketch_2022_05_09ahex_cells](https://github.com/villares/sketch-a-day/tree/master/2022/sketch_2022_05_09ahex_cells) [[py5](https://py5.ixora.io/)]
---
### sketch_2022_05_08ahex_cells
![sketch_2022_05_08ahex_cells](2022/sketch_2022_05_08ahex_cells/sketch_2022_05_08ahex_cells.gif)