function kotD

% http://geometrie.hs-nb.de/MATLAB-Programme/kotD.m   Bearbeitungsstand: 2.3.2005
% (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')';

Name='kotD-Vgb';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,P,'k','linewidth',2.0);
axis('off')
axis('equal')
view(2)
axis(ax(1:4))
Schrift(P(:,1),'{\itA}','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(P(:,2),'{\itB}','VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(P(:,3),'{\itC}','VerticalAlignment','middle','HorizontalAlignment','left','erase','none')
Schrift(P(:,4),'{\itD}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(P(:,5),'{\itE}','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(mean(P(:,[2 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((3*P(:,5)+P(:,1))/4,sprintf('%.0f%s',round(alph(5)/deg),'^{\circ }'),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
print('-deps',Name);

Name='kotD-2d';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,P,'k','linewidth',2.0);
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')
h=0.75*min(First(3,:));
M0=[min(P(1,:));max(P(2,:))-h;0]+[1;-1;0];
drawPolygon(@plot3,[M0+h*cot(min(alph))*[1;0;0] M0 M0+h*[0;1;0]],'+-k');
drawPolygon(@plot3,[M0+h*[0;1;0] M0+h*[1.1*cot(min(alph));1;0] ],':k');
for a=unique(alph)
  drawPolygon(@plot3,[M0 M0+1.1*h*[cot(a);1;0]],'k');
  drawPolygon(@plot3,[M0+h*[cot(a);1;0] M0+h*[cot(a);0;0]],'+:k');
  Schrift(M0+1.1*h*[cot(a);1;0],sprintf(' %.0f%s',round(a/deg),'^\circ'),'rotation',a/deg,'VerticalAlignment','baseline','HorizontalAlignment','left')
  if abs(a-min(alph))<sqrt(eps)
    Schrift(M0+h*[cot(a);0;0],sprintf('{\\itd}_{%.0f}',round(a/deg)),'VerticalAlignment','top','HorizontalAlignment','left')
  else
    Schrift(M0+h*[cot(a);0;0],sprintf('{\\itd}_{%.0f}',round(a/deg)),'VerticalAlignment','top','HorizontalAlignment','center')
  end
end
Schrift(M0,'0','VerticalAlignment','top','HorizontalAlignment','right')
Schrift(M0+[0;h;0],'\ith ','VerticalAlignment','middle','HorizontalAlignment','right')
drawPolygon(@plot3,[P(:,3) First(:,3) P(:,4)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,5) First(:,2)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,1) First(:,1)     P(:,2)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,2) First          P(:,3)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,3) First(:,3)     P(:,4)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,4) First(:,[3 2]) P(:,5)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,5) First(:,[2 1]) P(:,1)],'k','linewidth',2.0)
drawPolygon(@plot3,First,'k','linewidth',2.0)
drawPolygon(@plot3,[1.15*First(:,1)-0.15*P(:,1) First(:,1) 1.15*First(:,1)-0.15*P(:,2)],'k')
drawPolygon(@plot3,[1.15*First(:,3)-0.15*P(:,3) First(:,3) 1.15*First(:,3)-0.15*P(:,4)],'k')
drawPolygon(@plot3,[1.15*First(:,2)-0.15*P(:,5) First(:,2) 2*First(:,2)-1*First(:,3)],'k')
H=[gra(P(:,1),First(:,1),h) ...
   gra(P(:,2),First(:,1),h) ...
   gra(P(:,3),First(:,3),h) ...
   gra(P(:,4),First(:,3),h) ...
   gra(P(:,5),First(:,2),h)];
H=[H H(:,1)];
for k=2:length(H);
  v=H(:,k)-H(:,k-1);
  v=2*v/norm(v);
  drawPolygon(@plot3,[H(:,k-1)-v H(:,k)+v],'--k')
end
Schrift(mean(H(:,[5 6])')','\ith''','Rotation',180+atan2(H(2,6)-H(2,5),H(1,6)-H(1,5))/deg,'VerticalAlignment','baseline','HorizontalAlignment','center','erase','none')
MP=mean(P(:,[2 3])')';
v=P(:,3)-P(:,2);
v=v/norm(v);
MH=MP+h*[cot(alph(2))*[-v(2);v(1)];1];
drawPolygon(@plot3,[MP MH],'k')
Spitze(MP,MP-MH,2,30*deg)
Spitze(MH,MH-MP,2,30*deg)
Schrift(mean([MP MH]')',sprintf('_{ }{\\itd}_{%.0f}',round(alph(2)/deg)),'VerticalAlignment','middle','HorizontalAlignment','left')
MP=mean(P(:,[4 5])')';
v=P(:,5)-P(:,4);
v=v/norm(v);
MH=MP+h*[cot(alph(4))*[-v(2);v(1)];1];
drawPolygon(@plot3,[MP MH],'k')
Spitze(MP,MP-MH,2,30*deg)
Spitze(MH,MH-MP,2,30*deg)
Schrift(mean([MP MH]')',sprintf('_{ }{\\itd}_{%.0f}',round(alph(4)/deg)),'VerticalAlignment','middle','HorizontalAlignment','left')
axis('off')
axis('equal')
view(2)
axis(ax(1:4))
Schrift(P(:,1),'{\itA}','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(P(:,2),'{\itB}','VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(P(:,3),'{\itC}','VerticalAlignment','middle','HorizontalAlignment','left','erase','none')
Schrift(P(:,4),'{\itD}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(P(:,5),'{\itE}','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(mean(P(:,[2 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((3*P(:,5)+P(:,1))/4,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','bottom','HorizontalAlignment','left','erase','xor')
Schrift(First(:,3),'_{  }\itH''','VerticalAlignment','middle','HorizontalAlignment','left','erase','xor')
Schrift(0.6*P(:,1)+0.4*First(:,1),'{\its}_1^{,}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','xor')
Schrift(0.65*P(:,2)+0.35*First(:,1),'_{  }{\its}_2^{,}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','xor')
print('-deps',Name);

Name='kotD-3d';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,P,'k');
hold('on')
drawPolygon(@plot3,H,'--k','erase','xor');
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')
Schrift(0.45*P(:,1)+0.55*First(:,1),'{\its}_1 ','VerticalAlignment','middle','HorizontalAlignment','center','erase','xor')
Schrift((P(:,2)+First(:,1))/2,'_{ }{\its}_2','VerticalAlignment','middle','HorizontalAlignment','left','erase','xor')
axis('off')
view(-24,32)
axis('equal')
Schrift(P(:,1),'{\itA}','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(P(:,2),'{\itB}','VerticalAlignment','top','HorizontalAlignment','right','erase','none')
Schrift(P(:,3),'{\itC}','VerticalAlignment','middle','HorizontalAlignment','left','erase','none')
Schrift(P(:,4),'{\itD}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(P(:,5),'{\itE}','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(mean(H(:,[2 3])')','\ith','VerticalAlignment','baseline','HorizontalAlignment','center','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(mean(P(:,[2 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((3*P(:,5)+P(:,1))/4,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','center','erase','xor')
Schrift(First(:,3),'\itH','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
print('-deps',Name);

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 H=gra(P,F,h)
v=F-P;
H=P+((h-P(3))/v(3))*v;

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',30,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
