kopia lustrzana https://github.com/villares/sketch-a-day
main
rodzic
e0def3036b
commit
432b110f5d
12
README.md
12
README.md
|
@ -10,6 +10,18 @@ If you enjoy this, be a [patreon](https://patreon.com/arteprog) or make a donati
|
||||||
Feel free to [contact me](http://contato.lugaralgum.com) regarding licenses to use my work, teaching opportunities, consulting or other projects.
|
Feel free to [contact me](http://contato.lugaralgum.com) regarding licenses to use my work, teaching opportunities, consulting or other projects.
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
346: [code](https://github.com/villares/sketch-a-day/tree/master/s344) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
345: [code](https://github.com/villares/sketch-a-day/tree/master/s345) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||

|

|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
|
||||||
|
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)
|
||||||
|
with pushMatrix():
|
||||||
|
translate(self.pos.x, self.pos.y, -16)
|
||||||
|
rect(0, 0, self.size_, self.size_)
|
||||||
|
|
||||||
|
def plot(self, mode):
|
||||||
|
if self.state:
|
||||||
|
strokeWeight(self.size_ / 3.)
|
||||||
|
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)
|
||||||
|
self.draw_lines2(Cell.NL)
|
||||||
|
elif mode == 1:
|
||||||
|
stroke(0, 150, 0)
|
||||||
|
self.draw_lines2(Cell.ONL)
|
||||||
|
elif mode == 2:
|
||||||
|
stroke(0, 0, 150)
|
||||||
|
self.draw_lines(Cell.DNL)
|
||||||
|
elif mode == 3:
|
||||||
|
stroke(0, 150, 0)
|
||||||
|
self.draw_lines2(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_lines2(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)
|
||||||
|
ellipse(self.pos.x, self.pos.y, third, third)
|
||||||
|
|
||||||
|
def draw_lines2(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:
|
||||||
|
with pushMatrix():
|
||||||
|
translate(self.pos.x, self.pos.y, -15)
|
||||||
|
if ni == 0 and nj != 0:
|
||||||
|
line(-third, nj * third * 1.5, -third, 0)
|
||||||
|
line(+third, nj * third * 1.5, +third, 0)
|
||||||
|
elif nj == 0 and ni != 0:
|
||||||
|
line(ni * third * 1.5, -third, 0, -third)
|
||||||
|
line(ni * third * 1.5, +third, 0, +third)
|
||||||
|
else:
|
||||||
|
ellipse(0, 0, third * 2, third * 2)
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 838 KiB |
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 31 KiB |
|
@ -0,0 +1,99 @@
|
||||||
|
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
|
||||||
|
SKETCH_NAME = "s346" # 20181210
|
||||||
|
OUTPUT = ".png"
|
||||||
|
mode = 0
|
||||||
|
|
||||||
|
from cell import Cell
|
||||||
|
from random import choice
|
||||||
|
from java.awt import Toolkit
|
||||||
|
|
||||||
|
CELL_SIZE = 24
|
||||||
|
Cell.grid = dict()
|
||||||
|
|
||||||
|
xo, yo = 100, 100
|
||||||
|
xio, yio = 0, 0
|
||||||
|
s = 10
|
||||||
|
|
||||||
|
def setup():
|
||||||
|
size(600, 600, P3D)
|
||||||
|
smooth(2)
|
||||||
|
global img, grid_size
|
||||||
|
img = loadImage("unifont-11.0.02.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():
|
||||||
|
background(220)
|
||||||
|
strokeWeight(1)
|
||||||
|
# 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 key == "t":
|
||||||
|
init_grid(lambda i, j: choice((True, False))
|
||||||
|
if j < grid_size / 3 or j > grid_size * 0.66 else False )
|
||||||
|
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(
|
||||||
|
"""
|
||||||
|

|
||||||
|
|
||||||
|
{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)
|
||||||
|
)
|
Ładowanie…
Reference in New Issue