%
% hw02.m
%
% 02/06/03 jph
% 
%    03/06/05: corrected error in calculation of xx and yy.
%

%
% Initialize the world coordinates of the eight cube corners
%
P(1).X = 0.00; P(1).Y = 0.00; P(1).Z = 0.00;
P(2).X = 0.00; P(2).Y = 0.00; P(2).Z = 0.05;
P(3).X = 0.05; P(3).Y = 0.00; P(3).Z = 0.05; 
P(4).X = 0.05; P(4).Y = 0.00; P(4).Z = 0.00;
P(5).X = 0.00; P(5).Y = 0.05; P(5).Z = 0.00;
P(6).X = 0.00; P(6).Y = 0.05; P(6).Z = 0.05;
P(7).X = 0.05; P(7).Y = 0.05; P(7).Z = 0.05;
P(8).X = 0.05; P(8).Y = 0.05; P(8).Z = 0.00;
%
% First rotation - update X & Z coordinates of all eight points
%
Theta = 30.0 * 2.0 * pi / 360.0;
for i=1:8
  Zold = P(i).Z;
  Xold = P(i).X;
  P(i).Z = cos(Theta) * Zold - sin(Theta) * Xold;
  P(i).X = sin(Theta) * Zold + cos(Theta) * Xold;
end
%
% Second rotation - update Y & Z coordinates of all eight points
%
Theta = -20.0 * 2.0 * pi / 360.0;
for i=1:8
  Yold = P(i).Y;
  Zold = P(i).Z;
  P(i).Y = cos(Theta) * Yold - sin(Theta) * Zold;
  P(i).Z = sin(Theta) * Yold + cos(Theta) * Zold;
end
%
% Translation - update Z coordinates of all eight points
%
for i=1:8
  P(i).Z = P(i).Z + 1.0;
end
%
% Project all eight points into the image plane
%
f = 0.05;
for i=1:8
  P(i).x = f/P(i).Z * P(i).X;
  P(i).y = f/P(i).Z * P(i).Y;
  P(i).z = 0.0;
end
%
% plot the resulting image
%
order = [1 5 6 7 3 4]; % this ordering enables 'fill' to deal 
                       % with the occluded edges
xx = zeros(6,1);
yy = zeros(6,1);
for i=1:6
  xx(i) = P(order(i)).x;
  yy(i) = P(order(i)).y;
end
fill(xx,yy,[0 0 0]);
axis([-0.001 0.004 -0.001 0.004],'equal');
print -deps ML02.eps
print -dtiff ML02.tif
print -dps ML02.ps
