% make_cylinder_general.m

clear; close all;

% Reference position
r = [1; 1; 1];

% Reference orientation
R = eulerXYZ(-pi/3, 0, 0);

% Cylinder spec
Radius = 0.1;
Height = 0.3;
SideCount = 20;

% Vertices
vertices_0 = zeros(2*SideCount, 3);
for i = 1:SideCount
    theta = 2*pi/SideCount*(i-1);
    vertices_0(i,:) = [Radius*cos(theta), Radius*sin(theta), 0];
    vertices_0(SideCount+i,:) = [Radius*cos(theta), Radius*sin(theta), Height];
end

vertices = r' + vertices_0*R';

% Side faces
sideFaces = zeros(SideCount, 4);
for i = 1:(SideCount-1)
    sideFaces(i,:) = [i, i+1, SideCount+i+1, SideCount+i];
end
sideFaces(SideCount,:) = [SideCount, 1, SideCount+1, 2*SideCount];

% Bottom faces
bottomFaces = [
    1:SideCount;
    (SideCount+1):2*SideCount];

% Draw patches
figure(1);
h_side = patch('Faces', sideFaces, 'Vertices', vertices, 'FaceColor', 'y');
h_bottom = patch('Faces', bottomFaces, 'Vertices', vertices, 'FaceColor', 'y');

% Axes settings
xlabel('x'); ylabel('y'); zlabel('z');
axis vis3d equal;
view([-37.5, 30]);
camlight;
grid on;
xlim([0.8, 1.3]);
ylim([0.9, 1.4]);
zlim([0.8, 1.3]);