function Polyeder

% http://geometrie.hs-nb.de/MATLAB-Programme/Polyeder.m   Bearbeitungsstand: 12.10.2004
% (C) Martin Nitschke, nitschke@hs-nb.de

clc;
close('all')
global ax
ax=[inf -inf inf -inf inf -inf];
deg=pi/180;
a=20; b=50; c=45; d=33;
P=[0  0  b  0.8*b    b-d
   0 -a -a -a+c -a+c
   0  0  0  0    0];
N=zeros(size(P));
alph=[45 30 40 30 50]*deg;
P=[P P(:,1)];
dP=diff(P')';
% drawPolygon(@plot3,P,'k');
for k=1:length(alph)
  v=dP(:,k)/norm(dP(:,k));
  w=cos(alph(k))*[-v(2);v(1);0]+sin(alph(k))*[0;0;1];
  n=cross(v,w);
  N(:,k)=n/norm(n);
end
First=[EEE(P,N,1,2,5) EEE(P,N,2,4,5),EEE(P,N,2,3,4)];
hold('on')
% drawPolygon(@plot3,[P(:,1) First(:,1) P(:,2)],'k')
% drawPolygon(@plot3,[P(:,3) First(:,3) P(:,4)],'k')
% drawPolygon(@plot3,[P(:,5) First(:,2)],'k')
drawPolygon(@fill3,[P(:,1) First(:,1)     P(:,2)],0.3*[1 1 1],'EdgeColor','none')
drawPolygon(@fill3,[P(:,2) First          P(:,3)],0.4*[1 1 1],'EdgeColor','none')
drawPolygon(@fill3,[P(:,3) First(:,3)     P(:,4)],0.5*[1 1 1],'EdgeColor','none')
drawPolygon(@fill3,[P(:,4) First(:,[3 2]) P(:,5)],0.6*[1 1 1],'EdgeColor','none')
drawPolygon(@fill3,[P(:,5) First(:,[2 1]) P(:,1)],0.7*[1 1 1],'EdgeColor','none')
% drawPolygon(@plot3,First,'-k','erase','xor')
axis('off')
view(-24,32)
axis('equal')
axis(ax)
zlim([0 0.1])
print('-deps','Polyeder');

function A=EEE(P,N,i,j,k);
A=[N(:,i)';N(:,j)';N(:,k)']\[N(:,i)'*P(:,i);N(:,j)'*P(:,j);N(:,k)'*P(:,k)];

function drawPolygon(draw,P,varargin);
global ax
feval(draw,P(1,:),P(2,:),P(3,:),varargin{:})
if min(P(1,:))<ax(1)
  ax(1)=min(P(1,:));
end
if max(P(1,:))>ax(2)
  ax(2)=max(P(1,:));
end
if min(P(2,:))<ax(3)
  ax(3)=min(P(2,:));
end
if max(P(2,:))>ax(4)
  ax(4)=max(P(2,:));
end
if min(P(3,:))<ax(5)
  ax(5)=min(P(3,:));
end
if max(P(3,:))>ax(6)
  ax(6)=max(P(3,:));
end
