How can I plot a filled cylinder with a specific height?
55 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lisa S
el 18 de Oct. de 2019
Comentada: Star Strider
el 30 de Ag. de 2020
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)
0 comentarios
Respuesta aceptada
Star Strider
el 18 de Oct. de 2019
Editada: Star Strider
el 18 de Oct. de 2019
You can use the cylinder function, however writing your own code is more fun:
x = [1 3];
y = [2 5];
r=1;
hgt = [2 3];
th=0:pi/100:2*pi;
a=r*cos(th);
b=r*sin(th);
figure
surf([a; a]+x(1), [b; b]+y(1), [ones(1,size(th,2)); zeros(1,size(th,2))]*hgt(1), 'FaceColor','b', 'EdgeColor','none')
hold on
surf([a; a]+x(2), [b; b]+y(2), [ones(1,size(th,2)); zeros(1,size(th,2))]*hgt(2), 'FaceColor','r', 'EdgeColor','none')
plot3(a+x(1), b+y(1), ones(1,size(th,2))*hgt(1), '-k') % Draws Circle Around Top Of Cylinder
plot3(a+x(2), b+y(2), ones(1,size(th,2))*hgt(2), '-k') % Draws Circle Around Top Of Cylinder
hold off
axis('equal')
view(45,30)
The two surf calls (one for each cylinder) draws the cylinders. The plot3 calls draw circles around the tops of them to make the edge more visible.
Experiment to get different results.
EDIT — (18 Oct 2019 at 21:02)
Added plot figure:
2 comentarios
Más respuestas (1)
Adam Danz
el 18 de Oct. de 2019
Editada: Adam Danz
el 18 de Oct. de 2019
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
0 comentarios
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!