% --- Create figure with 1x2 subplots ---
figure('Position', [100, 100, 1200, 600]);  % wide window
tiledlayout(1, 2, 'TileSpacing', 'compact', 'Padding', 'compact');

% ===================================================================
% LEFT SUBPLOT: 3D Torus (Script 1)
% ===================================================================
ax1 = nexttile;
hold on; axis equal; grid on; view(3);

% ---- Initialize the Torus ------------------------------------------------
R = 6; a = 2;
num_pts = 100;
u = linspace(0, 2*pi, num_pts);
v = linspace(0, 2*pi, num_pts);
[u_grid, v_grid] = meshgrid(u, v);

x_grid = (R + a * cos(v_grid)) .* cos(u_grid);
y_grid = (R + a * cos(v_grid)) .* sin(u_grid);
z_grid = a * sin(v_grid);

% White surface
torus = surf(x_grid, y_grid, z_grid, 'EdgeColor', 'none', 'FaceColor', 'w');

% ---- Shading -------------------------------------------------------------
rho = sqrt(x_grid.^2 + y_grid.^2);
inner_mask      = (rho > 0) & (rho < (R - a));
outer_band_mask = (rho > (R - a)) & (rho <= R);

C = zeros(size(x_grid), 'uint8');
C(inner_mask) = 1;
C(outer_band_mask) = 2;

hGreen = surf(x_grid, y_grid, z_grid, C, 'FaceColor','flat','EdgeColor','none','FaceAlpha',0.75);
hBlue  = surf(x_grid, y_grid, z_grid, C, 'FaceColor','flat','EdgeColor','none','FaceAlpha',0.75);

colormap(ax1, [0 0.5 1; 0 0.8 0]);  % blue = 1, green = 2
hGreen.CDataMapping = 'direct';
hBlue.CDataMapping  = 'direct';

% ---- Black latitudes (meridians) -----------------------------------------
skip = 10;
idx = 1:skip:size(x_grid, 1);
for i = idx
    plot3(x_grid(i,:), y_grid(i,:), z_grid(i,:), 'k', 'LineWidth', 3);
end

% ---- Black longitudes (dashed) -------------------------------------------
m = 10;
u0 = linspace(0, 2*pi, m+1); u0(end) = [];
for k = 1:m
    vv = linspace(0, 2*pi, 300);
    x_lon = (R + a*cos(vv)) * cos(u0(k));
    y_lon = (R + a*cos(vv)) * sin(u0(k));
    z_lon = a*sin(vv);
    plot3(x_lon, y_lon, z_lon, '--k', 'LineWidth', 3);
end

% ---- Red top circle ------------------------------------------------------
u_circle = linspace(0, 2*pi, 200);
plot3(R*cos(u_circle), R*sin(u_circle), a*ones(size(u_circle)), 'r', 'LineWidth', 3);

% ---- Title & Labels ------------------------------------------------------
title('3D Torus Plot (e.g., R = 6, a = 2)', 'FontSize', 24, 'FontWeight', 'bold');
xlabel('x_1'); ylabel('x_2'); zlabel('x_3');
set(ax1, 'FontSize', 16, 'FontWeight', 'bold');

% ---- Legend (left side) --------------------------------------------------
hGreenBox = patch(NaN, NaN, [0 0.8 0], 'FaceAlpha', 0.75, 'EdgeColor', 'none');
hBlueBox  = patch(NaN, NaN, [0 0.5 1], 'FaceAlpha', 0.75, 'EdgeColor', 'none');
hRedLine  = plot(NaN, NaN, 'r', 'LineWidth', 3);
hBlackLine = plot(NaN, NaN, 'k', 'LineWidth', 3);
hDashedLine = plot(NaN, NaN, '--k', 'LineWidth', 3);
hWhiteBox = patch(NaN, NaN, [1 1 1], 'FaceAlpha', 0.75, 'EdgeColor', 'none');

legend(ax1, [hGreenBox, hBlueBox, hRedLine, hBlackLine, hDashedLine, hWhiteBox], ...
    'Hyperbolic points (K < 0)', ...
    'Elliptic points (K > 0)', ...
    'Parabolic points (K = 0)', ...
    'Lines of curvature (constant K)', ...
    'Lines of curvature (varying K)', ...
    'Note: u is radial w.r.t the x_1-x_2 plane', ...
    'Location', 'northeastoutside', 'FontSize', 24);

set([hGreenBox, hBlueBox, hRedLine, hBlackLine, hDashedLine, hWhiteBox], 'HandleVisibility', 'off');

% ===================================================================
% RIGHT SUBPLOT: 2D Cross-Section
% ===================================================================
ax2 = nexttile;
hold on; axis equal; grid on;

xlim([0 10]); ylim([-3 3]);

% --- Plot the circle ---
theta = linspace(0, 2*pi, 200);
plot(R + a*cos(theta), a*sin(theta), 'k', 'LineWidth', 2);

% --- Center point ---
plot(R, 0, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 6);

% --- Label 'R' ---
plot([0, R], [0, 0], 'k--', 'LineWidth', 1.2);
text(R/2, 0.4, 'R', 'FontSize', 16, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');

% --- Point P and radius line ---
v_deg = 40; v = v_deg*pi/180;
P = [R + a*cos(v), a*sin(v)];
plot(P(1), P(2), 'ko', 'MarkerSize', 6);
plot([R, P(1)], [0, P(2)], 'k-', 'LineWidth', 1.5);

% --- Label 'a' outside ---
dir = (P - [R, 0]) / norm(P - [R, 0]);
label_pos = P + 0.3 * dir;
text(label_pos(1), label_pos(2), 'a', 'FontSize', 16, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');

% --- Arc for v ---
arc_radius = 0.7;
arc_angles = linspace(0, v, 40);
arc_x = R + arc_radius * cos(arc_angles);
arc_y = 0 + arc_radius * sin(arc_angles);
plot(arc_x, arc_y, 'k', 'LineWidth', 1.5);

% --- Label 'v' near the middle of the arc ---
mid_angle = v / 2;
label_radius = 1;  % < arc_radius
cx = R;  % = 6
cy = 0;
label_x = cx + label_radius * cos(mid_angle);
label_y = cy + label_radius * sin(mid_angle);

text(label_x, label_y, 'v', ...
    'FontSize', 16, 'FontWeight', 'bold', ...
    'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');

% --- Title & Labels ---
title('Torus Cross-Section', 'FontSize', 32, 'FontWeight', 'bold');
xlabel('x_1'); ylabel('x_3');
set(ax2, 'FontSize', 16, 'FontWeight', 'bold');

% --- Final layout ---
%sgtitle({'Problem 6 Torus Plots:', '3D View and Cross-Section'}, 'FontSize', 32, 'FontWeight', 'bold');

%f = gcf;
%f = gcf; exportgraphics(f, 'problem6TorusPlots.pdf');