kopia lustrzana https://github.com/villares/sketch-a-day
main
rodzic
ac8651fb3e
commit
3e5d6de8ec
|
@ -0,0 +1,101 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from random import choice
|
||||||
|
|
||||||
|
class Cell():
|
||||||
|
border = 0
|
||||||
|
RES = 4.
|
||||||
|
grid = dict()
|
||||||
|
debug_mode = False
|
||||||
|
# constants
|
||||||
|
|
||||||
|
# ortho neighbours
|
||||||
|
ONL = ((+0, -1, +0),
|
||||||
|
(-1, +0, +0), # (+0, +0, +0),
|
||||||
|
(+1, +0, +0),
|
||||||
|
(+0, +1, +0),
|
||||||
|
(+0, +0, -1), (+0, +0, +1),
|
||||||
|
)
|
||||||
|
# diagonal neighbours
|
||||||
|
DNL = ((+1, +1, +0),
|
||||||
|
(-1, -1, +0), # (+0, +0, +0),
|
||||||
|
(+1, -1, +0),
|
||||||
|
(-1, +1, +0),
|
||||||
|
(+0, -1, +1),
|
||||||
|
(-1, +0, +1),
|
||||||
|
(+1, +0, +1),
|
||||||
|
(+0, +1, +1),
|
||||||
|
(+0, -1, -1),
|
||||||
|
(-1, +0, -1),
|
||||||
|
(+1, +0, -1),
|
||||||
|
(+0, +1, -1),
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, index, cell_size, state=False):
|
||||||
|
self.index = index
|
||||||
|
self.state = state
|
||||||
|
self.size_ = cell_size
|
||||||
|
self.mouse_down = False
|
||||||
|
self.variation = "a"
|
||||||
|
self.ang = choice((0, 1, 2, 3))
|
||||||
|
self.calculate_pos()
|
||||||
|
|
||||||
|
def calculate_pos(self):
|
||||||
|
i, j, k = self.index
|
||||||
|
self.pos = PVector(Cell.border + self.size_ / 2 + i * self.size_ - width / 2,
|
||||||
|
Cell.border + self.size_ / 2 +
|
||||||
|
j * self.size_ - height / 2,
|
||||||
|
k * self.size_)
|
||||||
|
|
||||||
|
def update(self, mx, my):
|
||||||
|
# mouse over & selection treatment
|
||||||
|
hs = self.size_ / 2
|
||||||
|
px, py = self.pos.x + width / 2, self.pos.y + height / 2
|
||||||
|
self.mouse_on = (px - hs < mx < px + hs and
|
||||||
|
py - hs < my < py + hs)
|
||||||
|
if self.mouse_on and mousePressed:
|
||||||
|
self.mouse_down = True
|
||||||
|
|
||||||
|
elif self.mouse_down:
|
||||||
|
self.state = not self.state
|
||||||
|
self.mouse_down = False
|
||||||
|
|
||||||
|
|
||||||
|
def plot(self, mode):
|
||||||
|
|
||||||
|
if self.state:
|
||||||
|
strokeWeight(1)
|
||||||
|
self.draw_mode()
|
||||||
|
if mode == -1:
|
||||||
|
fill(100, 100, 100, 10)
|
||||||
|
noStroke()
|
||||||
|
rect(self.pos.x, self.pos.y, self.size_, self.size_)
|
||||||
|
noFill()
|
||||||
|
|
||||||
|
def draw_mode(self):
|
||||||
|
""" draws node """
|
||||||
|
siz = self.size_
|
||||||
|
with pushMatrix():
|
||||||
|
translate(self.pos.x, self.pos.y, self.pos.z)
|
||||||
|
if Cell.debug_mode:
|
||||||
|
fill(255, 0, 100)
|
||||||
|
text(self.module, 0, 0)
|
||||||
|
noFill() # stroke(0)
|
||||||
|
strokeWeight(2)
|
||||||
|
stroke(255)
|
||||||
|
box(siz / 3)
|
||||||
|
strokeWeight(2)
|
||||||
|
i, j, k = self.index
|
||||||
|
for (ni, nj, nk) in Cell.ONL:
|
||||||
|
nb = Cell.grid.get((i + ni, j + nj, k + nk), None)
|
||||||
|
if nb and nb.state:
|
||||||
|
stroke(200)
|
||||||
|
with pushMatrix():
|
||||||
|
translate(ni * siz / 3, nj * siz / 3, nk * siz / 3)
|
||||||
|
box(siz / 3)
|
||||||
|
for (ni, nj, nk) in Cell.DNL:
|
||||||
|
nb = Cell.grid.get((i + ni, j + nj, k + nk), None)
|
||||||
|
if nb and nb.state:
|
||||||
|
stroke(64 + (ni * 2 + nj * 3 + nk * 5) * 16, 255, 200)
|
||||||
|
with pushMatrix():
|
||||||
|
translate(ni * siz / 3, nj * siz / 3, nk * siz / 3)
|
||||||
|
box(siz / 3)
|
|
@ -0,0 +1,41 @@
|
||||||
|
"""
|
||||||
|
Alexandre B A Villares http://abav.lugaralgum.com - GPL v3
|
||||||
|
|
||||||
|
A helper for the Processing gifAnimation library https://github.com/extrapixel/gif-animation/tree/3.0
|
||||||
|
Download from https://github.com/villares/processing-play/blob/master/export_GIF/unzip_and_move_to_libraries_GifAnimation.zip
|
||||||
|
This helper was inspired by an example by Art Simon https://github.com/APCSPrinciples/AnimatedGIF/
|
||||||
|
|
||||||
|
# add at the start of your sketch:
|
||||||
|
add_library('gifAnimation')
|
||||||
|
from gif_exporter import gif_export
|
||||||
|
# add at the end of draw():
|
||||||
|
gif_export(GifMaker)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def gif_export(GifMaker, # gets a reference to the library
|
||||||
|
filename="exported", # .gif will be added
|
||||||
|
repeat=0, # 0 makes it an "endless" animation
|
||||||
|
quality=255, # quality range 0 - 255
|
||||||
|
delay=900, # this is quick
|
||||||
|
# 0 will stop on keyPressed or frameCount >= 100000
|
||||||
|
frames=0,
|
||||||
|
finish=False): # force stop
|
||||||
|
global gifExporter
|
||||||
|
try:
|
||||||
|
gifExporter
|
||||||
|
except NameError:
|
||||||
|
gifExporter = GifMaker(this, filename + ".gif")
|
||||||
|
gifExporter.setRepeat(repeat)
|
||||||
|
gifExporter.setQuality(quality)
|
||||||
|
gifExporter.setDelay(delay)
|
||||||
|
|
||||||
|
gifExporter.addFrame()
|
||||||
|
|
||||||
|
if frames == 0:
|
||||||
|
if keyPressed and key == "e":
|
||||||
|
finish = True
|
||||||
|
|
||||||
|
if finish:
|
||||||
|
gifExporter.finish()
|
||||||
|
print("gif saved")
|
||||||
|
exit()
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 1.7 MiB |
|
@ -0,0 +1,110 @@
|
||||||
|
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
|
||||||
|
SKETCH_NAME, OUTPUT = "sketch_190127a", ".gif" # find sketch name yourself!
|
||||||
|
|
||||||
|
from cell import Cell
|
||||||
|
from random import choice
|
||||||
|
add_library('GifAnimation')
|
||||||
|
add_library('peasycam')
|
||||||
|
from gif_exporter import gif_export
|
||||||
|
|
||||||
|
CELL_SIZE = 100
|
||||||
|
modulus = 3
|
||||||
|
mode = 0
|
||||||
|
save_frame = False
|
||||||
|
frame_saved = 0
|
||||||
|
|
||||||
|
def setup():
|
||||||
|
#hint(ENABLE_DEPTH_SORT)
|
||||||
|
size(600, 600, P3D)
|
||||||
|
colorMode(HSB)
|
||||||
|
global grid_size
|
||||||
|
grid_size = width / CELL_SIZE
|
||||||
|
rectMode(CENTER)
|
||||||
|
strokeCap(SQUARE)
|
||||||
|
cam = PeasyCam(this, 700)
|
||||||
|
|
||||||
|
|
||||||
|
def init_grid(f=None):
|
||||||
|
# default grid is with random state for cells
|
||||||
|
if f == None:
|
||||||
|
f = lambda i, j: choice((True, False))
|
||||||
|
# number of collums and rows -2 for default cell sized border
|
||||||
|
w = int(width // CELL_SIZE) # - 2
|
||||||
|
h = int(height // CELL_SIZE) # - 2
|
||||||
|
z = 3
|
||||||
|
# print(w, h)
|
||||||
|
for i in range(w):
|
||||||
|
for j in range(h):
|
||||||
|
for k in range(z):
|
||||||
|
Cell.grid[(i, j, k)] = Cell((i, j, k), CELL_SIZE, f(i, j))
|
||||||
|
|
||||||
|
def draw():
|
||||||
|
global save_frame, frame_saved
|
||||||
|
background(0)
|
||||||
|
for c in Cell.grid.values():
|
||||||
|
c.update(mouseX, mouseY)
|
||||||
|
for c in Cell.grid.values():
|
||||||
|
c.plot(mode)
|
||||||
|
|
||||||
|
if save_frame:
|
||||||
|
save_frame = False
|
||||||
|
frame_saved += 1
|
||||||
|
gif_export(GifMaker, SKETCH_NAME)
|
||||||
|
println(frame_saved)
|
||||||
|
|
||||||
|
def keyPressed():
|
||||||
|
global mode, modulus, save_frame
|
||||||
|
if key == "g" or key == "G":
|
||||||
|
save_frame = True
|
||||||
|
if key == "s" or key == "S":
|
||||||
|
saveFrame(SKETCH_NAME + "_#######.png")
|
||||||
|
if key != CODED and key in "01234567789":
|
||||||
|
mode = int(key)
|
||||||
|
if key == "-":
|
||||||
|
mode = -1
|
||||||
|
if key == " ":
|
||||||
|
t = lambda i, j: True
|
||||||
|
f = lambda i, j: False
|
||||||
|
init_grid(choice((t, f)))
|
||||||
|
if key == "r":
|
||||||
|
init_grid()
|
||||||
|
if key == "x":
|
||||||
|
init_grid(lambda i, j: (i + j) % modulus)
|
||||||
|
if key == "<" and modulus > 2:
|
||||||
|
modulus -= 1
|
||||||
|
if key == ">":
|
||||||
|
modulus += 1
|
||||||
|
if key == "z":
|
||||||
|
move_grid()
|
||||||
|
if keyCode == RIGHT:
|
||||||
|
move_grid(x=1, y=0)
|
||||||
|
if keyCode == LEFT:
|
||||||
|
move_grid(x=-1, y=0)
|
||||||
|
if keyCode == UP:
|
||||||
|
move_grid(x=0, y=-1)
|
||||||
|
if keyCode == DOWN:
|
||||||
|
move_grid(x=0, y=1)
|
||||||
|
|
||||||
|
def move_grid(x=1, y=1):
|
||||||
|
w, h = width // CELL_SIZE, height // CELL_SIZE
|
||||||
|
new_grid = dict()
|
||||||
|
for i in range(w):
|
||||||
|
for j in range(h):
|
||||||
|
for k in range(h):
|
||||||
|
c = Cell.grid.get((i, j, k), None)
|
||||||
|
if c:
|
||||||
|
c.index = ((i + x) % w, (j + y) % h, k)
|
||||||
|
c.calculate_pos()
|
||||||
|
new_grid[c.index] = c
|
||||||
|
Cell.grid = new_grid
|
||||||
|
|
||||||
|
|
||||||
|
# print text to add to the project's README.md
|
||||||
|
def settings():
|
||||||
|
println(
|
||||||
|
"""
|
||||||
|

|
||||||
|
|
||||||
|
[{0}](https://github.com/villares/sketch-a-day/tree/master/2019/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||||
|
""".format(SKETCH_NAME, OUTPUT)
|
||||||
|
)
|
Ładowanie…
Reference in New Issue