MATLAB: Patch lateral surface of a cylinder

cylinderellipsepatchpolygon

Hello MATLAB Community!
I'm having some difficulties in rendering a cylinder. I only want to visualize the lateral surface and since the cylinder is slighty asymmetric I can't apply the standard cylinder function and therefore follow an elliptical approach. There must be a mistake in the line where the patch-function is applied but I can't figure it out. I was trying to define a polygon by tracing both circles. Thank you in advance!
%% Some random parameters
a_fit=1000;
b_fit=1010;
%% create samples and apply to the ellipcital equation
samples=1000;
samp_theta=linspace(0,2*pi,samples);
samp_r=sqrt(1./((cos(samp_theta')).^2./a_fit.^2+(sin(samp_theta')).^2./b_fit.^2));
[samp_X,samp_Y] = pol2cart(samp_theta',samp_r);
samp_Z_1=repmat(-3,samples,1);
samp_Z_2=repmat(3,samples,1);
%% visualize
figure(1);
hold on
plot3(samp_X,samp_Y,samp_Z_1,'Color',[0.8500, 0.3250, 0.0980]);
plot3(samp_X,samp_Y,samp_Z_2,'Color',[0.8500, 0.3250, 0.0980]);
p=patch([samp_X;fliplr(samp_X)], [samp_Y;fliplr(samp_Y)], [samp_Z_1;samp_Z_2],'r')
hold off
xlabel('X / mm');
ylabel('Y / mm');
zlabel('Z / mm');

Best Answer

Yes, your patch coordinates are not being created correctly. You need to have each patch sequentially. This means defining the 4 coordinates of the first box, then the 4 coordinates of the adjacent box, etc. This example may be helpful (the part that draws the 2 green triangles). Taking a similar approach, you want to define the 4 vertices in a column, and have as many columns as you have samples.
This means you need to have 4x as many X, Y, and Z coordinates as you have samples. It takes a little manipulating to get it right. I took the following approach
  • first row contains the bottom left vertex
  • second row contains the top left vertex
  • third row contains the top right vertex
  • fourth row contains the bottom right vertex
Here's your code with my modifications.
%% Some random parameters
a_fit=1000;
b_fit=1010;
%% create samples and apply to the ellipcital equation
samples=1000;
samp_theta=linspace(0,2*pi,samples);
samp_r=sqrt(1./((cos(samp_theta')).^2./a_fit.^2+(sin(samp_theta')).^2./b_fit.^2));
[samp_X,samp_Y] = pol2cart(samp_theta',samp_r);
samp_Z_1=repmat(-3,samples,1);
samp_Z_2=repmat(3,samples,1);
%% visualize
figure(1);
hold on
patch('XData',samp_X,'YData',samp_Y,'ZData',samp_Z_1,'FaceColor',[0.8500, 0.3250, 0.0980]);
patch('XData',samp_X,'YData',samp_Y,'ZData',samp_Z_2,'FaceColor',[0.8500, 0.3250, 0.0980]);
% This is new. I define the vertices using the existing variables
% The data is transposed so that they are 4xSamples
lX = [samp_X samp_X samp_X samp_X]';
lY = [samp_Y samp_Y samp_Y samp_Y]';
lZ = [samp_Z_1 samp_Z_2 samp_Z_2 samp_Z_1]';
% circshift the X and Y values to allow for my vertex numbering scheme
lX(3:4,:)=circshift(lX(3:4,:),-1,2);
lY(3:4,:)=circshift(lY(3:4,:),-1,2);
patch('XData',lX,'YData',lY,'ZData',lZ,'FaceColor',[0.8500, 0.3250, 0.0980],'EdgeColor',[0.8500, 0.3250, 0.0980]);
hold off
xlabel('X / mm');
ylabel('Y / mm');
zlabel('Z / mm');
view(3)
And just to make sure the first and last patches meet, here is a closer look at those patches.
figure
patch(lX(:,1:3),lY(:,1:3),lZ(:,1:3),'g')
hold on
patch(lX(:,end-3:end),lY(:,end-3:end),lZ(:,end-3:end),'r')
hold off
% added this to see in 3D
view(3)