// ---------- 5 fractals ----------
#define FR_CELL 4 // 4x4 px per cell -> 80x60 grid
#define FR_COLS 80
#define FR_ROWS 60
uint16_t fractalColor(int n, int maxN) {
if (n >= maxN) return TFT_BLACK;
uint8_t r = (n * 7) & 255;
uint8_t g = (n * 3 + 60) & 255;
uint8_t b = (n * 11 + 120) & 255;
return tft.color565(r, g, b);
}
int escapeMandel(float cr, float ci, int maxIt) {
float zr = 0, zi = 0;
for (int i = 0; i < maxIt; i++) {
float zr2 = zr * zr, zi2 = zi * zi;
if (zr2 + zi2 > 4.0f) return i;
zi = 2 * zr * zi + ci;
zr = zr2 - zi2 + cr;
}
return maxIt;
}
int escapeJulia(float zr, float zi, float cr, float ci, int maxIt) {
for (int i = 0; i < maxIt; i++) {
float zr2 = zr * zr, zi2 = zi * zi;
if (zr2 + zi2 > 4.0f) return i;
zi = 2 * zr * zi + ci;
zr = zr2 - zi2 + cr;
}
return maxIt;
}
int escapeShip(float cr, float ci, int maxIt) {
float zr = 0, zi = 0;
for (int i = 0; i < maxIt; i++) {
float ar = fabs(zr), ai = fabs(zi);
if (ar * ar + ai * ai > 4.0f) return i;
zi = 2 * ar * ai + ci;
zr = ar * ar - ai * ai + cr;
}
return maxIt;
}
// 18: Mandelbrot zoom
void drawMandelZoom(float tt) {
const float cx = -0.743643f, cy = 0.131826f;
float sc = 3.0f * expf(-tt * 0.35f);
if (sc < 0.0005f) sc = 3.0f; // loop the zoom
for (int gy = 0; gy < FR_ROWS; gy++)
for (int gx = 0; gx < FR_COLS; gx++) {
float cr = cx + (gx - FR_COLS / 2) * sc / FR_COLS;
float ci = cy + (gy - FR_ROWS / 2) * sc / FR_COLS;
int n = escapeMandel(cr, ci, 32);
tft.fillRect(gx * FR_CELL, gy * FR_CELL, FR_CELL, FR_CELL, fractalColor(n, 32));
}
setLed(false);
}
// 19: Julia morph
void drawJuliaMorph(float tt) {
float a = tt * 0.35f;
float cr = 0.7885f * cos(a), ci = 0.7885f * sin(a);
for (int gy = 0; gy < FR_ROWS; gy++)
for (int gx = 0; gx < FR_COLS; gx++) {
float zr = (gx - FR_COLS / 2) * 3.0f / FR_COLS;
float zi = (gy - FR_ROWS / 2) * 3.0f / FR_COLS;
int n = escapeJulia(zr, zi, cr, ci, 28);
tft.fillRect(gx * FR_CELL, gy * FR_CELL, FR_CELL, FR_CELL, fractalColor(n + 40, 68));
}
setLed(false);
}
// 20: Burning Ship zoom
void drawBurningShip(float tt) {
const float cx = -1.7557f, cy = -0.0278f;
float sc = 2.2f * expf(-tt * 0.2f);
if (sc < 0.002f) sc = 2.2f;
for (int gy = 0; gy < FR_ROWS; gy++)
for (int gx = 0; gx < FR_COLS; gx++) {
float cr = cx + (gx - FR_COLS / 2) * sc / FR_COLS;
float ci = cy + (gy - FR_ROWS / 2) * sc / FR_COLS;
int n = escapeShip(cr, ci, 32);
tft.fillRect(gx * FR_CELL, gy * FR_CELL, FR_CELL, FR_CELL, fractalColor(n * 2, 64));
}
setLed(false);
}
// 21: Sierpinski triangle (chaos game, builds up over time)
void drawSierpinski(float tt) {
static const float V[3][2] = {{0.5f, 0.02f}, {0.02f, 0.98f}, {0.98f, 0.98f}};
static float px = 0.5f, py = 0.5f;
static uint32_t count = 0;
if (count == 0) tft.fillScreen(TFT_BLACK);
for (int i = 0; i < 400; i++) {
int k = random(3);
px = (px + V[k][0]) / 2;
py = (py + V[k][1]) / 2;
tft.drawPixel((int)(px * 319), (int)(py * 239), tft.color565(230, 180, 40));
}
count += 400;
if (count > 30000) count = 0; // restart the picture
setLed(false);
}
// 22: Barnsley fern (chaos game, grows over time)
void drawFern(float tt) {
static float x = 0, y = 0;
static uint32_t count = 0;
if (count == 0) tft.fillScreen(TFT_BLACK);
for (int i = 0; i < 400; i++) {
float u = random(10000) / 10000.0f;
float nx, ny;
if (u < 0.01f) { nx = 0; ny = 0.16f * y; }
else if (u < 0.86f) { nx = 0.85f * x + 0.04f * y; ny = -0.04f * x + 0.85f * y + 1.6f; }
else if (u < 0.93f) { nx = 0.20f * x - 0.26f * y; ny = 0.23f * x + 0.22f * y + 1.6f; }
else { nx = -0.15f * x + 0.28f * y; ny = 0.26f * x + 0.24f * y + 0.44f; }
x = nx; y = ny;
int px = 160 + (int)(x * 40);
int py = 230 - (int)(y * 22);
if (px >= 0 && px < 320 && py >= 0 && py < 240)
tft.drawPixel(px, py, tft.color565(40, 220, 90));
}
count += 400;
if (count > 40000) count = 0;
setLed(false);
}Loading
esp32-2432s028r
esp32-2432s028r