Alexandre B A Villares 2018-12-03 09:33:50 -02:00
rodzic a4ce0a8622
commit 9a13a6eec3
13 zmienionych plików z 131 dodań i 0 usunięć

77
s339/cell.py 100644
Wyświetl plik

@ -0,0 +1,77 @@
class Cell():
# neighbours list
NL = ((-1, -1), (+0, -1), (+1, -1),
(-1, +0), (+0, +0), (+1, +0),
(-1, +1), (+0, +1), (+1, +1))
ONL = ( (+0, -1),
(-1, +0), (+0, +0), (+1, +0),
(+0, +1))
DNL = ((-1, -1) , (+1, -1),
(+0, +0),
(-1, +1), (+1, +1))
def __init__(self, index, cell_size, state=False):
self.index = index
self.state = state
self.size_ = cell_size
self.mouse_down = False
i, j = index[0], index[1]
self.pos = PVector(self.size_ / 2 + i * self.size_,
self.size_ / 2 + j * self.size_)
def update(self):
# mouse over & selection treatment
hs = self.size_ / 2
px, py = self.pos.x, self.pos.y
self.mouse_on = (px - hs < mouseX < px + hs and
py - hs < mouseY < py + hs)
if self.mouse_on and mousePressed:
self.mouse_down = True
if self.mouse_down and not mousePressed:
self.state = not self.state
self.mouse_down = False
if self.mouse_on:
fill(128, 128)
else:
noFill()
strokeWeight(1)
stroke(200)
rect(self.pos.x, self.pos.y, self.size_, self.size_)
def plot(self, mode):
if self.state:
strokeWeight(self.size_ / 5.)
if mode == -1:
fill(0)
noStroke()
rect(self.pos.x, self.pos.y, self.size_, self.size_)
if mode == 0:
stroke(0)
self.draw_lines(Cell.NL)
elif mode == 1:
stroke(0, 150, 0)
self.draw_lines(Cell.ONL)
elif mode == 2:
stroke(0, 0, 150)
self.draw_lines(Cell.DNL)
elif mode == 3:
stroke(0, 150, 0)
self.draw_lines(Cell.ONL)
stroke(0, 0, 150)
self.draw_lines(Cell.DNL)
elif mode == 4:
stroke(0, 150, 0)
self.draw_lines(Cell.DNL)
stroke(0, 0, 150)
self.draw_lines(Cell.ONL)
def draw_lines(self, nbs):
third = self.size_ / 3
i, j = self.index[0], self.index[1]
for (ni, nj) in nbs:
nb = Cell.grid.get((i + ni, j + nj), None)
if nb and nb.state:
line(self.pos.x + ni * third * 1.5,
self.pos.y + nj * third * 1.5,
self.pos.x, self.pos.y)

BIN
s339/s339.gif 100644

Plik binarny nie jest wyświetlany.

Po

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

54
s339/s339.pyde 100644
Wyświetl plik

@ -0,0 +1,54 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s339" # 20181203
OUTPUT = ".png"
mode = 0
from cell import Cell
from random import choice
CELL_SIZE = 20
Cell.grid = dict()
def setup():
size(500, 500)
rectMode(CENTER)
init_grid()
def init_grid(func=lambda i,j: choice((True, False))):
w, h = width//CELL_SIZE, height//CELL_SIZE
for i in range(w):
for j in range(h):
Cell.grid[(i, j)] = Cell((i,j), CELL_SIZE, func(i, j))
def draw():
background(220)
for c in Cell.grid.values():
c.update()
for c in Cell.grid.values():
c.plot(mode)
def keyPressed():
global mode
if key == "s":
saveFrame(SKETCH_NAME + "_###.png")
if key in "01234567789":
mode = int(key)
if key == "-":
mode = -1
if key == " ":
init_grid(lambda i, j: False)
if key == "x":
init_grid(lambda i, j: (i + j) % 2 == 0)
if key == "r":
init_grid()
# print text to add to the project's README.md
def settings():
println(
"""
![{0}]({0}/{0}{2})
{1}: [code](https://github.com/villares/sketch-a-day/tree/master/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
""".format(SKETCH_NAME, SKETCH_NAME[1:], OUTPUT)
)

BIN
s339/s339_20702.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_20765.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_20815.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_20848.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_20987.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_21004.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_21019.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_21045.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_21068.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s339/s339_21196.png 100644

Plik binarny nie jest wyświetlany.

Po

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