From 59cf07049af068ede40af12e73570a440b9b751a Mon Sep 17 00:00:00 2001 From: Alexandre B A Villares <3694604+villares@users.noreply.github.com> Date: Sat, 11 Jan 2025 00:07:13 -0300 Subject: [PATCH] Create sketch_2025_01_09-10.py --- .../sketch_2025_01_09/sketch_2025_01_09-10.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 2025/sketch_2025_01_09/sketch_2025_01_09-10.py diff --git a/2025/sketch_2025_01_09/sketch_2025_01_09-10.py b/2025/sketch_2025_01_09/sketch_2025_01_09-10.py new file mode 100644 index 00000000..77201c65 --- /dev/null +++ b/2025/sketch_2025_01_09/sketch_2025_01_09-10.py @@ -0,0 +1,62 @@ +# Wolfram's cellular automata + some color +# Inspired by something made by Carlos @vamoss a long time ago. +from random import choice +import numpy as np +import py5 + +rows = cols = 100 + +def setup(): + py5.size(800, 800) + py5.no_smooth() + start() + +def start(): + global matrix, gen, ruleset + matrix = np.zeros((cols, rows), dtype=int) + matrix[cols // 2, 0] = 1 # Starts with "1" in the middle of the first line + #matrix[:, 0] = [choice((0, 1)) for _ in range(cols)] + gen = 0 + #ruleset = [choice((0, 1)) for _ in range(8)] + ruleset = [1, 0, 1, 0, 0, 1, 1, 0] + + print(ruleset) + + +def draw(): + py5.background(200) + py5.scale(8) + offset = gen % rows + for i in range(cols): + for j in range(rows): + y = j - offset + if y <= 0: + y = rows + y + if matrix[i, j] == 1: # Se a célula i, está viva/ativa/1 + left = matrix[(i + cols - 1) % cols, j - 1] + centre = matrix[i, j - 1] + right = matrix[(i + 1) % cols, j - 1] + c = py5.color(55 + left * 100, 55 + centre * 100, 55 + right * 100) + py5.stroke(c) + else: + py5.stroke(200) + py5.point(i, y - 1) + new_gen() + +def new_gen(): + global gen + for i in range(cols): + left = matrix[(i + cols - 1) % cols, gen % rows] + centre = matrix[i, gen % rows] + right = matrix[(i + 1) % cols, gen % rows] + result = ruleset[left * 4 + centre * 2 + right * 1] + matrix[i, (gen + 1) % rows] = result + gen += 1 + +def key_pressed(): + if py5.key == ' ': + start() + else: + py5.save_frame('####.png') + +py5.run_sketch() \ No newline at end of file