Por que razão saltos ao acaso desenham um triângulo perfeito
Salte metade da distância até um vértice escolhido ao acaso, vezes sem conta. Do puro acaso surge um fractal perfeito.
É reproduzido a partir do YouTube depois de um clique.
Experimente: mude a regra
Puro acaso, uma regra: o triângulo de Sierpiński, três cópias de si mesmo com metade do tamanho.
Por que razão funciona
Escolha ao acaso um dos três vértices de um triângulo e salte metade da distância até ele. Marque o ponto e repita. Os saltos são puro acaso, mas depois de alguns milhares aparece o triângulo de Sierpiński: um triângulo com triângulos recortados, em todas as escalas.
xₖ₊₁ = xₖ + r · (vértice − xₖ)triângulo: r = 1/2pentágono: r ≈ 0,618hexágono: r = 2/3
Construa-o em Python
Python 3 · numpy · matplotlib60 000 saltos ao acaso, cada um até metade da distância a um vértice, com cores segundo o vértice.
"""Chaos game: jump a part r of the way to a random corner of a polygon, again and again.
Run: python chaos-game.py (needs: pip install numpy matplotlib)
"""
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("dark_background")
corners, r, no_repeat = 3, 0.5, False # triangle, halfway: the Sierpinski triangle
steps = 60000
rng = np.random.default_rng()
angles = np.pi / 2 + 2 * np.pi * np.arange(corners) / corners
vertex = np.column_stack([np.cos(angles), np.sin(angles)])
points, chosen = np.zeros((steps, 2)), np.zeros(steps, dtype=int)
x, last = np.array([0.0, 0.0]), -1
for i in range(steps):
c = rng.integers(corners)
while no_repeat and c == last: # the restricted game: never the same corner twice
c = rng.integers(corners)
x = x + r * (vertex[c] - x) # the whole rule
points[i], chosen[i], last = x, c, c
plt.figure(figsize=(6, 6))
plt.scatter(points[20:, 0], points[20:, 1], s=0.3, c=chosen[20:], cmap="rainbow")
plt.axis("equal")
plt.axis("off")
plt.show()
# Try: corners, r = 5, 0.618 (a pentagon) or corners, no_repeat = 4, True (a square, restricted)
Experimente: Defina corners = 5 e r = 0.618, ou corners = 4 com no_repeat = True.
Aprenda este script linha a linha, com exercícios e um quiz → · execute-o no Google Colab (em inglês) →
Para professores: um plano de aula de 45 minutos e uma folha de exercícios em três níveis →
A matemática, tal como o filme a conta
Ler as notas do próprio filme (do YouTube, em inglês)
Start anywhere. Pick one of the triangle's three corners at random and jump exactly halfway toward it. Plot the point.
Repeat. Nothing about the choices is planned — a coin, a die, pure chance — and yet after a few thousand jumps the Sierpiński triangle appears, sharp and complete. The reason is that "jump halfway to a corner" is a contraction: it shrinks every distance by half, so wherever the point starts, after a handful of jumps it is pulled onto the one shape that the three half-size copies of itself map back onto — the fractal is the only set the rule leaves unchanged.
Each point here is coloured by the corner it just jumped toward, which is why order seems to grow out of rainbow noise. From the triangle on, only the rule changes: a pentagon with a golden jump of 0.618, a pentagon where the same corner may never be chosen twice in a row, a square where the opposite corner is forbidden, a hexagon with a two-thirds jump. Every new shape is born from the cloud of the previous one, because the walkers simply keep going under the new rule.
Nothing is drawn; everything is counted.
Da descrição de Pure Randomness Draws a Perfect Fractal (em inglês).