Add files via upload

main
Alexandre B A Villares 2018-12-18 23:12:28 -02:00 zatwierdzone przez GitHub
rodzic abed301b41
commit c52912f1a7
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
7 zmienionych plików z 207 dodań i 0 usunięć

BIN
s354/cell$py.class 100644

Plik binarny nie jest wyświetlany.

110
s354/cell.py 100644
Wyświetl plik

@ -0,0 +1,110 @@
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(1)
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.ONL)
self.draw_lines(Cell.DNL, -4)
# self.draw_lines(Cell.DNL)
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, -4)
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, -4)
def draw_lines(self, nbs, res=0):
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:
rect(self.pos.x + ni * third * 1.5,
self.pos.y + nj * third * 1.5,
third + res, third + res)
# point(self.pos.x, self.pos.y) #, third, third)
#rect(self.pos.x, self.pos.y, third, third)
arrow(self.pos.x, self.pos.y,
self.pos.x + ni * third * 1.5,
self.pos.y + nj * third * 1.5,
third / 2, third / 2,
tail_func=ellipse)
def arrow(x1, y1, x2, y2, shorter=0, head=None,
tail_func=None, tail_size=None):
"""
"""
L = dist(x1, y1, x2, y2)
if not head:
head = max(L / 20, 2)
if L > head:
with pushMatrix():
translate(x1, y1)
angle = atan2(x1 - x2, y2 - y1)
rotate(angle)
offset = shorter / 2
strokeCap(ROUND)
line(0, L - offset, -head / 3, L - offset - head)
line(0, L - offset, head / 3, L - offset - head)
strokeCap(SQUARE)
line(0, offset, 0, L - offset)
if tail_func:
if not tail_size:
tail_size = head
tail_func(0, 0, tail_size, tail_size)

BIN
s354/data/a.png 100644

Plik binarny nie jest wyświetlany.

Po

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

97
s354/s354.pyde 100644
Wyświetl plik

@ -0,0 +1,97 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s354" # 20181218
OUTPUT = ".png"
mode = 0
from cell import Cell
from random import choice
from java.awt import Toolkit
CELL_SIZE = 50
Cell.grid = dict()
xo, yo = 100, 100
xio, yio = 0, 0
s = 10
def setup():
size(600, 600)
global img, grid_size
img = loadImage("a.png")
grid_size = width / CELL_SIZE
def init_grid(f=None):
w, h = width // CELL_SIZE, height // CELL_SIZE
for i in range(w):
for j in range(h):
if f == None:
f = lambda i, j: choice((True, False))
Cell.grid[(i, j)] = Cell((i, j), CELL_SIZE, f(i, j) )
def p_ou_b(i, j):
c = img.get(xo + i, yo + j)
if c == color(0): return True
else: return False
def draw():
#scale(3)
background(220)
# KeyEvent.VK_CAPS_LOCK is 20
capsLocked = Toolkit.getDefaultToolkit().getLockingKeyState(20)
if capsLocked:
rectMode(CORNER)
image(img, xio, yio)
noFill()
rect(xio + xo, yio + yo, grid_size, grid_size)
init_grid(p_ou_b)
else:
rectMode(CENTER)
for c in Cell.grid.values():
c.update()
for c in Cell.grid.values():
c.plot(mode)
def keyPressed():
global mode
global xo, yo, xio, yio
if key == CODED:
if keyCode == RIGHT and xo < img.width - 11:
xo += 16
if keyCode == LEFT and xo > 10:
xo -= 16
if keyCode == DOWN and yo < img.height - 11:
yo += 16
if keyCode == UP and yo > 10:
yo -= 16
else:
if key == "s" or 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()
if xo > width - grid_size:
xio = width - grid_size - xo
if yo > height - grid_size:
yio = height - grid_size - yo
# 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
s354/s354_1171.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s354/s354_1192.png 100644

Plik binarny nie jest wyświetlany.

Po

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

BIN
s354/s354_1257.png 100644

Plik binarny nie jest wyświetlany.

Po

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