function Dach

% http://geometrie.hs-nb.de/MATLAB-Programme/Dach.m   Bearbeitungsstand: 21.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=75; c=45; d=33;
P=[0  0  b  b    b-d
   0 -a -a -a+c -a+c
   0  0  0  0    0];
N=zeros(size(P));
NSym=sym(N);
alph=[45 45 45 30  30]*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);
  NSym(:,k)=sym(n)/sqrt(dot(sym(n),sym(n)));
end
First=[EEE(P,N,1,2,5) EEE(P,N,2,4,5),EEE(P,N,2,3,4)]
FSym=[EEE(sym(P),NSym,1,2,5) EEE(sym(P),NSym,2,4,5),EEE(sym(P),NSym,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.5*[1 1 1])
drawPolygon(@fill3,[P(:,2) First          P(:,3)],0.9*[1 1 1])
drawPolygon(@fill3,[P(:,3) First(:,3)     P(:,4)],0.5*[1 1 1])
drawPolygon(@fill3,[P(:,4) First(:,[3 2]) P(:,5)],0.6*[1 1 1])
drawPolygon(@fill3,[P(:,5) First(:,[2 1]) P(:,1)],0.7*[1 1 1])
drawPolygon(@plot3,First,'.-k','MarkerSize',30,'erase','xor')
axis('off')
view(-24,32)
axis('equal')
axis([ax(1:4)+[-7 18 0 0] 0 0.1])
PRef=(P(:,2)+2*P(:,3))/3;
L=7;
vRef=P(:,3)-P(:,2);
vRef=L*vRef/norm(vRef);
uRef=2*[-vRef(2);vRef(1);0];
wRef=cos(alph(2))*uRef+sin(alph(2))*[0;0;2*L];
drawPolygon(@plot3,[PRef-vRef+wRef PRef-vRef PRef+vRef],'k','linewidth',2.0,'erase','xor')
drawPolygon(@plot3,[PRef-vRef PRef-vRef+uRef],':k','linewidth',2.0,'erase','xor')
Schrift(PRef+vRef,'\bf\itv','VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Schrift(PRef-vRef+uRef,'\bf\itu','VerticalAlignment','baseline','HorizontalAlignment','right','erase','xor')
Schrift(PRef-vRef+wRef,' \bf\itw','VerticalAlignment','middle','HorizontalAlignment','left','erase','xor')
Spitze(PRef+vRef,vRef,L/2,30*deg)
Spitze(PRef-vRef+wRef,wRef,L/2,30*deg)
Spitze(PRef-vRef+uRef,uRef,L/2,30*deg,'erase','xor')
Schrift(P(:,1),sprintf('%s(%.0f,%.0f,%.0f) ','{\itA}',P(:,1)),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(P(:,2),sprintf('%s(%.0f,%.0f,%.0f) ','{\itB}',P(:,2)),'VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(P(:,3),sprintf(' %s(%.0f,%.0f,%.0f)','{\itC}',P(:,3)),'VerticalAlignment','middle','HorizontalAlignment','left','erase','none')
Schrift(P(:,4),sprintf(' %s(%.0f,%.0f,%.0f)','{\itD}',P(:,4)),'VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(P(:,5),sprintf('%s(%.0f,%.0f,%.0f) ','{\itE}',P(:,5)),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(mean(P(:,[1 2])')'-[0;0;2.5],sprintf('%.0f%s  ',round(alph(1)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift((2*P(:,2)+P(:,3))/3,sprintf('%.0f%s',round(alph(2)/deg),'^\circ'),'VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Schrift(mean(P(:,[3 4])')',sprintf(' %.0f%s',round(alph(3)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(mean(P(:,[4 5])')',sprintf('%.0f%s',round(alph(4)/deg),'^\circ'),'VerticalAlignment','bottom','HorizontalAlignment','center','erase','none')
Schrift(mean(P(:,[5 1])')',sprintf('%.0f%s',round(alph(5)/deg),'^{\circ }'),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(First(:,1),' \itF','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(First(:,2),'\itG ','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
Schrift(First(:,3),'\itH','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
print('-deps','Dach');

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

function Schrift(P,txt,varargin)
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',24,varargin{:});

function Spitze(P,v,L,phi,varargin);
deg=pi/180;
if nargin<=2
  L=1;
end
if nargin<=3
  phi=7.5*deg;
end
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
Q=P-v;
t=[0:6:360]*deg;
for k=1:size(P,2)
  N=tan(phi/2)*null(v(:,k)');
  for w=2:length(t)
    plot3(...
      [P(1,k) Q(1,k)+cos(t(w))*L(:,k)*N(1,1)+sin(t(w))*L(:,k)*N(1,2) Q(1,k)+cos(t(w-1))*L(:,k)*N(1,1)+sin(t(w-1))*L(:,k)*N(1,2) P(1,k)],...
      [P(2,k) Q(2,k)+cos(t(w))*L(:,k)*N(2,1)+sin(t(w))*L(:,k)*N(2,2) Q(2,k)+cos(t(w-1))*L(:,k)*N(2,1)+sin(t(w-1))*L(:,k)*N(2,2) P(2,k)],...
      [P(3,k) Q(3,k)+cos(t(w))*L(:,k)*N(3,1)+sin(t(w))*L(:,k)*N(3,2) Q(3,k)+cos(t(w-1))*L(:,k)*N(3,1)+sin(t(w-1))*L(:,k)*N(3,2) P(3,k)],...
      'k',varargin{:});
  end
end
