From 39d3abb58a09095834f7caa7cac492399041cfe8 Mon Sep 17 00:00:00 2001 From: Alexandre B A Villares <3694604+villares@users.noreply.github.com> Date: Mon, 13 Jan 2025 09:52:28 -0300 Subject: [PATCH] Create sketch_2025_01_13.py --- 2025/sketch_2025_01_13/sketch_2025_01_13.py | 85 +++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 2025/sketch_2025_01_13/sketch_2025_01_13.py diff --git a/2025/sketch_2025_01_13/sketch_2025_01_13.py b/2025/sketch_2025_01_13/sketch_2025_01_13.py new file mode 100644 index 00000000..b32ed0ff --- /dev/null +++ b/2025/sketch_2025_01_13/sketch_2025_01_13.py @@ -0,0 +1,85 @@ +import py5 + +margem = 10 + + +def setup(): + py5.size(800, 800) + start_polygons() + +def start_polygons(n=7): + global poligonos + W = py5.width - 2 * margem + poligonos = [ + ((0, 0), (W, 0), (W, W), (0, W)), + ] + for _ in range(n): + dividir_poligonos() + +def draw(): + py5.background(0) + py5.color_mode(py5.HSB) + py5.translate(margem, margem) + for shp in poligonos: + xc, yc = centroide(shp) + x0, y0 = shp[0] + w, h = py5.width, py5.height + d = py5.dist(xc, yc, x0, y0) + c = py5.color(0 + (yc * 2) % 128, + 255 - d, + 192 + xc % 64, + ) + py5.fill(c) + py5.stroke(0) + py5.stroke_weight(1.5) + py5.begin_shape() + py5.vertices(shp) + py5.end_shape(py5.CLOSE) + +def dividir_quad(q): + d1 = py5.dist(*q[0], *q[2]) + d2 = py5.dist(*q[1], *q[3]) + if d1 < d2: + return q[:3], q[2:] + q[:1] + else: + return q[1:4], q[3:] + q[:2] + +def dividir_tri(t): + b, c, a = t + ab = centroide((a, b)) + bc = centroide((b, c)) + ca = centroide((c, a)) + ta, tb = dividir_quad((ca, ab, bc, c)) + return ( + (ab, bc, b), + (ab, a, ca), + ta, tb, + ) + +def centroide(s): + xs, ys = zip(*s) + return (sum(xs) / len(xs), + sum(ys) / len(ys)) + +def key_pressed(): + if py5.key == ' ': + py5.save_frame('out.png') + +def dividir_poligonos(): + novos_poligonos = [] + for i, p in enumerate(poligonos): + if i % 2 == 0: + novos_poligonos.extend(dividir(p)) + else: + novos_poligonos.append(p) + poligonos[:] = novos_poligonos + + +def dividir(p): + if len(p) == 4: + return dividir_quad(p) + else: + return dividir_tri(p) + +py5.run_sketch() +