% Plotting complex phase using HSV and |z| height
clear
% Enter function
%fn = @(x) x;
fn = @(x) x.^4;
%fn = @(x) exp(x);
%fn = @(x) 1./(x.*(1/2-x).*(x-i).*(x-i+1));
%fn = @(x) sqrt(x+1);
%fn = @(x) x.^(1/2);
%fn = @(x) sin(x);
%fn = @(x) log(x);
%fn = @(x) sin(1./x);
%fn = @(x) x.^2-0.75-0.2*i;
%fn = @(x) (1-i/2)*sin(x);
iter=1;
xmin=-2;
xmax=2;
ymin=-2;
ymax=2;
Nx=500;
Ny=500;
x=linspace(xmin,xmax,Nx);
y=linspace(ymin,ymax,Ny);
[X,Y] = meshgrid(x,y);
z = complex(X, Y);
tmp=z;
for n=1:1
    tmp = fn(tmp);
end
Z=tmp;
XX=real(Z);
YY=imag(Z);
R2=max(max(X.^2));
R=max(max(XX.^2+YY.^2));

circle(:,:,1) = X.^2+Y.^2 < R2;
circle(:,:,2)=circle(:,:,1);
circle(:,:,3)=circle(:,:,1);

addcirc(:,:,1)=circle(:,:,1)==0;
addcirc(:,:,2)=circle(:,:,1)==0;
addcirc(:,:,3)=circle(:,:,1)==0;

warning off MATLAB:divideByZero;
hsvCircle=ones(Nx,Ny,3);
hsvCircle(:,:,1)=atan2(YY,XX)*180/pi+(atan2(YY,XX)*180/pi<0)*360; %[0,360]
hsvCircle(:,:,1)=hsvCircle(:,:,1)/360;
%r=log(1+sqrt(XX.^2+YY.^2));
%hsvCircle(:,:,2)=(1+sin(pi*r))/2;
%hsvCircle(:,:,3)=(1+cos(pi*r))/2;

lgz=log(XX.^2+YY.^2)/2;
hsvCircle(:,:,2)=0.75;
hsvCircle(:,:,3)=1-(lgz-floor(lgz))/2;
%hsvCircle(:,:,3)=(lgz-floor(lgz));

hsvCircle(:,:,1) = flipud((hsvCircle(:,:,1)));
hsvCircle(:,:,2) = flipud((hsvCircle(:,:,2)));
hsvCircle(:,:,3) = flipud((hsvCircle(:,:,3)));

rgbCircle=hsv2rgb(hsvCircle);
rgbCircle=rgbCircle.*circle+addcirc;
%rgbCircle=rgbCircle.*circle;
%rgbCircle=uint8(rgbCircle*255);

image(rgbCircle)
axis square
set(gca,'XTickLabel',{})
set(gca,'YTickLabel',{})