Alexandre B A Villares 2022-05-12 21:54:01 -03:00
rodzic f4b50dc613
commit 612efa8684
7 zmienionych plików z 382 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,104 @@
from math import sqrt
from py5 import *
from functools import cache
SIN_60 = sqrt(3) * 0.5 # sin(radians(60))
class mock_cell:
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(50000 / (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)
d = (4 + 4 * sin(self.gen / 5.0))
stroke(d * 16, 200, 200) #42 * self.nbs)
stroke_weight(2 + d * self.W / 6)
point(0, 0)
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()
#
# 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 == 2 and self.state == 0:
self.next_state = 1
lnbs = self.get_live_nbs()
if lnbs:
self.gen = lnbs[0].gen + 1
elif nbs == 0 or nbs == 4 or nbs == 6:
self.next_state = 0
else:
self.next_state = self.state
def check_click(self):
if self.__class__.last_clicked != self:
self.state ^= 1
self.__class__.last_clicked = self
@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: 209 KiB

Wyświetl plik

@ -0,0 +1,79 @@
import py5
from cell import Cell
play = former_play_state = False
sample_rate = 1
def setup():
global cols, rows, board
py5.size(800, 800)
py5.color_mode(py5.HSB)
cols = int(py5.width / Cell.W * 1.5)
rows = int(py5.height / Cell.H * 2) - 1
init_board()
def draw():
py5.background(0)
for cell in Cell.board.values():
cell.display()
if play and py5.frame_count % sample_rate == 0:
next_board()
def key_pressed():
global play
if py5.key == 'e':
init_board()
elif py5.key == 'r':
init_board(rnd=True)
elif py5.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():
mx, my = py5.mouse_x, py5.mouse_y
i = (mx - Cell.W) // (Cell.W * 1.5)
if i % 2 == 0:
j = (my - Cell.H) // (Cell.H * 2)
else:
j = (my - Cell.H * 2) // (Cell.H * 2)
try:
cell = Cell.board[(i, j)]
cell.check_click()
except KeyError:
pass
def init_board(rnd=False):
for i in range(cols):
for j in range(rows):
Cell(i, j, rnd)
py5.run_sketch()

Wyświetl plik

@ -0,0 +1,104 @@
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 = 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(50000 / (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)
d = (4 + 4 * sin(self.gen / 5.0))
stroke(d * 16, 200, 200) #42 * self.nbs)
stroke_weight(2 + d * self.W / 6)
point(0, 0)
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()
#
# 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 == 2 and self.state == 0:
# self.next_state = 1
# lnbs = self.get_live_nbs()
# if lnbs:
# self.gen = lnbs[0].gen + 1
# elif nbs not in (4, 3, 2):
# self.next_state = 0
# else:
# self.next_state = self.state
def check_click(self):
if self.__class__.last_clicked != self:
self.state ^= 1
self.__class__.last_clicked = self
@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: 183 KiB

Wyświetl plik

@ -0,0 +1,79 @@
import py5
from cell import Cell
play = former_play_state = False
sample_rate = 1
def setup():
global cols, rows, board
py5.size(800, 800)
py5.color_mode(py5.HSB)
cols = int(py5.width / Cell.W * 1.5)
rows = int(py5.height / Cell.H * 2) - 1
init_board()
def draw():
py5.background(0)
for cell in Cell.board.values():
cell.display()
if play and py5.frame_count % sample_rate == 0:
next_board()
def key_pressed():
global play
if py5.key == 'e':
init_board()
elif py5.key == 'r':
init_board(rnd=True)
elif py5.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():
mx, my = py5.mouse_x, py5.mouse_y
i = (mx - Cell.W) // (Cell.W * 1.5)
if i % 2 == 0:
j = (my - Cell.H) // (Cell.H * 2)
else:
j = (my - Cell.H * 2) // (Cell.H * 2)
try:
cell = Cell.board[(i, j)]
cell.check_click()
except KeyError:
pass
def init_board(rnd=False):
for i in range(cols):
for j in range(rows):
Cell(i, j, rnd)
py5.run_sketch()

Wyświetl plik

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