## Funções que desenho elementos de quadrinhos
[índice das aulas](https://hackmd.io/@villares/sesc-quadrinhos-prog)
### Balão oval

```python!
def setup():
size(400, 400)
balao_oval(200, 200, 400, 200)
def balao_oval(x, y, w, h):
stroke_join(ROUND)
stroke_weight(5) # note que no triângulo fica "meia espessura"
# no_fill() # para mostrar construção
triangle(x - w * 0.45, y,
x, y,
x + w * 0.45, y + h * 0.45)
ellipse(x, y, w * 0.9, h * 0.9)
no_stroke() # triangulo sem traço para "furar" a ellipse
# triangulo mata meia espessura, mas fica legal
triangle(x - w * 0.45, y,
x, y,
x + w * 0.45, y + h * 0.45)
```
### Balão poligonal
`

``
```python!
def setup():
size(400,400)
background (0, 0, 200)
stroke_weight(3)
balao(100, 100, 100, 100)https://lugaralgum.com/hackmd/H1eMj5opn.png
balao(250, 200, 200, 50, flip=True)
def balao(x, y, w, h, gola=20, flip=False):
pontos = [
(-w/2, -h/2),
(w/2, -h/2),
(w/2, +h/2),
(0, +h/2),
(0, +3*h/2),
(-gola, +h/2),
(-w/2, +h/2),
]
if flip:
pontos = [(-xp,yp) for xp, yp in pontos]
begin_shape()
for xv, yv in pontos:
vertex(x + xv, y + yv)
end_shape(CLOSE)
```
### Nuvem e explosão

```python
drag = None
pts = [
(200, 100),
(100, 100),
]
num_points = 12
def setup():
size(600, 600)
def draw():
global num_points
background(200)
stroke(0)
stroke_weight(5)
fill(255)
star(300, 300, pts[0][0], pts[0][1], num_points, w_a=pts[1][0]/100, w_b=pts[1][1]/100)
fill(200, 0, 0)
no_stroke()
for x, y in pts:
circle(x, y, 15)
text_size(14)
text(f'raio a: {pts[0][0]}', 50, 500)
text(f'raio b: {pts[0][1]}', 50, 520)
text(f'fator a: {pts[1][0]/100:.2}', 50, 540)
text(f'fator b: {pts[1][1]/100:.2}', 50, 560)
def star(x, y, radius_a, radius_b, n_points, rot=0, w_a=1, w_b=1):
step = TWO_PI / n_points
begin_shape()
for i in range(n_points + 1):
ang = i * step + rot
sx = cos(ang) * radius_a * w_a
sy = sin(ang) * radius_a
cx = cos(ang + step / 2.0) * radius_b * w_b
cy = sin(ang + step / 2.0) * radius_b
if i == 0:
vertex(x + cx, y + cy)
else:
quadratic_vertex(x + sx, y + sy, x + cx, y + cy)
end_shape()
def mouse_pressed():
global drag
for i, (x, y) in enumerate(pts):
if dist(mouse_x, mouse_y, x, y) < 10:
drag = i
break
def mouse_dragged():
if drag is not None:
pts[drag] = (mouse_x, mouse_y)
def mouse_released():
global drag
drag = None
```

Recortes

```python=
def setup():
global img
size(600, 400)
# https://pt.wikipedia.org/wiki/Ficheiro:Mappa_Topographico_do_Municipio_de_S%C3%A3o_Paulo_-_Folha_37,_Acervo_do_Museu_Paulista_da_USP.jpg
img = load_image('/home/villares/GitHub/sketch-a-day/2023/sketch_2023_07_21/mapa.jpg')
no_loop()
image_mode(CENTER)
def draw():
background(0, 0, 200)
w = h = 200
stroke_weight(15)
mask = make_mask(w + 10, h + 10)
clipped = clip_with_mask(
img, mask,
random(0, img.width - w),
random(0, img.height - h))
image(mask, 100, 200)
image(clipped, 300, 200)
image(mask, 500, 200)
image(clipped, 500, 200)
save_frame('####.png')
def make_mask(w, h):
m = create_graphics(int(w), int(h))
m.begin_draw()
m.stroke_weight(5)
m.circle(w / 2, h / 2, w - 10)
m.fill(0, 100)
m.rect(w / 2, h / 2, 100, 100)
m.fill(0)
m.no_stroke()
m.rect(w / 2 - 100, h / 2 - 100, 100, 100)
m.end_draw()
return m
def clip_with_mask(img, mask, x, y):
"""Clip an image using a mask, its dimensions, and a position."""
w, h = mask.width, mask.height
result = create_image(w, h, ARGB)
result.copy(img, int(x), int(y), w, h, 0, 0, w, h)
result.mask(mask)
return result
def key_pressed():
redraw()
```