Thursday, April 15, 2010

MATLAB: Generate Carousel I

In earlier posts I described how to generate a rotating cylinder using MATLAB and how to define to coloring scheme of the rotating cylinder. The reason for these exercises was that I wanted to write code that would create a carousel for entertainment and because I can. I selected MATLAB because among other great attributes it is perfect for prototyping.

I am not done with the design yet but so far it consists of 3 elements: Cylinder, cone, and circle. The cylinder was described in the aforementioned posts. For the other two elements:

CONE:

The roof of the carousel is a cone described using the following parametric equations:

x = (h-u)/hrcostheta

y = (h-u)/hrsintheta

z = u

where, h is the height of the cone, r is the radius of the base,  u in [0,h] and theta in [0,2pi).

In the MATLAB codespace the following entry was used to describe a cone:

rCc = 15; %radius
thetaCc = linspace(0, 2*pi, N);
hCc = linspace(4, 7, M); %height

[UCc, hCc_] = meshgrid(thetaCc,hCc);

[XCc,YCc,ZCc] = gkcone(rCc,hCc_,UCc);

hhCc = surf(XCc,YCc,ZCc);
set(hhCc,'CData',colorCylinder);

=============================================================

function [x,y,z] = gkcone(radius,height,theta)

step = (max(max(height)) - height) ./max(max(height));

x = step .*radius .*cos( theta );
y = step .*radius .*sin( theta );
z = height;

CIRCLE:

Parametric equations were used to describe the circle too. The equations are the same as the cylinder but with a zero z-axis component.

I created 4 cylinders, 4 circles, and a cone with the following result:

The color scheme of the circles is solid white or solid gray. These where generated using:

color_ = ones(M,N+1,3); % WHITE

color_ = ones(M,N+1,3) * 0.9; % GRAY

No comments:

Post a Comment