# Help me to solve how to draw mandelbrot.

Hello people, I have bought a 480x360 tft screen and I am using the mcufriend libaries. I am trying to draw the mandelbrot fractal and found a nice website here, Coding the Mandelbrot Set - Comfortably Numbered
He is using a program called Scratch which is very easy to understand code. I ported it to the arduino ide and I am not getting the same output. The problem is the fractal is blocky but similar to a heavy pixelated mandelbrot. You can see his full code on the link at the bottom (and click on see inside project).
My code is here,

``````void loop()
{
uint16_t xpos, ypos;  //screen coordinates
tp = ts.getPoint();   //tp.x, tp.y are ADC values
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
int x = -240;
int y = 180;
for(int i = 0; i < 360; i++){
y--;
x = -240;
for(int j = 0; j < 480; j++){
x++;
int cR = x/20;
int cX = y/20;
int zR = 0;
int zX = 0;
int r = 0;
for(;;){
if(r > 50 || zR > 2){break;}
r++;
int old_zR = zR;
zR = cR+((zR*zR)-(zX*zX));
zX = (2*old_zR*zX)+cX;
}
if (zR < 2){
xpos = map(x, -240, 240, 0, tft.width());
ypos = map(y, -180, 180, 0, tft.height());
tft.drawPixel(xpos, ypos, BLACK);
}
else{
xpos = map(x, -240, 240, 0, tft.width());
ypos = map(y, -180, 180, 0, tft.height());
int color = map(r, 1, 50, 1, 7);
switch(r){
case 1:
tft.drawPixel(xpos, ypos, BLUE);
break;
case 2:
tft.drawPixel(xpos, ypos, RED);
break;
case 3:
tft.drawPixel(xpos, ypos, GREEN);
break;
case 4:
tft.drawPixel(xpos, ypos, CYAN);
break;
case 5:
tft.drawPixel(xpos, ypos, MAGENTA);
break;
case 6:
tft.drawPixel(xpos, ypos, YELLOW);
break;
case 7:
tft.drawPixel(xpos, ypos, WHITE);
break;

}

}
}
}
}
}
``````

I did a similar project using an ESP32 to increase the speed. See here (sorry, it’s in French, you may have to translate it)

Can you see what’s not good in my code? This is the result I get,

Another picture at -20x zoom.

``````           int cR = x/20;
int cX = y/20;
``````

This looks suspicious to me. Why are you doing an integer divide on the coordinates? I think this might explain why your first picture shows what appears be 20x20 blocks (24 across, 18 vertical)

Hi john, the division is the magnification of the fractal. Although the result is different, it is still not a fractal. I have included the image I get. The division has been removed.

I think your problem is that you are using integers where the original program probably uses 'float' type for everything. Maybe this will help:

``````        float cR = x / 120.0;
float cX = y / 120.0;
float zR = 0;
float zX = 0;
int r = 0;
``````

and:

``````          float old_zR = zR;
``````

johnwasser:
I think your problem is that you are using integers where the original program probably uses ‘float’ type for everything. Maybe this will help:

``````        float cR = x / 120.0;
``````

float cX = y / 120.0;
float zR = 0;
float zX = 0;
int r = 0;

That’s brilliant, it’s working! Thank you.
I simplified the code too.

``````void loop()
{
tp = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
for(int y = 170; y > -170; y--){
for(int x = -240; x < 240; x++){
float cR = x/120.0;
float cX = y/120.0;
float zR = 0;
float zX = 0;
int r = 0;
do{
r++;
float old_zR = zR;
zR = cR+((zR*zR)-(zX*zX));
zX = (2*old_zR*zX)+cX;
} while(!(r > 50 || zR > 2));
if (zR < 2){
tft.drawPixel(x+240, y+170, BLACK);
}
else{
tft.drawPixel(x+240, y+170, r*500);
}
}
}
} }
``````

If the colors are 24-bit I would try (r * 335544UL) for the pixel color. That will extend the color palette into the reds and not just blue and some green. The number 16,777,215 is the largest number you can put in 24 bits. Divide that by 50 and you get 335,544.