---
# System prepended metadata

title: Grupo de estudos em Python - Gestos - 20/março/2025

---

# Grupo de estudos em Python

## Gestos
## `https://hackmd.io/@sesc-av-paulista/estudos-em-python-20-marco`



### Primeiro exemplo com lista
```python=

pontos = []

def setup():
    size(600, 600)
    
def draw():
    background(0, 0, 100)
    for x, y in pontos:
        fill(random(255), 200)
        circle(x, y, random(10, 30))

def mouse_dragged():
    pontos.append((mouse_x, mouse_y))

```
![image](https://hackmd.io/_uploads/B1FvF0K2yg.png)


```python
>>> for t in enumerate(frutas):
    print(t)
    
(0, 'avocado')
(1, 'mamão')
(2, 'manga')

>>> for numero, fruta in enumerate(frutas, 1):
    print(numero, fruta)
    
1 avocado
2 mamão
3 manga
```


## Exemplo com `deque`

![image](https://hackmd.io/_uploads/HJtTJk5n1g.png)

```python=
from collections import deque

pontos = deque(maxlen=300)

def setup():
    size(600, 600)
    color_mode(HSB)  # Hue(matiz), Sat, Bri
    
def draw():
    background(0, 0, 100)
    for i, (x, y) in enumerate(pontos):
        f = i + frame_count
        fill(f % 255, 200, 200)
        circle(x, y, f % 100)

#         d = 30 + 20 * sin(radians(f))
#         circle(x, y, d)

def mouse_dragged():
    pontos.appendleft((mouse_x, mouse_y))
    print(len(pontos))


def key_pressed():
    if key == ' ':
        pontos.clear()

```


### Gesto 2D

![2D-5488](https://gist.github.com/user-attachments/assets/21b19933-73e0-4f00-b77c-d28596d3e84a)
```python=
"""
Gesto em 2D

"s" para salvar e barra de espaço para recomeçar

Sketch escrito em estilo "module mode"
No Thonny, use com o plguing py5 "imported mode" desativado
"""
from collections import deque  # a double-ended queue

import py5  # check out https://github.com/py5coding 
from py5_tools import animated_gif

history = deque(maxlen=360)  # mouse dragged positions

def setup():   # py5 will call this once to set things up
    py5.size(600, 600)
    py5.color_mode(py5.HSB)
    py5.no_stroke()

def draw():   # py5 will call this in a loop
    py5.background(0)
    for i, (x, y) in enumerate(history):
        h = py5.remap((i - py5.frame_count) % 360, 0, 360, 0, 255)
        d = py5.remap((i - py5.frame_count) % 360, 0, 360, 8, 32)
        py5.fill(h , 255, 255, 150)
        py5.circle(x, y, d)
    if history:
        history.append(history.popleft())  # circula os valores do deque
        # remove da esquerda (começo) e acrescenta na direita (append normal no fim)

def mouse_dragged():  # py5 will call this when you drag the mouse
    history.append((py5.mouse_x, py5.mouse_y))
    
def key_pressed():   # py5 will call this when a key is pressed
    if py5.key == ' ':
        history.clear()
    elif py5.key == 's':
        f = py5.frame_count
        animated_gif(f'2D-{py5.frame_count}.gif', frame_numbers=range(f + 1, f + 361, 4), duration=0.075) 
        
py5.run_sketch()


```


### Gesto 3D


![3D-12044](https://gist.github.com/user-attachments/assets/31b02d46-46eb-49c9-b080-a300536085c4)


```python
"""
Gesto em 3D
 "s" para salvar e barra de espaço para recomeçar

Sketch escrito em estilo "module mode"
No Thonny, use com o plguing py5 "imported mode" desativado
"""
from collections import deque  # a double-ended queue

import py5  # check out https://github.com/py5coding 
from py5_tools import animated_gif

history = deque(maxlen=360)  # mouse dragged positions

def setup():   # py5 will call this once to set things up
    py5.size(600, 600, py5.P3D)
    py5.color_mode(py5.HSB)

def draw():   # py5 will call this in a loop
    py5.background(0)
    for i, (x, y, r) in enumerate(history):
        with py5.push_matrix():
            py5.translate(py5.width / 2, py5.height / 2) # origem no meio da tela
            py5.rotate_y(r - py5.radians(py5.frame_count))  # rotação geral
            py5.translate(-py5.width / 2, -py5.height / 2)  # volto sistema de coordenadas
            h = py5.remap((i - py5.frame_count) % 360, 0, 360, 0, 255)
            py5.fill(h , 255, 255, 150)
            py5.no_stroke()
            py5.translate(x, y) # (i + py5.frame_count) % 3 - 1)
            #py5.rotate_y(-r + py5.radians(py5.frame_count))  # virar bolinha para camera
            d = py5.remap((i - py5.frame_count) % 360, 0, 360, 8, 32)
            py5.circle(0, 0, d)
    if history:
        history.append(history.popleft())

def mouse_dragged():  # py5 will call this when you drag the mouse
    history.append((py5.mouse_x, py5.mouse_y, py5.radians(py5.frame_count)))
    
def key_pressed():   # py5 will call this when a key is pressed
    if py5.key == ' ':
        history.clear()
    elif py5.key == 's':
        f = py5.frame_count
        animated_gif(f'3D-{py5.frame_count}.gif', frame_numbers=range(f + 1, f + 361, 4), duration=0.075) 
        
py5.run_sketch()


```

# Gesto que anda!

![image](https://hackmd.io/_uploads/Sys7Bxq2yg.png)


```python
from collections import deque

gestos = deque([], maxlen=10)
gesto_em_curso = deque()
print(gestos)

def setup():
    size(600, 600)
    color_mode(HSB)
    
def draw():
    background(0, 0, 100)
    for i, gesto in enumerate(gestos):
        fill((i * 16) % 255, 200, 200)
        for x, y in gesto:
            circle(x, y, 15)
        anima_gesto(gesto)
    no_fill()
    for x, y in gesto_em_curso:
            circle(x, y, 15)

def anima_gesto(gesto):
    if len(gesto) > 2:  # deque vazio é "falseane" ("falsy")
        a = gesto.pop()  # pega o último ponto/vetor (e remove do final deque)
        b = gesto[-1] # é o penúltimo que agora é o último...
        r = b - a # vetor de deslocamento do último para o penúltimo ponto 
        p = gesto[0]
        pr = p + r  # aplicando o vetor no primeiro ponto/vetor
        
        # pular as bordas da tela
        pr.x = pr.x % width
        pr.y = pr.y % height
        
        gesto.appendleft(pr)  # acrescenta o novo primeito ponto
    
def mouse_dragged():
    if dist(mouse_x, mouse_y, pmouse_x, pmouse_y) > 2:
        gesto_em_curso.append(Py5Vector(mouse_x, mouse_y))

def mouse_released():
    gestos.append(gesto_em_curso.copy())
    gesto_em_curso.clear()    

    

def key_pressed():
    if key == ' ':
        pontos.clear()

```

Com subtração do vetor temos uns movimentos cíclicos no lugar em vez de andar longe.

![image](https://hackmd.io/_uploads/BJ7lUxcn1g.png)
