when i use the following algorithm for using the mandelbrot algorithm i get the a image similar to the image i want but it looks more like an outline of the final image and when i use the algorithm with complex numbers it gives a proper sharp image
the code is written in c++
//pseudocode
//z=x+yi //x=0,y=0
//c=x0+y0i //x=scaled(px),y=scaled(py)
//do
//z2=x2+(2*xy)i-y2
//z=z2+c //z=x2-y2+x0+(2*xy+y0)i
//x=x2-y2+x0 //y=2*xy+y0
//x2=x*x //y2=y*y
//if(sqrt(x2+y2)<2 )
// break
//iteration++
//while( iteration<max_iteration)
//code without complex numbers
int mandelbrot::_test_num_iteration( double px, double py ) {
double x0{ scale_x( px ) }, x{ 0 };
double y0{ scale_y( py ) }, y{ 0 };
double x2{ 0 }, y2{ 0 };
//z=x+iy c=x0+iy0
int iteration{ 0 };
while (iteration < max_iteration) { //for instance max_iteration=1000
x2 = x * x; y2 = y * y;
x = x2 + x0 - y2; y = 2 * x * y + y0;
x2 = (x * x); y2 = (y * y);
if (sqrt(( x2 + y2 )) > 2)
break;
iteration++;
}
return iteration;
}
//code with complex numbers
int mandelbrot::get_iteration( double px, double py) {
//x+iy=z
//z2=x2+(2*xyi)-y2
//c=x0+iy0
//x0&y0=scaled x and y coordinates
double x{ scale_x( px ) };
double y{ scale_y( py ) };
complex::complex_numbers z;
complex::complex_numbers c{ x,y };
int iteration{ 0 };
while (iteration < max_iteration) {
z = z * z + c;
if (mod( z ) > 2) //mod is a function which returns sqrt(x2+y2)
break;
iteration++;
}
return iteration;
}
The problem is that you're not calculating both new values from the old values.
Here,
x = x2 + x0 - y2; y = 2 * x * y + y0;
you're using the new x
for the new y
.
You need a couple of variables for temporary storage of the updated values.
double new_x = x2 - y2 + x0;
double new_y = 2 * x * y + y0;
x = new_x;
y = new_y;