% Julia set
% f(z) = z^2 + c, where z and c are complex. z is the position on the
% complex plane.

close all

%c = -0.8i;
%c = 0.285 + 0.01i;
%c =  -0.4 + 0.6i
%c = 0.392 + 0.258 * i

a = linspace(0, 2*pi, 100);
c = 0.7885.*exp(i*a); 

%phi = ( 1 + sqrt(5) ) / 2; % Golden ratio!
%c = 1-phi;

n = 200; % Square dimensions of each frame.
pixelgrid = zeros(n);

xpixels = linspace(-2,2,n);
ypixels = linspace(-2,2,n);
frame = 1;
for c = c
xpos = 1;
ypos = 1;
for zx = xpixels
    
    ypos = 1;
    for zy = ypixels
        

        z = zx + zy*1i; % Make complex
        iterations = 0;
        max_iter = 800;
        while (abs(z) < 2 && iterations < max_iter)
            z = z^2 + c;
            iterations = iterations + 1;
        end
        if iterations == max_iter
            pixelgrid(xpos, ypos) = 0;
        else
            pixelgrid(xpos, ypos) = iterations;
        end
        ypos = ypos + 1;
    end
    xpos = xpos + 1;
end


% Find largest element in the array with this jank
%M = max(pixelgrid, [], 'all');
% Normalize to [0,1]
%bitmap_pixelgrid = pixelgrid./100;
%imshow(pixelgrid, jet(100)) % jet is the colormap, jet(40) is a decimated version ie. several values map to a single color.
stringname = strcat("julia",sprintf("%04i", frame), ".bmp")
cmap = gray(100);
imwrite(pixelgrid, cmap, stringname);
frame = frame + 1;
end

