main
Alexandre B A Villares 2018-04-02 00:44:08 -03:00
rodzic 6d6e746f86
commit f090158fed
4 zmienionych plików z 115 dodań i 170 usunięć

92
s091/graphs.py 100644
Wyświetl plik

@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
TAM_PONTO = 50 # TAM_PONTO dos Pontos
class Ponto():
VEL_MAX = 5
SET = set()
" Pontos num grafo, VEL_MAX inicial sorteada, criam Arestas com outros Pontos "
def __init__(self, x, y, NUM_CONNECT, cor=color(0)):
VEL_MAX = Ponto.VEL_MAX
self.x = x
self.y = y
self.z = 0 # para compatibilidade com PVector...
self.vx = random(-VEL_MAX, VEL_MAX)
self.vy = random(-VEL_MAX, VEL_MAX)
self.sel = False # se está selecionado, começa sem seleção
self.cor = color(random(128, 255), # R
random(128, 255), # G
random(128, 255), # B
128) # Alpha ~50%
self.cria_arestas(NUM_CONNECT)
def desenha(self):
if self.sel:
stroke(0)
else:
noStroke()
fill(self.cor)
ellipse(self.x, self.y, TAM_PONTO, TAM_PONTO)
def move(self, VEL_MAX):
Ponto.VEL_MAX = VEL_MAX
self.x += self.vx
self.y += self.vy
if not (0 < self.x < width):
self.vx = -self.vx
if not (0 < self.y < height):
self.vy = -self.vy
self.vx = self.limitar(self.vx, VEL_MAX)
self.vy = self.limitar(self.vy, VEL_MAX)
def cria_arestas(self, NUM_CONNECT):
for _ in range(NUM_CONNECT):
lista_pontos = list(Ponto.SET)
if lista_pontos:
nova_aresta = Aresta(rnd_choice(lista_pontos), self)
Aresta.ARESTAS.append(nova_aresta)
def limitar(self, v, v_max):
if v > v_max:
return v_max
elif v < -v_max:
return -v_max
else:
return v
class Aresta():
""" Arestas contém só dois Pontos e podem ou não estar selecionadas """
ARESTAS = []
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
self.sel = False
def desenha(self):
if self.sel:
stroke(0)
else:
stroke(255)
line(self.p1.x, self.p1.y, self.p2.x, self.p2.y)
noStroke()
fill(255)
ellipse(self.p1.x, self.p1.y, TAM_PONTO / 6, TAM_PONTO / 6)
ellipse(self.p2.x, self.p2.y, TAM_PONTO / 6, TAM_PONTO / 6)
def puxa_empurra(self, TAM_BARRA):
d = dist(self.p1.x, self.p1.y, self.p2.x, self.p2.y)
delta = TAM_BARRA - d
dir = PVector.sub(self.p1, self.p2)
dir.mult(delta / 1000)
self.p1.vx = self.p1.vx + dir.x
self.p1.vy = self.p1.vy + dir.y
self.p2.vx = self.p2.vx - dir.x
self.p2.vy = self.p2.vy - dir.y
def rnd_choice(collection):
i = int(random(len(collection)))
return collection[i]

Wyświetl plik

