Plotting Cylinder Surface Using fill3
Show older comments
I am attempting to plot a cylinder (or any abitrary) surface using the fill3 or patch function.
MATLAB, however, does not produce the cylinder surface using the minmal working example below, unless the angle theta is restricted to less than pi.
Any help explaining this issue and finding a solution is greatly appreciated!
figure()
theta = 0:0.5:2*pi;
xs = [cos(theta) flip(cos(theta))];
ys = [sin(theta) flip(sin(theta))];
fill3(xs, ys, [zeros(size(theta)) ones(size(theta))], 'k', 'FaceAlpha', 0.2)
Accepted Answer
More Answers (1)
Wick
4 minutes ago
0 votes
I realize this is an old question, but here's the solution for making the cylinder side with patch objects using direct manipulation of the vertices and faces.
clearvars
theta = 0:0.5:2*pi;
N = length(theta);
% allowing that the (x,y) at the top and bottom might not be the same if the cylinder isn't aligned along the z axis
x1 = cos(theta');
x2 = x1;
y1 = sin(theta');
y2 = y1;
z1 = zeros(size(x1));
z2 = ones(size(x2));
vertices = [[x1;x2] [y1;y2] [z1;z2]];
% building faces that go down each face then back up. Each edge repeats on adjacent faces
a = (1:N)';
d = [2:N 1]';
b = a + N;
c = d + N;
faces = [a b c d];
% You can remove the last line of 'faces' if your angle vector goes all the
% way back to exactly 2*pi. The original post only approximated that so the
% last line closes the outer curve by repeating the very first edge.
figure()
patch('Vertices',vertices,'Faces',faces,'FaceColor','r')
view(-52,56)
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



