%% Math 152: Parallelopiped demo

%% example in lectures
a = [0 0 1];
b = [1 -1 1];
c = -[-1 -1 1];

%% reduces to well-known result if there are all right-angles
%a = [2 0 0];
%b = [0 3 0];
%c = [0 0 5];

%% example with zero volume
%a = [0 0 1];
%b = [1 -1 1];
%c = 0.1*a + 0.3*b;

%% volume via det of 3x3 matrix
M = [a; b; c]
volume = det(M)

%% plot the 3 vectors
figure(1); clf;
lw = 'linewidth';
plot3([0 a(1)], [0 a(2)], [0 a(3)], 'r-', lw, 7);
hold on
plot3([0 b(1)], [0 b(2)], [0 b(3)], 'b-', lw, 7);
plot3([0 c(1)], [0 c(2)], [0 c(3)], 'g-', lw, 7);

axis equal
xlabel('x'); ylabel('y'); zlabel('z');
colormap('gray')

%% not recomended reading past here ;-)
% unpleasant code to plot the parallelopiped
o = [0 0 0];
f1 = [o; a; a+b; b];
f2 = [o; a; a+c; c];
f3 = [o; b; b+c; c];
f4 = f1 + c;
f5 = f2 + b;
f6 = f3 + a;

xx = [f1(:,1) f2(:,1) f3(:,1) f4(:,1) f5(:,1) f6(:,1)];
yy = [f1(:,2) f2(:,2) f3(:,2) f4(:,2) f5(:,2) f6(:,2)];
zz = [f1(:,3) f2(:,3) f3(:,3) f4(:,3) f5(:,3) f6(:,3)];

patch(xx, yy, zz, zz);

