### Sesc Av. Paulista
## Grupo de estudos em Python
# `hackmd.io/@sesc-av-paulista/estudos-em-python-22-maio`
## Noise e outros tipos de aleatoriedade
- referência: https://abav.lugaralgum.com/material-aulas/Processing-Python-py5/noise.html
- py5
- https://en.wikipedia.org/wiki/Perlin_noise
### Primeiro exemplo `random()` vs. `noise()`

### Noise em 2D (e comparando com `os_noise()`)

### Exemplo muito lento 360.000 consultas e `point()`

### Exemplo com 360 consultas em retícula de círculos coloridos (HSB)

### Exemplo com 3D (x, y, frame_count)
Variando o tamanho do círculos

### Super resumo box em 3D

### Em 3D com cubos (fica bastante lento)
```python=
def setup():
size(600, 600, P3D)
color_mode(HSB)
def draw():
background(0)
translate(width / 2, height / 2, -height / 2)
rotate_y(PI / 6)
step = 0.005
# OS Noise
noise_seed(1)
w = 30
for x in range(0, 600, w):
for y in range(0, 600, w):
for z in range(0, 600, w):
n = os_noise((x + mouse_x) * step,
(y + mouse_y) * step,
z * step,
frame_count * step
)
r = 128 + n * 127
no_stroke()
fill(r, 200, 200)
d = remap(n, -1, 1, 0, w)
xyz_box(x - width / 2,
y - height / 2,
z - height / 2, d)
window_title(str(round(get_frame_rate())))
def xyz_box(x, y, z, *args):
push_matrix()
translate(x, y, z)
box(*args)
pop_matrix()
```
### 3D com PeasyCam
```python=
# só precisa rodar uma vez. Disponível no py5 0.10.6a0
# import py5_tools
# py5_tools.processing.download_library('PeasyCam')
from peasy import PeasyCam
from itertools import product
def setup():
size(600, 600, P3D)
color_mode(HSB)
this = get_current_sketch()
cam = PeasyCam(this, 600)
def draw():
background(0)
#translate(width / 2, height / 2, -height / 2)
#rotate_y(PI / 6)
step = 0.005
# OS Noise
noise_seed(1)
w = 30
for x, y, z in product(range(0, 600, w), repeat=3):
n = os_noise(x * step,
y * step,
z * step,
frame_count * 10 * step
)
r = 128 + n * 127
stroke(r, 200, 200)
d = remap(n, -1, 1, 0, w)
stroke_weight(d)
point(x - width / 2,
y - height / 2,
z - height / 2)
window_title(str(round(get_frame_rate())))
```
# Noise com NumPy
- https://github.com/py5coding/py5generator/discussions/536
```python=
from itertools import product
import py5
import numpy as np
step_scale = 0.003
def setup():
global screen_grid, noise_space_grid, len_grid
py5.size(500, 500, py5.P3D)
py5.color_mode(py5.HSB)
py5.stroke_weight(5)
grid = list(product(range(0, py5.height, 5), range(0, py5.width, 5)))
len_grid = len(grid)
screen_grid = np.array((grid))
def draw():
global screen_grid, remapped_values, positions_and_values
py5.window_title(f'{py5.get_frame_rate():.1f}')
py5.translate(py5.width/2, py5.height/2, -py5.height/2)
py5.rotate_x(py5.radians(45))
py5.translate(-py5.width/2, -py5.height/2)
py5.background(0)
offset = np.array((-py5.mouse_x * 10, -py5.mouse_y * 10))
t = py5.frame_count * 10 * step_scale
noise_space_grid = (screen_grid + offset) * step_scale
noise_values = py5.os_noise(
noise_space_grid[:,0],
noise_space_grid[:,1],
np.full(len_grid, t)
)
remapped_values = (noise_values + 1) / 2 * 255 # could have been remap()
# positions_and_values = np.hstack((screen_grid, remapped_values.reshape(-1, 1)))
# for x, y, n in positions_and_values:
# py5.stroke(n, 255, 255)
# py5.point(x, y, n)
colored_points(screen_grid[:,0], screen_grid[:,1], remapped_values)
@np.vectorize
def colored_points(x, y, n):
py5.stroke(n, 255, 255)
py5.point(x, y, n)
# at some point we should try a Py5Shape with py5.begin_shape(py5.POINTS)
# & vertices() & set_strokes() but I'm tired
py5.run_sketch(block=False)
```