Tuesday, March 9, 2010

MATLAB: Generate Rotating Cylinder II

In an earlier cylinder post I put together MATLAB code that could generate a rotating cylinder. The cylinder was described using parametric equations

x = rcostheta 

y = rsintheta

z = z

for z in [0,h] and theta in [0,2pi).

The coloring scheme of the rotating cylinder was defined with indexed values which is a quick way to do it but not very intuitive especially if you are used to RGB (Red-Green-Blue) definitions.

MATLAB makes it easy to specify truecolor that can be used to define the vertex colors (through the CData property) by simply defining an m-by-n-by-3 array, where the 3 corresponds to the RGB triplet. That is, the first m-by-n matrix contains the red components, the second the green components, and the third the blue components of the colors.

For my project I selected an alternating white, light blue, and pink.

The code:

% per MATLAB: white: [1 1 1]
% per MATLAB: light blue: [0.043    0.518    0.780]
% per MATLAB: pink: [1.000    0.600    0.784]

colorIndex = [
   1 1 1;
   0.043    0.518    0.780;
   1.000    0.600    0.784;
   ];

coloring = ones(M,N+1,3); % All white

for id = 2:3:N+1
   for jd = 1:3;
      coloring(:,id,jd) = coloring(:,id,jd) *colorIndex(2,jd);
   end
end

for id = 3:3:N+1
   for jd = 1:3;
      coloring(:,id,jd) = coloring(:,id,jd) *colorIndex(3,jd);
   end
end

First the truecolor scheme parameter coloring is set to white for all values in the m-by-n-by-3 array. The reasons are:

  1. I elected a third of the multi-dimensional array to be white.
  2. Speed up the 2 for-loops.

The for-loops are a quick way to define the other two colors per column for the rest of the array. The rest of the code is the same as shown in the earlier post.

The results is:

 

truecolor_flat

 

 

shading flat

 

 

 

truecolor_interp

 

 

shading interp

No comments:

Post a Comment