kopia lustrzana https://github.com/villares/sketch-a-day
day 112
rodzic
cebe3f5e9c
commit
a05d98b404
12
README.md
12
README.md
|
@ -8,6 +8,18 @@ If you enjoy this, make a small donation [here](https://www.paypal.com/cgi-bin/w
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|

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

|

|
||||||
|
|
||||||
111: [code](https://github.com/villares/sketch-a-day/tree/master/s111) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
111: [code](https://github.com/villares/sketch-a-day/tree/master/s111) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||||
|
|
|
@ -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=182, # 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
|
|
@ -0,0 +1,193 @@
|
||||||
|
# -*- 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, 128)
|
||||||
|
B = Slider(0, 1023, 128)
|
||||||
|
C = Slider(0, 1023, 128)
|
||||||
|
D = Slider(0, 1023, 128)
|
||||||
|
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}
|
||||||
|
|
||||||
|
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()
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 5.4 MiB |
|
@ -0,0 +1,121 @@
|
||||||
|
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
|
||||||
|
SKETCH_NAME = "s112" # 180422
|
||||||
|
|
||||||
|
add_library('serial') # import processing.serial.*;
|
||||||
|
add_library('arduino') # import cc.arduino.*;
|
||||||
|
add_library('gifAnimation')
|
||||||
|
|
||||||
|
from gif_exporter import *
|
||||||
|
from inputs import *
|
||||||
|
|
||||||
|
ELEMENTS = []
|
||||||
|
GIF_EXPORT = False
|
||||||
|
|
||||||
|
def setup():
|
||||||
|
global input
|
||||||
|
size(600, 600)
|
||||||
|
noFill() # sem preenchimento
|
||||||
|
frameRate(30)
|
||||||
|
strokeWeight(3)
|
||||||
|
colorMode(HSB)
|
||||||
|
# Ask user for Arduino port, uses slider if none is selected`
|
||||||
|
input = Input(Arduino, slider_pins=[1, 2, 3, 4])
|
||||||
|
|
||||||
|
def draw():
|
||||||
|
background(0) # fundo preto
|
||||||
|
|
||||||
|
grid_elem = int(input.analog(1) / 16) # 0 a 63 linhas e colunas na grade
|
||||||
|
elem_size = int(input.analog(2) / 16) # 0 a 63 tamanho base dos quadrados
|
||||||
|
rand_size = int(input.analog(3) / 16) # escala a randomização do tamanho
|
||||||
|
rand_posi = int(input.analog(4) / 16) # escala a randomização da posição
|
||||||
|
# trava a random entre os ciclos de draw
|
||||||
|
# mas varia com o número de colunas na grade
|
||||||
|
randomSeed(int(input.analog(1)) / 4)
|
||||||
|
# espaçamento entre os elementos
|
||||||
|
spac_size = width / (grid_elem + 1)
|
||||||
|
v = spac_size * 1.5
|
||||||
|
h = spac_size * sqrt(3)
|
||||||
|
for ix in range(-1, grid_elem + 1):
|
||||||
|
for iy in range(-1, grid_elem + 1):
|
||||||
|
if iy % 2:
|
||||||
|
x = ix * h + h / 4
|
||||||
|
else:
|
||||||
|
x = ix * h - h / 4
|
||||||
|
if ix % 2:
|
||||||
|
es = elem_size
|
||||||
|
else:
|
||||||
|
es = -elem_size
|
||||||
|
y = iy * v
|
||||||
|
for i in range(3):
|
||||||
|
final_size = es * (i + 0.5)
|
||||||
|
C = color(map(final_size, -63, 63, 0, 255),
|
||||||
|
255, 255)
|
||||||
|
oX = rand_posi * random(-1, 1)
|
||||||
|
oY = rand_posi * random(-1, 1)
|
||||||
|
ELEMENTS.append((C, x + oX, y + oY, final_size))
|
||||||
|
# three layers of elements
|
||||||
|
for e0, e1, e2 in zip(ELEMENTS[0::3], ELEMENTS[1::3], ELEMENTS[2::3]):
|
||||||
|
st0, x0, y0, es0 = e0
|
||||||
|
st1, x1, y1, es1 = e1
|
||||||
|
st2, x2, y2, es2 = e2
|
||||||
|
fs0 = es0 + rand_size * random(-1, 1)
|
||||||
|
fs1 = es1 + rand_size * random(-1, 1)
|
||||||
|
fs2 = es2 + rand_size * random(-1, 1)
|
||||||
|
with pushMatrix():
|
||||||
|
translate(x0,y0)
|
||||||
|
rotate(fs0)
|
||||||
|
stroke(st0)
|
||||||
|
hexagon(0, 0, fs0)
|
||||||
|
with pushMatrix():
|
||||||
|
translate(x1,y1)
|
||||||
|
rotate(fs1)
|
||||||
|
stroke(st1)
|
||||||
|
hexagon(0, 0, fs1)
|
||||||
|
with pushMatrix():
|
||||||
|
translate(x2,y2)
|
||||||
|
rotate(fs2)
|
||||||
|
stroke(st2)
|
||||||
|
hexagon(0, 0, fs2)
|
||||||
|
|
||||||
|
# empty list
|
||||||
|
ELEMENTS[:] = []
|
||||||
|
|
||||||
|
# uncomment next lines to export GIF
|
||||||
|
global GIF_EXPORT
|
||||||
|
if not frameCount % 20 and GIF_EXPORT:
|
||||||
|
GIF_EXPORT = gif_export(GifMaker,
|
||||||
|
frames=1000,
|
||||||
|
delay=300,
|
||||||
|
filename=SKETCH_NAME)
|
||||||
|
|
||||||
|
# Updates reading or draws sliders and checks mouse dragging / keystrokes
|
||||||
|
input.update()
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
input.keyPressed()
|
||||||
|
|
||||||
|
def keyReleased():
|
||||||
|
input.keyReleased()
|
||||||
|
|
||||||
|
def rnd_choice(collection):
|
||||||
|
i = int(random(len(collection)))
|
||||||
|
return collection[i]
|
||||||
|
|
||||||
|
def hexagon(x, y, r):
|
||||||
|
with pushMatrix():
|
||||||
|
translate(x, y)
|
||||||
|
beginShape()
|
||||||
|
for i in range(6):
|
||||||
|
sx = cos(i * TWO_PI /6) * r
|
||||||
|
sy = sin(i * TWO_PI /6) * r
|
||||||
|
vertex(sx, sy)
|
||||||
|
endShape(CLOSE)
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 2.9 MiB |
Ładowanie…
Reference in New Issue