Plotting Cylinder Surface Using fill3
显示 更早的评论
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)
采纳的回答
更多回答(1 个)
Wick
about 15 hours 前
0 个投票
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)
类别
在 帮助中心 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




