Fraktale · Gra w chaos

Dlaczego losowe skoki rysują idealny trójkąt

Reguła

Skacz w połowę drogi do losowego wierzchołka, raz za razem. Z czystego przypadku wyłania się idealny fraktal.

Film gra z YouTube po kliknięciu.

Demo na żywo · działa w przeglądarce

Spróbuj: zmień regułę

3
0,500

Czysty przypadek, jedna reguła: trójkąt Sierpińskiego, trzy jego kopie o połowę mniejsze.

Dlaczego to działa

Wybierz losowo jeden z trzech wierzchołków trójkąta i skocz w połowę drogi do niego. Zaznacz to miejsce i powtórz. Skoki są czystym przypadkiem, a jednak po kilku tysiącach pojawia się trójkąt Sierpińskiego: trójkąt z wyciętymi trójkątami, w każdej skali.

xₖ₊₁ = xₖ + r · (wierzchołek − xₖ)trójkąt: r = 1/2pięciokąt: r ≈ 0,618sześciokąt: r = 2/3

Zbuduj to w Pythonie

Python 3 · numpy · matplotlib

60 000 losowych skoków w połowę drogi do wierzchołka, kolory według wierzchołka.

chaos-game.py
"""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)

Spróbuj: Ustaw corners = 5 i r = 0.618 albo corners = 4 z no_repeat = True.

Poznaj ten skrypt linijka po linijce, z zadaniami i quizem → · uruchom go w Google Colab (po angielsku) →

Dla nauczycieli: karta lekcji na 45 minut i karta pracy na trzech poziomach →

Matematyka tak, jak opowiada ją film

Pokaż notatki z filmu (z YouTube, po angielsku)

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.

Z opisu filmu Pure Randomness Draws a Perfect Fractal (po angielsku).

Shorty

Źródła