I wanted to play with this as a building block for a bigger project. (These projects keep me entertained!)
I wanted to write a bit of MATLAB code that would create a colorful rotating cylinder. I know that core MATLAB offers the function cylinder that can generate surfaces of revolution (and thus cylinders!) However, I do not particularly like to be spoon-fed code all the time. Especially when it is straight forward to use a bit of math and code a few extra lines.
Here it goes:
% Define number of points on the x and z axis
N = 64;
M = 32;
x = linspace(-pi, pi, N);
z = linspace(-2, 2, M);
% Create color assignment for each data point.
% Cdata is a property of function surf that defines the vertex colors. A
% matrix containing values that specify the color at every point in ZData.
% Use pseudo-random number generator for ‘unique’ pattern’. Define outside loop to maintain assignment of colors for all iterations.
coloring = rand(M,N+1);
% Code in for-loop to generate rotational motion.
for ii = 1:N
dummy = [ii:N 1:ii];
x_ = x(dummy);
[xx, zz] = meshgrid(x_,z);
% Use parametric equations
X = 1 .*cos(xx);
Y = 1 .*sin(xx);
Z = zz;
h = surf(X,Y,Z); % Create 3D surface plot
axis off
set(h,'CData',coloring) % Use color scheme defined outside of loop
shading interp
F(ii) = getframe; % Get movie frames
pause(0.1) % To observe motion
end
An AVI movie can be created by playing the movie frames stored in memory:
movie2avi(F, 'rotatingCylinder01.avi', 'fps', 10, 'compression', 'Cinepak')
Random coloring schemes with stripes can be created as follows:
coloring = rand(M,N+1)
c = rand(1,N+1);
coloring = repmat(c, [M 1]);
c = rand(M,1);
coloring = repmat(c, 1, N+1);
