# Desenhos com py5 **Ferramentas**: - Como instalar Thonny (IDE com Python) + py5 https://abav.lugaralgum.com/como-instalar-py5/ - Plano B, editor online **pyp5js**: https://abav.lugaralgum.com/pyp5js/py5mode/ - Referência rápida: https://github.com/villares/processing.py-cheat-sheet/blob/pt-br/py5/py5_cc.pdf - Referência completa https://py5coding.org - [Mais material](https://github.com/villares/material-aulas/blob/main/Processing-Python-py5/README.md) ## Começo do polígono ```python= def setup(): size(600, 600) #rect(100, 100, 200, 50) # x, y ,largura, altura n = 16 raio = 250 a = TWO_PI / n x_centro, y_centro = 300, 300 begin_shape() for i in range(n): x = x_centro + raio * cos(a * i) y = y_centro + raio * sin(a * i) vertex(x, y) end_shape(CLOSE) ``` ## Grade ![](https://lugaralgum.com/hackmd/dXZ8rdV.png) ```python def setup(): size(600, 600) background(100, 100, 200) for x in range(100, 600, 100): for y in range(100, 600, 100): poligono(x, y, 40, 5) def poligono(xc, yc, raio, n=6): """ Desenha um polígono com o centro nas cooredenadas `xc`, `yc`, inscrito em um círculo com um certo `raio` e com o número de vértices `n`. """ a = TWO_PI / n # ângulo entre vertices, 360 / número de vertices begin_shape() for i in range(n): x = xc + raio * cos(a * i) y = yc + raio * sin(a * i) vertex(x, y) end_shape(CLOSE) ``` ![](https://lugaralgum.com/hackmd/n9D9pGs.png) ```python def setup(): size(600, 600) background(100, 100, 200) for x in range(100, 600, 100): for y in range(100, 600, 100): n = random_int(3, 11) fill(random(255), random(255), random(255)) poligono(x, y, 40, n) ``` ## Polígonos ![](https://lugaralgum.com/hackmd/GmrJPX0.png) ```python= def setup(): size(600, 600) no_stroke() begin_shape() vertices(pontos_poligono(300, 300, 100, 10)) end_shape(CLOSE) stroke(0) stroke_weight(5) points(pontos_poligono(300, 300, 100, 10)) for x, y in pontos_poligono(300, 300, 100, 10): no_fill() circle(x, y, 50) def pontos_poligono(xo, yo, raio, n=6): """ Devolve pontos de um polígono com o centro nas cooredenadas `xo`, `yo`, inscrito em um círculo com um certo `raio` e com o número de vértices `n`. """ a = TWO_PI / n # ângulo entre vertices, 360 / número de vertices fill(255, 255, 0) pontos = [] for i in range(n): x = xo + raio * cos(a * i) y = yo + raio * sin(a * i) pontos.append((x, y)) return pontos ``` Versão reduzida: ```python def pontos_poligono(xo, yo, raio, n=6): a = TWO_PI / n # ângulo entre vertices, 360 / número de vertices fill(255, 255, 0) return [(xo + raio * cos(a * i), yo + raio * sin(a * i)) for i in range(n)] ``` ### Peixe ![](https://lugaralgum.com/hackmd/ejrACgi.png) ### Pincel ```python def setup(): size(800, 800) rect_mode(CENTER) background(0, 255, 0) def draw(): no_fill() stroke(random(256), random(256), random(256)) if is_mouse_pressed: rect(mouse_x, mouse_y, 50, 50) def key_pressed(): if key == 'e': background(0, 255, 0) ``` ![árvore](https://lugaralgum.com/hackmd/ExYZwpn.png) ```python= # colorful tree / árvore colorida def setup(): size(600, 600) color_mode(HSB) def draw(): # laço/loop principal (ele fica repetindo) background(240) galho(300, 500, 90, -HALF_PI) def galho(x, y, length, direction): nx = x + length * cos(direction) ny = y + length * sin(direction) angle = radians(30) stroke((20 * direction) % 255, 200, 200) line(x, y, nx, ny) #x1, y1, x2, y2 shorten = 0.8 # reduz 20% quando multiplica if length > 5: galho(nx, ny, length * shorten, direction + angle) galho(nx, ny, length * shorten, direction - angle) ```