% -------------------------------------------------------------------------
% script file to draw double a prism with a given number of faces
% -------------------------------------------------------------------------

% size of base and number of faces
r = 2; n = 3; alpha = 2*pi/n;

ca = cos(alpha);sa = sin(alpha);
R = [ % rotate about z by 2*pi / n
   ca -sa  0   0
   sa  ca  0   0
   0   0   1   0
   0   0   0   1
];
F = [ % reflect across xy plane
   1   0   0   0
   0   1   0   0
   0   0  -1   0
   0   0   0   1
];

vx = r*sin(alpha/2); vy = r*cos(alpha/2);
v = [ % vertices of the first patch
  -vx   vx   0
  -vy  -vy   0
   0    0    r
   1    1    1
];
u = F * v; % upside-down copy of the patch

% just for fun, assign a different color to each patch
clr = [1 0 0]; hue = 0; delta = 2*pi/n;

% setup the plotting parameters
feature('UseGenericOpenGL', 1);
hold off; newplot; hold on;
view(30, 15); axis([-3 3 -3 3 -3 3], 'square'); grid on;
set(gcf, 'Renderer', 'OpenGL');

% accumulate vertices for the patch object
x = []; y = []; z = []; c = [];
x = [x v(1,1:3)']; y = [y v(2,1:3)']; z = [z v(3,1:3)']; c(1,1,1:3) = 1.00 * clr;
for i = 2:n
  v = R * v; [clr hue] = HueRot(hue, alpha);
  x = [x v(1,:)']; y = [y v(2,:)']; z = [z v(3,:)']; c(1,i,1:3) = 1.00 * clr;
end
x = [x u(1,1:3)']; y = [y u(2,1:3)']; z = [z u(3,1:3)']; c(1,n+1,1:3) = 0.67 * clr;
for i = 2:n
  u = R * u; [clr hue] = HueRot(hue, alpha);
  x = [x u(1,1:3)']; y = [y u(2,1:3)']; z = [z u(3,1:3)']; c(1,n+i,1:3) = 0.67 * clr;
end

% draw the patch object, using its handle to set transparency
h = patch(x, y, z, c);
set(h, 'FaceAlpha', .5);

hold off;
