%Burning Ship Fractal

clf
clear

xmin = -2;
xmax = 1.5;
ymin = -2;
ymax = 2;

%number of points = resolution in x and y direction
nPoints = 600;

%upper limit for |z|, consider series has diverged if exceeded
zmax = 1e6;


for k=1:nPoints %Changed to k and l because i is used as the imaginary part
    for l=1:nPoints 

        %%%% map the index 'i' onto the interval [xmin,xmax]
        %%%% i=1       must yield 'xmin'
        %%%% i=nPoints must yield 'xmax'
        x(k) = xmin+(k-1)/(nPoints-1)*(xmax-xmin);

        %%%% map the index 'j' onto the interval [ymin,ymax]
        %%%% j=1       must yield 'ymin'
        %%%% j=nPoints must yield 'ymax'
        y(l) = ymin+(l-1)/(nPoints-1)*(ymax-ymin);

        c = complex(x(k),y(l)); % this converts (x,y) to a complex number 'c'
        z = complex(0.0,0.0);   % iteration starts with complex number = 0
        
        it_max = 310;            % no more than 50 iterations please
        for n = 1:it_max
            %%%% add the iteration formula for the Mandelbrot set
            z = (abs(real(z))+1i*(abs(imag(z))))^2+c;
            if (abs(z)>=zmax);
                break
            end
        end
        
            
        if (abs(z)<=zmax)
            ZZ(k,l)=0;% make it '0' for blue if |z| is small and series has not diverged
        else ZZ(k,l)=n; % make it '1' for red  if |z| is large and series has diverged
        end
    end

    fprintf('i= %d of %d points in X direction: x= %f\n',k,nPoints,x(k));
end


m=max(max(ZZ));
for i=1:300
    n1=2*i;
    n2=n1+10;
    ZZ=mod(ZZ+1,m);
   set(gca,'YDir','normal');
    imagesc(imrotate(ZZ,-90),[n1 n2]);
    pause(0.27);

end


axis equal
set(gca,'YDir','normal');
imagesc(imrotate(ZZ,-90)); %to rotate the image by 90 degrees, so it looks more like a ship