@ -8,7 +8,6 @@ https://github.com/hackingmath/python-sliders http://farrellpolymath.com/
"""
class Inputs:
TILT = None
#update_inputs = None
@staticmethod
def select_source(Arduino):
@ -43,7 +42,6 @@ class Inputs:
@staticmethod
def update():
Slider.update_all()
Inputs.TILT = (keyPressed and key == ' ')
else:
arduino = Inputs.Arduino(this, Inputs.Arduino.list()[port], 57600)
@ -57,6 +55,7 @@ class Inputs:
def update():
Analog_input.update_all()
Inputs.TILT = arduino.digitalRead(13) == Inputs.Arduino.HIGH
Inputs.update_inputs = update
return A, B, C, D
@ -124,7 +123,7 @@ class Slider:
self.y < mouseY < self.y + 20):
fill(250)
textSize(10)
text(str(self.val), self.rectx, self.recty + 35)
text(str(int(self.val)), self.rectx, self.recty + 35)
if mousePressed:
self.rectx = mouseX
# key usage

Wyświetl plik

@ -6,27 +6,11 @@ add_library('arduino') # import cc.arduino.*;
add_library('gifAnimation')
from gif_exporter import gif_export
from shapes import *
from graphs import *
from parameters import *
SHAPES = [circle, # defined in shapes.py
square,
exes,
losang]
pontos = set() # conjunto de Pontos
arestas = [] # lista de Arestas
TAM_PONTO = 50 # TAM_PONTO dos Pontos \
TAM_BARRA = 100
VEL_MAX = 2 # velocidade máxima nas ortogonais vx e vy
NUM_PONTOS = 5
NUM_CONNECT = 1
def setup():
frameRate(30)
background(0)
global A, B, C, D
# Ask user for Arduino port, cancel will return `None`
port = Inputs.select_source(Arduino)
@ -34,12 +18,6 @@ def setup():
A, B, C, D = Inputs.setup_inputs(port)
size(400, 400)
fill(0)
for _ in range(NUM_PONTOS):
x, y = random(width), random(height)
pontos.add(Ponto(x, y)) # acrescenta um Ponto
def draw():
global TAM_BARRA, NUM_PONTOS, VEL_MAX, NUM_CONNECT
@ -49,142 +27,41 @@ def draw():
NUM_PONTOS = int(B.val / 4)
VEL_MAX = C.val / 128
NUM_CONNECT = 1+ int(D.val / 256)
update_num_pontos()
# para cada ponto
for ponto in pontos:
for ponto in Ponto.SET:
ponto.desenha() # desenha
ponto.move() # atualiza posição
ponto.move(VEL_MAX) # atualiza posição
# para cada aresta
for aresta in arestas: # checa se há Arestas com Pontos já removidos
if (aresta.p1 not in pontos) or (aresta.p2 not in pontos):
arestas.remove(aresta) # nesse caso remove a Aresta também
for aresta in Aresta.ARESTAS: # checa se há Arestas com Pontos já removidos
if (aresta.p1 not in Ponto.SET) or (aresta.p2 not in Ponto.SET):
Aresta.ARESTAS.remove(aresta) # nesse caso remove a Aresta também
else: # senão
aresta.desenha() # desenha a linha
aresta.puxa_empurra() # altera a velocidade dos pontos
# if Inputs.TILT:
# background(128)
aresta.puxa_empurra(TAM_BARRA) # altera a velocidade dos pontos
# atualiza número de pontos
if NUM_PONTOS > len(Ponto.SET):
Ponto.SET.add(Ponto(random(width), random(height), NUM_CONNECT))
elif NUM_PONTOS < len(Ponto.SET):
Ponto.SET.remove(rnd_choice(list(Ponto.SET)))
if Inputs.TILT:
Ponto.SET = set()
# uncomment next lines to export GIF
if not frameCount % 30:
gif_export(GifMaker,
frames=2000,
delay=500,
filename=SKETCH_NAME)
# if not frameCount % 30:
# gif_export(GifMaker,
# frames=2000,
# delay=500,
# filename=SKETCH_NAME)
# Updates reading or draws sliders and checks mouse dragging / keystrokes
Inputs.update_inputs()
# Sob clique do mouse seleciona/deseleciona Pontos ou Arestas
def mouseClicked():
for ponto in pontos: # para cada Ponto checa distância do mouse
if dist(mouseX, mouseY, ponto.x, ponto.y) < TAM_PONTO / 2:
ponto.sel = not ponto.sel # inverte status de seleção
mouse = PVector(mouseX, mouseY)
def keyPressed(): # Quando uma tecla é pressionada
# Barra de espaço acrescenta Pontos na posição atual do mouse
if key == ' ':
pontos.add(Ponto(mouseX, mouseY)) # acrescenta Ponto no set
def mouseDragged(): # quando o mouse é arrastado
for ponto in pontos: # para cada Ponto checa distância do mouse
for ponto in Ponto.SET: # para cada Ponto checa distância do mouse
if dist(mouseX, mouseY, ponto.x, ponto.y) < TAM_PONTO / 2:
# move o Ponto para posição do mouse
ponto.x, ponto.y = mouseX, mouseY
ponto.vx = 0
ponto.vy = 0
def update_num_pontos():
print(NUM_PONTOS, len(pontos), NUM_CONNECT)
if NUM_PONTOS > len(pontos):
pontos.add(Ponto(random(width), random(height)))
elif NUM_PONTOS < len(pontos):
pontos.remove(rnd_choice(list(pontos)))
class Ponto():
" Pontos num grafo, VEL_MAX inicial sorteada, criam Arestas com outros Pontos "
def __init__(self, x, y, cor=color(0)):
self.x = x
self.y = y
self.z = 0 # para compatibilidade com PVector...
self.vx = random(-VEL_MAX, VEL_MAX)
self.vy = random(-VEL_MAX, VEL_MAX)
self.sel = False # se está selecionado, começa sem seleção
self.cor = color(random(128, 255), # R
random(128, 255), # G
random(128, 255), # B
128) # Alpha ~50%
self.cria_arestas()
def desenha(self):
if self.sel:
stroke(0)
else:
noStroke()
fill(self.cor)
ellipse(self.x, self.y, TAM_PONTO, TAM_PONTO)
def move(self):
self.x += self.vx
self.y += self.vy
if not (0 < self.x < width):
self.vx = -self.vx
if not (0 < self.y < height):
self.vy = -self.vy
self.vx = self.limitar(self.vx, VEL_MAX)
self.vy = self.limitar(self.vy, VEL_MAX)
def cria_arestas(self):
for _ in range(NUM_CONNECT):
lista_pontos = list(pontos)
if lista_pontos:
nova_aresta = Aresta(rnd_choice(lista_pontos), self)
arestas.append(nova_aresta)
def limitar(self, v, v_max):
if v > v_max:
return v_max
elif v < -v_max:
return -v_max
else:
return v
class Aresta():
""" Arestas contém só dois Pontos e podem ou não estar selecionadas """
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
self.sel = False
def desenha(self):
if self.sel:
stroke(0)
else:
stroke(255)
line(self.p1.x, self.p1.y, self.p2.x, self.p2.y)
noStroke()
fill(255)
ellipse(self.p1.x, self.p1.y, TAM_PONTO / 6, TAM_PONTO / 6)
ellipse(self.p2.x, self.p2.y, TAM_PONTO / 6, TAM_PONTO / 6)
def puxa_empurra(self):
d = dist(self.p1.x, self.p1.y, self.p2.x, self.p2.y)
delta = TAM_BARRA - d
dir = PVector.sub(self.p1, self.p2)
dir.mult(delta / 1000)
self.p1.vx = self.p1.vx + dir.x
self.p1.vy = self.p1.vy + dir.y
self.p2.vx = self.p2.vx - dir.x
self.p2.vy = self.p2.vy - dir.y
def rnd_choice(collection):
i = int(random(len(collection)))
return collection[i]

Wyświetl plik

@ -1,23 +0,0 @@
COLORS = [color(0), color(0), color(0),
color(255), color(255),
color(200, 0, 100),
]
def circle(x, y, s):
ellipse(x, y, s, s)
def square(x, y, s):
rectMode(CENTER)
rect(x, y, s, s)
def exes(x, y, s):
with pushMatrix():
translate(x, y)
line(-s / 2, -s / 2, s / 2, s / 2)
line(s / 2, -s / 2, -s / 2, s / 2)
def losang(x, y, s):
with pushMatrix():
translate(x, y)
rotate(radians(45))
rect(0, 0, s, s)