Alexandre B A Villares 2025-10-12 23:41:43 -03:00
rodzic 879a08d091
commit 819501b425
3 zmienionych plików z 120 dodań i 0 usunięć

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 775 KiB

Wyświetl plik

@ -0,0 +1,110 @@
import py5
import py5_tools
axiom = "X"
rules = {
'X': 'F+[[X]-X]-F[-FX]+X',
'F': 'FF'
}
R = 'X' # which rule to randomize
step = 5
#angle = 22.5
iterations = 4 # repeticoes (voltas na aplicação das regraas)
G = 6 # grid cols and rows
W = 200 # grid width
seqs = []
def setup():
py5.size(600, 600, py5.P3D)
py5.color_mode(6, 'magma', 255) # py5.CMAP, py5.mpl_cmaps.VIRIDIS, 255 (alpha range)
py5.text_align(py5.CENTER, py5.TOP)
py5.text_size(12)
py5.fill('gray')
py5.random_seed(1)
generate_all()
py5_tools.animated_gif('out_.gif', frame_numbers=range(1, 91), duration=0.15)
def generate_all():
seqs.clear()
for k in range(G * G):
rules = new_rules()
seqs.append(generate())
def generate():
starting_sequence = axiom
for i in range(iterations):
sequence = ""
for symbol in starting_sequence:
sequence += rules.get(symbol, symbol)
starting_sequence = sequence
return sequence
def draw():
global angle
angle = -90 + py5.frame_count * 2
py5.background(float('NaN'))
k = 0
for i in range(G):
x = i * W
for j in range(G):
y = j * W
draw_sequence(seqs[k], x + W / 2, y + 3 * W / 4)
py5.text(rules[R], x + W / 2, y + W * 0.95)
k += 1
def draw_sequence(sequence, x, y):
with py5.push_matrix():
py5.translate(x, y)
#py5.rotate_y(py5.radians(py5.frame_count * 8))
for i, symbol in enumerate(sequence):
if symbol == "F":
py5.stroke(i / len(sequence) * 255)
py5.line(0, 0, 0, -step) # desenha uma linha
py5.translate(0, -step) # move a origem
if symbol == "+":
py5.rotate(py5.radians(angle))
py5.rotate_y(py5.radians(angle))
if symbol == "-":
py5.rotate(py5.radians(-angle))
py5.rotate_y(py5.radians(-angle))
if symbol == "[":
py5.push_matrix() # grava o estado (posição e ângulo)
if symbol == "]":
py5.pop_matrix() # volta ao último estado gravado
if symbol == '0':
with py5.push_style():
py5.fill(255 - i / len(sequence) * 255)
py5.no_stroke()
py5.circle(0, 0, step / 2)
def key_pressed():
if py5.key == ' ':
generate_all()
elif py5.key == 's':
py5.save_frame(f'###.png')
def new_rules():
while True:
new_rule = ''.join(py5.random_permutation(rules[R]))
if check_rule(new_rule):
rules[R] = new_rule
break
return rules
def check_rule(rule):
push_count = 0
for symbol in rule:
if symbol == '[':
push_count +=1
elif symbol == ']':
push_count -=1
if push_count < 0:
return False
if push_count != 0: # This shouldn't be needed with permutations
return False # that always have balanced [s and ]s.
return True
py5.run_sketch(block=False)

Wyświetl plik

@ -26,6 +26,16 @@ If you appreciate what I have been doing, you may also support my artistic work,
<!-- SKETCHES_START -->
---
### sketch_2025_10_12
![sketch_2025_10_12](https://raw.githubusercontent.com/villares/sketch-a-day/main/2025/sketch_2025_10_12/sketch_2025_10_12.gif)
[sketch_2025_10_12](https://github.com/villares/sketch-a-day/tree/main/2025/sketch_2025_10_12) [[py5](https://py5coding.org/)]
---
### sketch_2025_10_11