---
# System prepended metadata

title: Grupo de estudos em Python - Random walkers - 27/3

---

# Grupo de estudos em Python 

## Caminhantes aleatórios
## `https://hackmd.io/@sesc-av-paulista/estudos-em-python-27-marco`


Referência: Deconbatch
- https://www.deconbatch.com/2020/02/the-joy-of-random-walking.html



#### Primeiro exemplo
![image](https://hackmd.io/_uploads/r1dAkzmakl.png)

```python=
x = 300
y = 300

def setup():
    size(600, 600)
    no_smooth()
    
def draw():
    global x, y
    fill(0)
    circle(x, y, 2)
    x = x + random(-2, 2)
    y = y + random(-2, 2)
```


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

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

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

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


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


### Com OO, um primeiro obj. Walker

```python


def setup():
    global w
    size(600, 600)
    color_mode(HSB) # hue(matiz), sat, bri
    w = Walker(300, 300)
    
def draw():
    w.update()
 
class Walker():
        
    def __init__(self, x, y):
        self.pos = Py5Vector(x, y)
        self.pos_anterior = Py5Vector(x, y)	
        self.matiz = 0

    def update(self):
        self.pos = self.pos + Py5Vector.random(dim=2) * 5
        stroke(self.matiz, 200, 200)
        line(self.pos_anterior.x,
             self.pos_anterior.y,
             self.pos.x, self.pos.y)
        self.pos_anterior = self.pos
        self.matiz = (self.matiz + 1) % 255
       
```

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

```python
walkers = []

def setup():
    global w
    size(600, 600)
    color_mode(HSB) # hue(matiz), sat, bri
    for _ in range(10):
        w = Walker(random(width), random(height))
        walkers.append(w)
        
def draw():
    for w in walkers:
        w.update()
 
class Walker():
        
    def __init__(self, x, y):
        self.pos = Py5Vector(x, y)
        self.pos_anterior = Py5Vector(x, y)	
        self.matiz = 0

    def update(self):
        self.pos += Py5Vector.random(dim=2) * 5
        stroke(self.matiz, 200, 200)
        line(self.pos_anterior.x,
             self.pos_anterior.y,
             self.pos.x, self.pos.y)
        self.pos_anterior = self.pos.copy
        self.matiz = (self.matiz + 1) % 255
       
```


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

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


### Removendo walkers desativados no loop

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


```python
walkers = []

def setup():
    size(600, 600)
    background(0)
    color_mode(HSB) # hue(matiz), sat, bri
        
def draw():
    for w in walkers[:]:
        w.update()
        if w.tamanho < 1:
            walkers.remove(w)
    print(len(walkers))

def mouse_dragged():
    w = Walker(mouse_x, mouse_y)
    walkers.append(w)
        
class Walker():
        
    def __init__(self, x, y):
        self.pos = Py5Vector(x, y)
        self.pos_anterior = Py5Vector(x, y)	
        self.tamanho = 50

    def update(self):
        if self.tamanho > 1:
            self.pos += Py5Vector.random(dim=2) * 5
            # circle(self.pos.x, self.pos.y, self.tamanho)
            circle(*self.pos, self.tamanho)
            self.tamanho *= 0.99
    
```


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

```python
  def update(self):
        if self.tamanho > 1:
            color_mode(HSB) # hue(matiz), sat, bri
            no_stroke()
            fill(100 + self.tamanho * 2, 200, 200, 255 - self.tamanho * 5)
            self.pos += Py5Vector.random(dim=2) * 5
            # circle(self.pos.x, self.pos.y, self.tamanho)
            circle(*self.pos, self.tamanho)
            self.tamanho *= 0.99
```


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