% -------------------------------------------------------------------------
% script to demonstrate x, y and z matrices for surf()
% -------------------------------------------------------------------------

R = 2;

phi = [-pi/2:pi/12:pi/2];
cosphi = cos(phi); sinphi = sin(phi);

theta = [-pi:pi/12:pi];
costheta = cos(theta); sintheta = sin(theta);

hold off; newplot; hold on;
grid on; view(217.5,20); axis([-8 8 -4 4 0 4], 'equal');
xlabel('x'); ylabel('y'); zlabel('z');

x1 = repmat(R * theta, size(phi'));
y1 = repmat(R * phi', size(theta));
z1 = repmat(0, size(phi,2), size(theta,2));
c1 = [.5 .8 .8];
h1 = surf(x1, y1, z1);
set(h1, ...
  'facealpha', .25, 'facecolor', c1, 'edgecolor', [.5 .5 .5] ...
);

x2 = R * cosphi' * costheta;
z2 = R * cosphi' * sintheta + R;
y2 = repmat(R * sinphi', size(theta));
c2 = [.8 .5 .5];
h2 = surf(x2, y2, z2);
set(h2, ...
  'facealpha', .25, 'facecolor', c2, 'edgecolor', [.5 .5 .5] ...
);
delete(h1); delete(h2);

imax = 30;
h = surf(x1, y1, z1);
set(h, 'facealpha', .25, ...
  'facecolor', c1, 'edgecolor', [.5 .5 .5] ...
);
getframe;
for i = 1:imax
  a = (imax-i)/imax; b = i/imax;
  delete(h);
  h = surf(a*x1 + b*x2, a*y1 + b*y2, a*z1 + b*z2);
  set(h, 'facealpha', .25, ...
    'facecolor', a*c1 + b*c2, 'edgecolor', [.5 .5 .5] ...
  );
  getframe;
end;
