% Cylindrical Coordinates
% 2021/11/22 by T. Hirano
clear; clc;
%
fig = figure;
hold on;
xmin=-2; xmax=2;
ymin=-2; ymax=2;
zmin=-2; zmax=2;
axis([xmin xmax ymin ymax zmin zmax]);
pbaspect([1 1 1]);
axis off;
view(60,20);
camzoom(1.2);
% Draw axes
quiver3(-2,0,0,4,0,0,0,'k','filled'); % x-axis
quiver3(0,-2,0,0,4,0,0,'k','filled'); % y-axis
quiver3(0,0,-2,0,0,4,0,'k','filled'); % z-axis
text(0,0,0,'O');
text(xmax,0,0,'x');
text(0,ymax,0,'y');
text(0,0,zmax,'z');
%
x = 1;
y = 0.5;
z = 1.5;
rho=sqrt(x^2+y^2);
phi=atan2(y,x);
quiver3(0,0,0,x,y,z,0,'r','filled'); % Position Vector
% Unit vectors
quiver3(x,y,z,(rho/3)*cos(phi),(rho/3)*sin(phi),0,0,'b','filled'); % rho
quiver3(x,y,z,-(rho/3)*sin(phi),(rho/3)*cos(phi),0,0,'b','filled'); % phi
quiver3(x,y,z,0,0,rho/3,0,'b','filled'); % z
text(x+(rho/3)*cos(phi),y+(rho/3)*sin(phi),z,'rho\^');
text(x-(rho/3)*sin(phi),y+(rho/3)*cos(phi),z,'phi\^');
text(x,y,z+rho/3,'z\^');
% Aux. lines
l1_1=line([0 0 x x x 0 0 x x],[y 0 0 0 y y y y 0],[z z z 0 0 0 z z z]);
l1_2=line([0 x x],[0 y y],[0 0 z]);
l1_1.LineStyle = '--';
l1_2.LineStyle = '--';
l1_1.Color = [0.5 0.5 0.5];
l1_2.Color = [0.5 0.5 0.5];
% Draw cylinder
nl=12;
for i=1:nl
    p=(i-1)*(2*pi)/nl;
    l2(i)=line(rho.*[cos(p) cos(p)],rho.*[sin(p) sin(p)],[-2 2]);
    l2(i).LineStyle = '--';
    l2(i).Color = [0.5 0.5 0.5];
end
nc=4; np=32;
for i=1:nc+1
    zc=(i-1)*(4/nc)-2;
    p=0:2*pi/np:2*pi;
    l3(i)=line(rho.*cos(p),rho.*sin(p),zc*ones([1 length(p)]));
    l3(i).LineStyle = '--';
    l3(i).Color = [0.5 0.5 0.5];
end
% Arc for angle
na=24;
for i=1:na
    p=0:phi/na:phi;
    l4(i)=line((rho/3).*cos(p),(rho/3).*sin(p),zeros([1 length(p)]));
    l4(i).Color = 'm';
end