Thread: ugly mandelbrot set :(

1. ugly mandelbrot set :(

hi i am a novice to c++ and have been doodling with borland graphics interface with dev 4.9.9.1
below is a programme i have written which calculates the mandelbrot set (all complex number c such that |z(k)| remained bounded subject to endless iteration z(n+1)=(z(n))^2+c, z(0)=c)).

Code:
```#include <iostream.h>
#include <stdlib.h>
#include <winbgim.h>

int main(){
initwindow(800,600);

for(int x=0; x<=800; x++)
{
for(int y=0; y<=600; y++)
{

long double x_re = -2.0 + x*0.005;
long double y_im = 1.5 - y*0.005;
long double x_0 = x_re, y_0 = y_im;
short terminating=0, i=0;

do{
x_0=(x_0*x_0-y_0*y_0+x_re);
y_0=(2.0*x_0*y_0+y_im);
terminating++;

if(x_0*x_0+y_0*y_0>4.0){
i=1; break;
}

}while(terminating<1000);

if(i) continue;
else putpixel(x,y,1);

}
}
while(!kbhit());
closegraph();
return 0;
}```
the problem with it is that when the graphics is generated, it looks like the mandelbrot set, but a grossly inaccurate representation. my guess is the limited number of significant figures c++ truncates for each calculation. any idea on increasing the number of significant figures? (something like what Window's calculator is capable of) thanks 4 the assistance!

2. if(i) continue;
else putpixel(x,y,1);

1.
Code:
`if ( !i ) putpixel(x,y,1);`
2. Shouldn't you be choosing a colour based on the final value?
Because it looks like your code produces something monochrome.

3. re: ugly mandelbrot

ok i've uploaded a picture of the gross representation of the image (actual size). have used 64 shades of grey, the whiter the pixel the closer it is to the set (i.e it doesnt diverge that fast). as u can see, the edges are spiking away, which they shouldnt be. any way to increase the precision of the calculation?

(an accurate mandelbrot image is provided as reference)

4. Originally Posted by Horse22
x_0=(x_0*x_0-y_0*y_0+x_re);
y_0=(2.0*x_0*y_0+y_im);
Here's your problem. The formulas are correct, but you cannot use the updated version of x_0 when you're calculating y_0. You must use the same value of x_0 when updating the real and imaginary parts.

Here' s a solution:
Code:
```double x_0_temp = x_0;
x_0=(x_0*x_0-y_0*y_0+x_re);
y_0=(2.0*x_0_temp*y_0+y_im);```
Now the image should look like the real Mandelbrot set.

Perhaps you should consider using the complex class in the STL. That way you'll avoid low-level bugs like this.

Popular pages Recent additions