// Kategorie Fraktal: Fraktale, seltsame Attraktoren und Zellautomaten #include "fx.h" // Farbe für Fraktal-Iterationen: innen dunkel, außen bunt static CRGB iterColor(uint8_t i, uint8_t maxIter, uint8_t shift) { if (i >= maxIter) return CRGB(0, 0, brightness / 10); return CHSV(i * 12 + shift, 255, scale8(min(255, brightness * 3), 90 + min(165, i * 20))); } // --- Mandelbrot: Zoom ins Seepferdchental; jede Etage eine Stufe tiefer --------- void renderMandelbrot(uint32_t now) { const float CX = -0.743643887f, CY = 0.131825904f; float t = ((now - fxStart) % 24000) / 1000.0f; float scale = 1.6f * powf(0.7f, t); // in 24 s rund 5000-fach uint8_t maxIter = 30 + t * 3; FOR_XYZ { float s = scale * powf(0.72f, z); float cr = CX + (x - MID) / MID * s, ci = CY + (y - MID) / MID * s; float zr = 0, zi = 0; uint8_t i = 0; for (; i < maxIter && zr * zr + zi * zi < 4; i++) { float tmp = zr * zr - zi * zi + cr; zi = 2 * zr * zi + ci; zr = tmp; } at(x, y, z) = iterColor(i, maxIter, t * 10); } } // --- Julia: der Parameter wandert im Kreis; jede Etage ist ein Stück voraus ----- void renderJulia(uint32_t now) { float t = (now - fxStart) * 0.00025f; const uint8_t MAX_ITER = 24; FOR_XYZ { float a = t + z * 0.06f; float cr = 0.7885f * cosf(a), ci = 0.7885f * sinf(a); float zr = (x - MID) / MID * 1.5f, zi = (y - MID) / MID * 1.5f; uint8_t i = 0; for (; i < MAX_ITER && zr * zr + zi * zi < 4; i++) { float tmp = zr * zr - zi * zi + cr; zi = 2 * zr * zi + ci; zr = tmp; } at(x, y, z) = iterColor(i, MAX_ITER, now / 60); } } // --- Bitmuster: Sierpinski- und XOR-Muster aus den Koordinaten, die durchwandern -- void renderBitPattern(uint32_t now) { uint32_t t = now - fxStart; uint8_t mode = t / 8000 % 3; uint8_t ox = t / 500, oy = t / 700, oz = t / 900; FastLED.clear(); FOR_XYZ { uint8_t X = x + ox, Y = y + oy, Z = z + oz; switch (mode) { case 0: // Sierpinski-Tetraeder if (((X & Y) | (Y & Z) | (X & Z)) % 8 == 0) at(x, y, z) = CHSV((X + Y + Z) * 10, 220, highlight); break; case 1: // XOR-Farben at(x, y, z) = CHSV((X ^ Y ^ Z) * 32 + t / 30, 255, scale8(min(255, brightness * 2), sin8(((X ^ Y ^ Z) & 7) * 32 - t / 6))); break; default: // UND-Gitter if ((X & Y & Z) % 4 != 0) at(x, y, z) = CHSV(160 + (X | Y | Z) * 8, 200, scale8(highlight, 200)); break; } } } // --- Fraktalbaum: wächst vom Stamm aus, jeder Ast teilt sich in zwei ------------- static int8_t treeX[NUM_LAYERS][8], treeY[NUM_LAYERS][8]; static uint8_t treeN[NUM_LAYERS]; static void growTree() { static const int8_t DIRS[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; treeX[0][0] = treeX[1][0] = 2; treeY[0][0] = treeY[1][0] = 2; treeN[0] = treeN[1] = 1; for (uint8_t z = 2; z < NUM_LAYERS; z++) { treeN[z] = 0; for (uint8_t i = 0; i < treeN[z - 1]; i++) { uint8_t a = random8(8), b = (a + random8(1, 8)) % 8; // zwei verschiedene Richtungen for (uint8_t d : {a, b}) { treeX[z][treeN[z]] = constrain(treeX[z - 1][i] + DIRS[d][0], 0, TOP); treeY[z][treeN[z]] = constrain(treeY[z - 1][i] + DIRS[d][1], 0, TOP); treeN[z]++; } } } } void renderFractalTree(uint32_t now) { static uint32_t cycleStart = 0; static uint8_t leafHue = 0; if (fresh || now - cycleStart > 9000) { fresh = false; cycleStart = now; leafHue = random8(); growTree(); } uint32_t t = now - cycleStart; uint8_t shown = min(NUM_LAYERS, t / 600 + 1); // Etage für Etage wachsen uint8_t fade = t < 7000 ? 255 : 255 - (t - 7000) * 255 / 2000; FastLED.clear(); for (uint8_t z = 0; z < shown; z++) for (uint8_t i = 0; i < treeN[z]; i++) { CRGB c = z < TOP ? CRGB(CHSV(20 + z * 15, 200, min(255, brightness * 2))) : CRGB(CHSV(leafHue + i * 4, 255, scale8(highlight, 160 + scale8(sin8(now / 5 + i * 40), 95)))); at(treeX[z][i], treeY[z][i], z) = c.nscale8(fade); } } // --- Chaosspiel: springt immer halb zu einer Ecke, es entsteht ein Sierpinski-Tetraeder void renderChaosGame(uint32_t now) { static float p[3] = {MID, MID, MID}; static const uint8_t TETRA[4][3] = {{0, 0, 0}, {TOP, TOP, 0}, {TOP, 0, TOP}, {0, TOP, TOP}}; fadeToBlackBy(leds, NUM_LEDS, 10); for (uint8_t n = 0; n < 6; n++) { uint8_t k = random8(4); for (uint8_t i = 0; i < 3; i++) p[i] = (p[i] + TETRA[k][i]) / 2; plot(p[0], p[1], p[2], CHSV(k * 64 + now / 100, 220, scale8(highlight, 120))); } } // --- Lorenz-Attraktor: der berühmte Schmetterling, als Leuchtspur --------------- void renderLorenz(uint32_t now) { static float x = 1, y = 1, z = 20; if (fresh) { fresh = false; x = rnd(-1, 1); y = 1; z = 20; } fadeToBlackBy(leds, NUM_LEDS, 22); for (uint8_t i = 0; i < 3; i++) { const float dt = 0.006f; float dx = 10 * (y - x), dy = x * (28 - z) - y, dz = x * y - 8.0f / 3 * z; x += dx * dt; y += dy * dt; z += dz * dt; } plotSoft((x + 20) / 40 * TOP, (y + 27) / 54 * TOP, (z - 3) / 47 * TOP, CHSV(x > 0 ? 20 : 150 + now / 200 % 40, 220, highlight)); } // --- Thomas-Attraktor: eine Spur windet sich durch alle Ecken des Würfels ------- void renderThomas(uint32_t now) { static float p[3] = {1.1f, 1.1f, -0.01f}; if (fresh) { fresh = false; p[0] = rnd(0.5f, 1.5f); p[1] = 1.1f; p[2] = -0.01f; } fadeToBlackBy(leds, NUM_LEDS, 14); for (uint8_t i = 0; i < 4; i++) { const float b = 0.208186f, dt = 0.05f; float d[3] = {sinf(p[1]) - b * p[0], sinf(p[2]) - b * p[1], sinf(p[0]) - b * p[2]}; for (uint8_t k = 0; k < 3; k++) p[k] += d[k] * dt; } plotSoft((p[0] + 4.5f) / 9 * TOP, (p[1] + 4.5f) / 9 * TOP, (p[2] + 4.5f) / 9 * TOP, CHSV(wrap8(now / 40 + p[2] * 20), 220, highlight)); } // --- Leben 3D: Conways Spiel des Lebens im Würfel (Regel B5/S4567) -------------- static uint8_t life[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE], lifeOld[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE]; static void seedLife() { FOR_XYZ life[z][y][x] = random8() < 77; // 30 % memset(lifeOld, 0, sizeof(lifeOld)); } void renderLife3d(uint32_t now) { static uint32_t last = 0; static uint8_t gens = 0, hue = 0; if (fresh) { fresh = false; seedLife(); gens = 0; last = now; } if (now - last >= 450) { last = now; uint8_t next[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE]; uint16_t alive = 0; FOR_XYZ { uint8_t n = 0; for (int dz = -1; dz <= 1; dz++) for (int dy = -1; dy <= 1; dy++) for (int dx = -1; dx <= 1; dx++) if (dx || dy || dz) n += life[(z + dz + NUM_LAYERS) % NUM_LAYERS][(y + dy + CUBE_SIZE) % CUBE_SIZE][(x + dx + CUBE_SIZE) % CUBE_SIZE]; next[z][y][x] = life[z][y][x] ? n >= 4 && n <= 7 : n == 5; alive += next[z][y][x]; } bool same = !memcmp(next, life, sizeof(life)), blink = !memcmp(next, lifeOld, sizeof(life)); memcpy(lifeOld, life, sizeof(life)); memcpy(life, next, sizeof(life)); if (!alive || same || blink || ++gens > 60) { // ausgestorben oder festgefahren seedLife(); gens = 0; hue += 70; } } FOR_XYZ { CRGB &led = at(x, y, z); if (life[z][y][x]) led = CHSV(hue + z * 12, 220, lifeOld[z][y][x] ? min(255, brightness * 3) : highlight); // neu geboren: heller else led.nscale8(200); // gestorbene glimmen kurz nach } } // --- Zellautomat: eindimensionale Regeln 30, 90, 110 und 150 um den Rand herum --- // Jede Generation erscheint oben, ältere rutschen nach unten. void renderAutomaton(uint32_t now) { static const uint8_t RULES[] = {30, 90, 110, 150}; static uint16_t rows[NUM_LAYERS]; // Bit i = Zelle i auf dem Rand static uint32_t last = 0; static uint8_t gen = 0, rule = 0; if (fresh) { fresh = false; memset(rows, 0, sizeof(rows)); rows[TOP] = 1; gen = 0; last = now; } if (now - last >= 350) { last = now; uint16_t cur = rows[TOP], nxt = 0; for (uint8_t i = 0; i < 16; i++) { uint8_t l = cur >> ((i + 15) % 16) & 1, c = cur >> i & 1, r = cur >> ((i + 1) % 16) & 1; if (RULES[rule] >> (l << 2 | c << 1 | r) & 1) nxt |= 1 << i; } memmove(rows, rows + 1, sizeof(rows[0]) * TOP); rows[TOP] = nxt; if (++gen >= 24 || !nxt || nxt == 0xFFFF) { // nächste Regel, neu mit einer Zelle gen = 0; rule = (rule + 1) % 4; rows[TOP] |= 1 << random8(16); } } FastLED.clear(); for (uint8_t z = 0; z < NUM_LAYERS; z++) for (uint8_t i = 0; i < 16; i++) { if (!(rows[z] >> i & 1)) continue; uint8_t x, y; ringPos(i, x, y); at(x, y, z) = CHSV(rule * 64 + z * 10, 230, z == TOP ? highlight : min(255, brightness * 2)); } } // --- Hilbertkurve: ein Licht zeichnet die raumfüllende Kurve (4×4×4) ------------- // Index 0..63 -> Koordinaten 0..3 (Algorithmus von J. Skilling, 3 Achsen, 2 Bit) static void hilbertPoint(uint8_t h, uint8_t p[3]) { for (uint8_t i = 0; i < 3; i++) p[i] = 0; for (uint8_t j = 0; j < 2; j++) for (uint8_t i = 0; i < 3; i++) p[i] |= (h >> (j * 3 + 2 - i) & 1) << j; uint8_t t = p[2] >> 1; for (uint8_t i = 2; i > 0; i--) p[i] ^= p[i - 1]; p[0] ^= t; for (uint8_t q = 2; q != 4; q <<= 1) { uint8_t mask = q - 1; for (int8_t i = 2; i >= 0; i--) { if (p[i] & q) p[0] ^= mask; else { t = (p[0] ^ p[i]) & mask; p[0] ^= t; p[i] ^= t; } } } } void renderHilbert(uint32_t now) { const float S = TOP / 3.0f; // 0..3 auf 0..4 strecken float pos = ((now - fxStart) % 7000) / 7000.0f * 66; // 64 Punkte, dann kurz Pause FastLED.clear(); uint8_t a[3], b[3]; for (uint8_t h = 0; h < 64 && h <= pos; h++) { hilbertPoint(h, a); uint8_t v = scale8(highlight, h + 4 > pos ? 255 : 110); plotSoft(a[0] * S, a[1] * S, a[2] * S, CHSV(h * 4, 230, v)); } if (pos < 63) { // Kopf gleitet zum nächsten Punkt hilbertPoint(pos, a); hilbertPoint(pos + 1, b); float f = pos - int(pos); plotSoft((a[0] + (b[0] - a[0]) * f) * S, (a[1] + (b[1] - a[1]) * f) * S, (a[2] + (b[2] - a[2]) * f) * S, CRGB(highlight, highlight, highlight)); } } // --- Kristall: umherirrende Teilchen bleiben am Kristall hängen (DLA) ------------ static uint8_t crystal[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE]; // 0 = leer, sonst Reihenfolge void renderCrystal(uint32_t now) { static int8_t w[3]; static uint8_t count = 0; static uint32_t doneAt = 0; if (fresh || (doneAt && now - doneAt > 2500)) { fresh = false; memset(crystal, 0, sizeof(crystal)); crystal[0][2][2] = count = 1; // Keim unten in der Mitte doneAt = 0; w[0] = -1; } if (!doneAt) for (uint8_t step = 0; step < 6; step++) { if (w[0] < 0) { // neues Teilchen oben irgendwo w[0] = random8(CUBE_SIZE); w[1] = random8(CUBE_SIZE); w[2] = TOP; } static const int8_t DIRS[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}; bool stick = false; for (const auto &d : DIRS) { int x = w[0] + d[0], y = w[1] + d[1], z = w[2] + d[2]; if (inCube(x, y, z) && crystal[z][y][x]) stick = true; } if (stick && !crystal[w[2]][w[1]][w[0]]) { crystal[w[2]][w[1]][w[0]] = ++count; if (w[2] == TOP || count > 70) doneAt = now; w[0] = -1; break; } const int8_t *d = DIRS[random8() < 40 ? 5 : random8(6)]; // leicht nach unten int x = w[0] + d[0], y = w[1] + d[1], z = w[2] + d[2]; if (inCube(x, y, z) && !crystal[z][y][x]) { w[0] = x; w[1] = y; w[2] = z; } } FastLED.clear(); FOR_XYZ { uint8_t c = crystal[z][y][x]; if (!c) continue; uint8_t v = doneAt ? scale8(highlight, 150 + scale8(sin8(now / 3 + c * 20), 105)) : min(255, brightness * 2); at(x, y, z) = CHSV(140 + c * 2, 200, v); } if (w[0] >= 0 && !doneAt) at(w[0], w[1], w[2]) += CRGB(brightness / 2, brightness / 2, brightness / 2); } // --- Fraktalwolken: Rauschen über drei Größenordnungen, wie ein farbiger Nebel ---- DEFINE_GRADIENT_PALETTE(nebulaColors){ 0, 0, 0, 0, // Lücken zwischen den Wolken 60, 20, 0, 90, // tiefviolett 110, 0, 60, 170, // blau 150, 140, 0, 170, // violett 195, 255, 30, 80, // pink 230, 255, 130, 0, // orange 255, 255, 220, 120, // gold }; void renderFractalClouds(uint32_t now) { static const CRGBPalette16 palette = nebulaColors; static float lo, hi; // Wertebereich der letzten Bilder, für vollen Kontrast uint32_t t = now - fxStart; uint8_t n[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE]; uint8_t mn = 255, mx = 0; FOR_XYZ { uint8_t v = inoise8(x * 60, y * 60 + t / 12, z * 60) / 2 + inoise8(x * 130 + t / 6, y * 130, z * 130) / 4 + inoise8(x * 260, y * 260, z * 260 + t / 3) / 4; n[z][y][x] = v; mn = min(mn, v); mx = max(mx, v); } if (fresh) { fresh = false; lo = mn; hi = mx; } lo += (mn - lo) * 0.05f; // langsam nachführen, damit nichts flackert hi += (mx - hi) * 0.05f; float scale = 255 / max(hi - lo, 24.0f); FOR_XYZ { uint8_t index = uint8_t(constrain((n[z][y][x] - lo) * scale, 0.0f, 255.0f)); at(x, y, z) = ColorFromPalette(palette, index, min(255, brightness * 3)); } }