%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Michael Pokojovy                             % 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1);

set(gcf, 'PaperUnits', 'centimeters');
xSize = 16; ySize = 10;
xLeft = (21 - xSize)/2; yTop = (30 - ySize)/2;
set(gcf,'PaperPosition', [xLeft yTop xSize ySize]);
set(gcf,'Position',[0 0 xSize*50 ySize*50]);

hold on;

N = 30;

r = 3;
R = 8;

phi   = linspace(0, 2*pi, N);
theta = linspace(0, 2*pi, N);

X = zeros(N, N);
Y = zeros(N, N);
Z = zeros(N, N);

for i = 1:N
for j = 1:N
    X(i, j) = (R + r*cos(theta(j)))*cos(phi(i));
    Y(i, j) = (R + r*cos(theta(j)))*sin(phi(i));
    Z(i, j) = r*sin(theta(j));
end
end

surface(X, Y, Z);
colormap gray;
view([78 60]);

X = (R + r*cos(theta))*cos(0);
Y = (R + r*cos(theta))*sin(0);
Z = r*sin(theta);
plot3(X, Y, Z, 'b', 'LineWidth', 2);

X = (R + r*cos(0.1))*cos(phi);
Y = (R + r*cos(0.1))*sin(phi);
Z = r*ones(size(phi))*sin(0.1);
plot3(X, Y, Z, 'r', 'LineWidth', 2);

xlabel('x');
ylabel('y');
zlabel('z');

t = title(['Torus: $(\sqrt{x^2 + y^2} - R)^2 + z^2 = r^2$, $r = 3$, $R = 8$'], 'interpreter', 'latex');
set(t, 'FontSize', 16);

t = legend('$T \backslash (\Gamma_{1} \cup \Gamma_{2})$', '$\Gamma_{1}$', '$\Gamma_{2}$');
set(t, 'interpreter', 'latex');