#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
float cx, cy, zx, zy, wx, wy;
uint8_t it;
const uint8_t MAX_ITER = 16;
// Mandelbrot scaling constants
const float SCALE_X = 120.0;
const float SCALE_Y = 120.0;
const float OFFSET_X = 2.0;
const float OFFSET_Y = 1.0;
void setup()
{
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
}
void loop()
{
for (uint8_t py = 0; py < 240; py++)
{
for (uint16_t px = 0; px < 320; px++)
{
cx = (px / SCALE_X) - OFFSET_X; // Scale to fit the Mandelbrot set
cy = (py / SCALE_Y) - OFFSET_Y; // Scale to fit the Mandelbrot set
zx = 0.0;
zy = 0.0;
wx = 0.0;
wy = 0.0;
it = 0;
while (wx + wy <= 4.0 && it < MAX_ITER)
{
zy = 2.0 * zx * zy + cy;
zx = wx - wy + cx;
wx = zx * zx;
wy = zy * zy;
it++;
}
if (it == MAX_ITER)
{
// tft.drawPixel(px, py, random(0xffff)); // Point in the Mandelbrot set
}
else
{
// Create a color gradient based on 'it'
uint16_t color = tft.color565(it * 16, 0, 255 - it * 16);
tft.drawPixel(px, py, color); // Point outside the Mandelbrot set
}
}
}
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.println("Mandelbrot Set");
while (1)
; // Stop the loop
}
/*
20 for py=0 to 23
30 for px=0 to 38
40 cx = px/10 - 2
50 cy = py/10 - 1
60 zx = 0: zy = 0: it = 0
70 wx = zx*zx: wy = zy*zy
80 if wx+wy>4 or it=16 then goto 120
90 zy = 2*zx*zy + cy
100 zx = wx - wy + cx
110 it = it + 1: goto 70
120 if it=16 then print " ";:goto 140
130 print "*";
140 next px
150 print
160 next py
*/