Alexandre B A Villares 2018-05-09 22:27:45 -03:00
rodzic 7faf101927
commit 378da663bc
5 zmienionych plików z 523 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,43 @@
"""
Alexandre B A Villares http://abav.lugaralgum.com - GPL v3
A helper for the Processing gifAnimation library (https://github.com/jordanorelli)
ported to Processing 3 by 01010101 (https://github.com/01010101)
Download the library from https://github.com/01010101/GifAnimation/archive/master.zip
This helper was inspired by an example by Art Simon https://github.com/APCSPrinciples/AnimatedGIF/
Put at the start of your sketch:
add_library('gifAnimation')
from gif_exporter import gif_export
and 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=100, # quality range 0 - 255
delay=170, # this is quick
frames=0): # 0 will stop on keyPressed or frameCount >= 100000
global gifExporter
try:
gifExporter
except NameError:
gifExporter = GifMaker(this, filename + ".gif")
gifExporter.setRepeat(repeat)
gifExporter.setQuality(quality)
gifExporter.setDelay(delay)
gif_export._frame = frameCount
print("gif start")
gifExporter.addFrame()
if (frames == 0 and keyPressed or frameCount - gif_export._frame >= 100000) \
or (frames != 0 and frameCount - gif_export._frame >= frames):
gifExporter.finish()
background(255)
print("gif saved")
del(gifExporter)
return False
else:
return True

196
s129/inputs.py 100644
Wyświetl plik

@ -0,0 +1,196 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from javax.swing import JOptionPane
"""
This will hpefully switch between Arduino (Firmata) variable input and
nice sliders based on Peter Farell's Sliders htts://twitter.com/hackingmath
https://github.com/hackingmath/python-sliders http://farrellpolymath.com/
"""
class Input:
def __init__(self, Arduino, slider_pins):
self.select_source(Arduino)
if self.source > 0:
self.arduino = Arduino(this, Arduino.list()[self.source], 57600)
else:
# start, end, default
A = Slider(0, 1023, 512)
B = Slider(0, 1023, 512)
C = Slider(0, 1023, 512)
D = Slider(0, 1023, 512)
A.position(40, height - 70)
B.position(40, height - 30)
C.position(width - 140, height - 70)
D.position(width - 140, height - 30)
# a, b, c, d = slider_pins
# self.sliders = {a: A, b: B, c: C, d: D}
a, b = slider_pins
self.sliders = {a: A, b: B}
def analog(self, pin):
if self.source:
return self.arduino.analogRead(pin)
else:
return self.sliders[pin].val
def update(self):
if not self.source:
for pin, slider in self.sliders.iteritems():
slider.update()
def keyPressed(self):
if key == 'a':
self.sliders[1].down = True
if key == 'd':
self.sliders[1].up = True
if key == 's':
self.sliders[2].down = True
if key == 'w':
self.sliders[2].up = True
# if keyCode == LEFT:
# self.sliders[3].down = True
# if keyCode == RIGHT:
# self.sliders[3].up = True
# if keyCode == DOWN:
# self.sliders[4].down = True
# if keyCode == UP:
# self.sliders[4].up = True
def keyReleased(self):
if key == 'a':
self.sliders[1].down = False
if key == 'd':
self.sliders[1].up = False
if key == 's':
self.sliders[2].down = False
if key == 'w':
self.sliders[2].up = False
# if keyCode == LEFT:
# self.sliders[3].down = False
# if keyCode == RIGHT:
# self.sliders[3].up = False
# if keyCode == DOWN:
# self.sliders[4].down = False
# if keyCode == UP:
# self.sliders[4].up = False
def digital(self, pin):
space_pressed = keyPressed and key == ' '
if self.source:
if pin == 13:
return self.arduino.digitalRead(13) or space_pressed
else:
return arduino.digitalRead(pin)
else:
return space_pressed
def select_source(self, Arduino):
# Input.Arduino = Arduino # to make available on this module
port_list = [str(num) + ": " + port for num, port
in enumerate(Arduino.list())]
if not port_list:
port_list.append(None)
self.source = option_pane("O seu Arduino está conectado?",
"Escolha a porta ou pressione Cancel\npara usar 'sliders':",
port_list,
-1) # index for default option
self.help()
def help(self):
if self.source:
message = """ Teclas:
'h' para esta ajuda
'p' para salvar uma imagem
'g' para salvar um GIF
Tombe a lousa para lousa para limpar o desenho!"""
else:
message = """ Teclas:
'h' para esta ajuda
'p' para salvar uma imagem
'g' para salvar um GIF
'a' (-) ou 'd' (+) para o slider 1
's' (-) ou 'w' (+) para o slider 2
(-) ou (+) para o slider 3
(-) ou (+) para o slider 4
[barra de espaço] para limpar o desenho"""
ok = JOptionPane.showMessageDialog(None, message)
def option_pane(title, message, options, default=None, index_only=True):
if default == None:
default = options[0]
elif index_only:
default = options[default]
selection = JOptionPane.showInputDialog(
frame,
message,
title,
JOptionPane.INFORMATION_MESSAGE,
None, # for Java null
options,
default) # must be in options, otherwise 1st is shown
if selection:
if index_only:
return options.index(selection)
else:
return selection
class Slider:
SLIDERS = []
def __init__(self, low, high, default):
'''slider has range from low to high
and is set to default'''
self.low = low
self.high = high
self.val = default
self.clicked = False
self.up, self.down = False, False
Slider.SLIDERS.append(self)
def position(self, x, y):
'''slider's position on screen'''
self.x = x
self.y = y
# the position of the rect you slide:
self.rectx = self.x + map(self.val, self.low, self.high, 0, 120)
self.recty = self.y - 10
def update(self):
'''updates the slider'''
pushStyle()
rectMode(CENTER)
# black translucid rect behind slider
fill(0, 100)
noStroke()
rect(self.x + 60, self.y, 130, 20)
# gray line behind slider
strokeWeight(4)
stroke(200)
line(self.x, self.y, self.x + 120, self.y)
# press mouse to move slider
if (self.x < mouseX < self.x + 120 and
self.y < mouseY < self.y + 20):
fill(250)
textSize(10)
text(str(int(self.val)), self.rectx, self.recty + 35)
if mousePressed:
self.rectx = mouseX
# key usage
if self.up:
self.rectx += 1
if self.down:
self.rectx -= 1
# constrain rectangle
self.rectx = constrain(self.rectx, self.x, self.x + 120)
# draw rectangle
strokeWeight(1)
fill(255)
rect(self.rectx, self.recty + 10, 10, 20)
self.val = map(self.rectx, self.x, self.x + 120, self.low, self.high)
popStyle()

BIN
s129/s129.gif 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 1.1 MiB

141
s129/s129.pyde 100644
Wyświetl plik

@ -0,0 +1,141 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s129" # 180509
GRID_SIDE = 40 # colunas na grade
add_library('gifAnimation')
from gif_exporter import *
def setup():
global input, GIF_EXPORT
size(600, 600, P2D)
frameRate(2)
# textAlign(CENTER, CENTER)
noFill()
Cell.CELLS = []
GIF_EXPORT = False
create_grid()
def draw():
# background(64, 127, 127, 128)
fill(64, 64, 127, 128)
rect(0, 0, width, height)
for cell in Cell.CELLS:
cell.draw_()
# uncomment next lines to export GIF
global GIF_EXPORT
if GIF_EXPORT:
GIF_EXPORT = gif_export(GifMaker,
frames=50,
delay=200,
filename=SKETCH_NAME)
Cell.update()
def create_grid():
global SPAC_SIZE
# espaçamento entre os elementos
SPAC_SIZE = int(width / GRID_SIDE)
# empty list
Cell.CELLS[:] = []
# append new cells to grid
for x in range(GRID_SIDE): # um x p/ cada coluna
for y in range(GRID_SIDE): # um y p/ cada linha
new_cell = Cell(x,y)
Cell.CELL_GRID[x][y] = new_cell
Cell.CELLS.append(new_cell)
for cell in Cell.CELLS:
cell.status = int(random(10)>7)
cell.update_nc()
def keyPressed():
global GIF_EXPORT
if key == 'p': # save PNG
saveFrame("####.png")
if key == 'g': # save GIF
GIF_EXPORT = True
if key == 'h':
input.help()
if key == 'r':
create_grid()
class Cell():
CELLS = []
CELL_GRID = [[None]*GRID_SIDE for _ in range(GRID_SIDE)]
def __init__(self, x, y):
self.x = x
self.y = y
self.status = 0
self.nc = 6
self.next = 0
self.color_ = color(255, 0, 0)
def update_nc(self):
'''
update neighbour count (self.nc)
AND apply the 'game' rule
'''
self.nc = self.count_neighbours()
# cells with 1, 5 or 6 neighbours die
if self.nc < 2:
self.next = 0
elif self.nc > 4:
self.next = 0
else:
self.next = 1
@classmethod
def update(cls):
for cell in cls.CELLS:
cell.status = cell.next
for cell in cls.CELLS:
cell.update_nc()
cell.color_ = map(cell.nc, 0, 6, 0, 255)
def draw_(self):
if self.status:
fill(self.color_)
noStroke()
s = SPAC_SIZE * 1.2
v = SPAC_SIZE * 1.5
h = SPAC_SIZE * sqrt(3)
if self.y % 2:
x = self.x * h + h / 4
else:
x = self.x * h - h / 4
y = self.y * v
ellipse(x, y, s, s)
# pointy_hexagon(x, y, s)
fill(255)
text(str((self.nc)), x, y)
if dist(mouseX, mouseY, x, y) < s:
for cell in self.neighbours():
if cell: print(cell.x, cell.y)
def count_neighbours(self):
count = 0
for cell in self.neighbours():
if cell and cell.status:
count += 1
return count
def neighbours(self):
neighbour_list = []
offset_list = [(-1, 0), (0, -1), (0, 1), (1, 0), (1, -1), (1, 1)]
for offset_x, offset_y in offset_list:
try:
neighbour = Cell.CELL_GRID[self.x + offset_x][self.y + offset_y]
neighbour_list.append(neighbour)
except IndexError:
pass
return neighbour_list
@staticmethod
def item_at_x_y(x, y, collection, w, h):
if 0 < x < w and 0 < y < h:
return collection[x + y * w]
return None

143
s129/s129fail1.py 100644
Wyświetl plik

@ -0,0 +1,143 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s129" # 180509
GRID_SIDE = 50 # colunas na grade
add_library('gifAnimation')
from gif_exporter import *
def setup():
global input, GIF_EXPORT
size(600, 600, P2D)
frameRate(3)
# textAlign(CENTER, CENTER)
noFill()
Cell.CELLS = []
GIF_EXPORT = False
create_grid()
def draw():
# background(64, 127, 127, 128)
fill(64, 64, 127, 128)
rect(0, 0, width, height)
for cell in Cell.CELLS:
cell.draw_()
# uncomment next lines to export GIF
global GIF_EXPORT
if GIF_EXPORT:
GIF_EXPORT = gif_export(GifMaker,
frames=50,
delay=200,
filename=SKETCH_NAME)
Cell.update()
def create_grid():
global SPAC_SIZE
# espaçamento entre os elementos
SPAC_SIZE = int(width / GRID_SIDE)
# empty list
Cell.CELLS[:] = []
# append new cells to grid
for x in range(GRID_SIDE): # um x p/ cada coluna
for y in range(GRID_SIDE): # um y p/ cada linha
new_cell = Cell(x, y)
if random(10) > 2:
new_cell.status = 1
Cell.CELLS.append(new_cell)
Cell.set_neighbours()
def keyPressed():
global GIF_EXPORT
if key == 'p': # save PNG
saveFrame("####.png")
if key == 'g': # save GIF
GIF_EXPORT = True
if key == 'h':
input.help()
if key == 'r':
create_grid()
class Cell():
CELLS = []
def __init__(self, x, y):
self.x = x
self.y = y
self.status = 0
self.next = 0
self.nc = 6
self.nl = []
self.color_ = color(255, 0, 0)
def update_nc(self):
'''
update neighbour count (self.nc)
AND apply the 'game' rule
'''
self.nc = self.count_neighbours()
# cells with 1, 4, 5 or 6 neighbours die
if self.nc < 2:
self.next = 0
elif self.nc > 5:
self.next = 0
else:
self.next = 1
@classmethod
def set_neighbours(cls):
for cell in cls.CELLS:
cell.nl = cell.neighbours()
@classmethod
def update(cls):
for cell in cls.CELLS:
cell.status = cell.next
for cell in cls.CELLS:
cell.update_nc()
cell.color_ = map(cell.nc, 0, 6, 0, 255)
def draw_(self):
if self.status:
fill(self.color_)
noStroke()
s = SPAC_SIZE * 1.2
v = SPAC_SIZE * 1.5
h = SPAC_SIZE * sqrt(3)
if self.y % 2:
x = self.x * h + h / 4
else:
x = self.x * h - h / 4
y = self.y * v
ellipse(x, y, s, s)
# pointy_hexagon(x, y, s)
if dist(mouseX, mouseY, x, y) < s:
fill(255)
text(str(self.nc), x, y)
println(self.nl)
def count_neighbours(self):
count = 0
for cell in self.nl:
if cell and cell.status:
count += 1
return count
def neighbours(self):
neighbour_list = []
offset_list = [(-1, 0), (0, -1), (0, 1), (1, 0), (1, -1), (1, 1)]
for offset_x, offset_y in offset_list:
neighbour = Cell.item_at_x_y(self.x + offset_x,
self.y + offset_y,
Cell.CELLS,
GRID_SIDE, GRID_SIDE)
neighbour_list.append(neighbour)
return neighbour_list
@staticmethod
def item_at_x_y(x, y, collection, w, h):
if 0 < x < w and 0 < y < h:
return collection[x + y * w]
return None