- /
-
Spinning Cylinders
on 7 Oct 2024
- 7
- 34
- 0
- 0
- 1331
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
function drawframe(frame)
% drawframe(f) creates a given animation frame given f
% Created by Jose Luis Paredes on 10-05-2024
arguments
frame (1,1){mustBeInteger, mustBePositive}
end
% Upper bound of the frames to be displayed
total_number_frames = 24;
if frame <= total_number_frames
% Create two cylinders -one with an increasing radius and one with a
% constant radius
inner_radius = 0.5;
outer_radius = 1;
const_radius = 1;
height = 2;
% Set the limit on the axes and rerive the figure
axes("XLim",[-outer_radius outer_radius],"YLim",[-outer_radius outer_radius],"ZLim",[-height height]);
% Retrieve the data beloging to each of the cylinders
[X1,Y1,Z1] = cylinder([inner_radius outer_radius]);
[X2,Y2,Z2] = cylinder(const_radius);
[X3,Y3,Z3] = cylinder([outer_radius inner_radius]);
% Scale the height of the cylinder with the variable radius
% translate the base and generate a surface plot
height1 = Z1*height;
cyl1(1) = surf(gca,X1,Y1,height1, 'FaceColor','interp');
hold on
height3 = Z3*height - height;
cyl1(2) = surf(gca,X3,Y3,height3, 'FaceColor','interp');
% Draw a cylinder midway the top and bottom
height2 = Z2*0.2;
surf(gca,X2,Y2,height2, 'FaceColor',[0.3010 0.7450 0.9330], 'EdgeColor','none');
midpoint = floor(size(X2,2)/2);
if frame <= midpoint
fill3(X2(1,1:midpoint),Y2(1,1:midpoint),Z2(1,1:midpoint),[1 0 0], 'EdgeColor', 'none');
elseif frame > midpoint
if frame < size(X2,2) && frame < total_number_frames
step = frame - midpoint;
fill3(X2(1,1:midpoint),Y2(1,1:midpoint),Z2(1,1:midpoint),[1 0 0], 'EdgeColor', 'none')
fill3(X2(1,step:midpoint+step),Y2(1,step:midpoint+step),Z2(1,step:midpoint+step),[1 0 0], ...
'EdgeColor', 'none');
else
fill3(X2(1,:),Y2(1,:),Z2(1,:),[1 0 0], 'EdgeColor', 'none');
end
end
% Generate the transform for each of the cylinders grouping the cylinders
% with the variables radius
h1 = hgtransform;
set(cyl1,"Parent", h1)
% Prepare to capture the animation into a movie
angle_vector = linspace(0, 2*pi, total_number_frames);
% Rotate the cylinders in opposite directions and place a sphere on top
% that moves in the direction of the rings
R1 = makehgtform("zrotate",angle_vector(frame));
h1.Matrix = R1;
else
return
end
end
Movie
Audio
This submission does not have audio.