How can I plot a filled cylinder with a specific height?
Show older comments
I am trying to plot two filled cylinders where the color represents the height of the cylinders.
Say each cylinder has the radius 1 and the heights are 2 and 3. I start with plotting the two circles. How can I continue from there?
figure
x=1;
y=2;
r=1;
th=0:pi/100:2*pi;
a=r*cos(th)+x;
b=r*sin(th)+y;
plot(a, b)
hold on
x=3;
y=5;
r=1;
th=0:pi/100:2*pi;
a=r*cos(th)+x;
b=r*sin(th)+y;
plot(a, b)
Accepted Answer
More Answers (1)
Here's a function where you just feed in the inputs
- r: radius of cyl.
- cnt: [x,y] center of cyl
- height: height of cyl
- nSides: number of "sides" of the cyl
- color: color of the cyl.
% Values for 2 cylinders
r = [1, 1]; % radii of each cyl
cnt = [1,2; 3,5]; % [x,y] center of each cyl
height = [2,8]; % height of each cyl
color = [1 0 0; 0 .5 0];% color of each cyl
nSides = 100; % number of "sides" of the cyl
% Create figure
figure()
hold on
% Loop through each cylinder
for i = 1:numel(r)
plotCylinderWithCaps(r(i),cnt(i,:),height(i),nSides,color(i,:));
end
view(3)
grid on
function [h1, h2, h3] = plotCylinderWithCaps(r,cnt,height,nSides,color)
[X,Y,Z] = cylinder(r,nSides);
X = X + cnt(1);
Y = Y + cnt(2);
Z = Z * height;
h1 = surf(X,Y,Z,'facecolor',color,'LineStyle','none');
h2 = fill3(X(1,:),Y(1,:),Z(1,:),color);
h3 = fill3(X(2,:),Y(2,:),Z(2,:),color);
end %only needed if this is within a script

Categories
Find more on Vector Fields 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!