function kotA

% http://geometrie.hs-nb.de/MATLAB-Programme/kotA.m   Bearbeitungsstand: 12.12.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];
alph=[45 45 45 30  30]*deg;
P=[P P(:,1)];
dP=diff(P')';
N=zeros(size(P));
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)]
alph=[45 45 45 30  30]*deg;
P=[P P(:,1)];
dP=diff(P')';

Name='kotA-3d';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,P,'k','linewidth',2.0);
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')
A1=Abw([P(:,1) First(:,1)     P(:,2)],alph(1),0.5*[1 1 1]);
A2=Abw([P(:,2) First          P(:,3)],alph(2),0.9*[1 1 1]);
A3=Abw([P(:,3) First(:,3)     P(:,4)],alph(3),0.5*[1 1 1]);
A4=Abw([P(:,4) First(:,[3 2]) P(:,5)],alph(4),0.6*[1 1 1]);
A5=Abw([P(:,5) First(:,[2 1]) P(:,1)],alph(5),0.7*[1 1 1]);
drawPolygon(@plot3,First,'k')
axis('off')
view(13,36)
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)-[0;2;0],'{\itE}    ','VerticalAlignment','middle','HorizontalAlignment','right','erase','xor')
% 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')
Schrift(A5(1:3,2),'[{\itG}]_{1  }','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(A5(1:3,3),'[{\itF}]_1','VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(A1(1:3,2),'[{\itF}]_{2 }','VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(A2(1:3,2),'[{\itF}]_3','VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Schrift(A2(1:3,3),'[{\itG}]_3','VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Schrift(A2(1:3,4),'[{\itH}]_3','VerticalAlignment','top','HorizontalAlignment','left','erase','none')
Schrift(A3(1:3,2),'_{  }[{\itH}]_4','VerticalAlignment','middle','HorizontalAlignment','left','erase','none')
Schrift(A4(1:3,2)+[0;1;0],'_{  }[{\itH}]_5','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(A4(1:3,3),'[{\itG}]_{5 }','VerticalAlignment','bottom','HorizontalAlignment','right','erase','none')
print('-deps',Name);

Name='kotA-2d-Lsg';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,P,'k','linewidth',2.0);
hold('on')
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)
axis('off')
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','top','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])')',sprintf('%.0f%s',round(alph(1)/deg),'^\circ'),'VerticalAlignment','middle','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((P(:,5)+P(:,1))/2,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','top','HorizontalAlignment','right','erase','xor')
drawPolygon(@plot3,[A1 A2 A3 A4 A5],'k','linewidth',0.5)
drawPolygon(@plot3,[A1 A2 A3 A4 A5],'-.k','linewidth',3.0)
drawPolygon(@plot3,[A1(1:3,2) First(:,1)],'--k')
drawPolygon(@plot3,[A2(1:3,2) First(:,1)],'--k')
drawPolygon(@plot3,[A2(1:3,3) First(:,2)],'--k')
drawPolygon(@plot3,[A2(1:3,4) First(:,3)],'--k')
drawPolygon(@plot3,[A3(1:3,2) First(:,3)],'--k')
drawPolygon(@plot3,[A4(1:3,2) First(:,3)],'--k')
drawPolygon(@plot3,[A4(1:3,3) First(:,2)],'--k')
drawPolygon(@plot3,[A5(1:3,2) First(:,2)],'--k')
drawPolygon(@plot3,[A5(1:3,3) First(:,1)],'--k')
k=5;
v=dP(:,k)/norm(dP(:,k));
w=[-v(2);v(1);0];
a=P(:,5)-P(:,1);
a=a/norm(a);
GAE=P(:,1)+a*a'*(A5(1:3,2)-P(:,1));
% Schrift(GAE+[0;2;0],'\itG_{AE  }','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
r=norm(A5(1:3,2)-GAE);
t=degGrid(0,alph(k));
K=repmat(GAE,1,length(t))+0.7*r*(repmat(cos(t),3,1).*repmat(w,1,length(t))+repmat(sin(t),3,1).*repmat(v,1,length(t)));
plot3(K(1,:),K(2,:),K(3,:),'k','linewidth',0.5,'erase','xor')
Schrift(K(:,end),sprintf('%.0f%s',round(alph(5)/deg),'^{\circ  }'),...
  'rotation',-atan2(GAE(2)-K(2,end),K(1,end)-GAE(1))/deg,'VerticalAlignment','bottom','HorizontalAlignment','right','erase','none')
G1=[First(1:2,2);0]+sin(alph(k))*r*v;
a1=G1-[First(1:2,2);0];
a2=G1-GAE
drawPolygon(@plot3,[[First(1:2,2);0] [First(1:2,2);0]+1.1*a1],'k','linewidth',0.5)
drawPolygon(@plot3,[GAE GAE+1.1*a2],'k','linewidth',0.5)
% Schrift(G1+[0;1;0],'[{\itG}]_0','VerticalAlignment','top','HorizontalAlignment','left','erase','none')
t=degGrid(0,pi-alph(k));
K=repmat(GAE,1,length(t))+repmat(cos(t),3,1).*repmat(a2,1,length(t))-repmat(sin(t),3,1).*repmat([-a2(2);a2(1);0],1,length(t));
plot3(K(1,:),K(2,:),K(3,:),':k','linewidth',1.0,'erase','xor')
Spitze(K(:,end),-sin(t(end))*a2-cos(t(end))*[-a2(2);a2(1);0],4,30*deg)
Schrift(A5(1:3,2),'_{ }[{\itG}]_1','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
S=Aff(First(:,2),First(:,1),P(:,5),P(:,1),A5(1:3,3));
Schrift(S,'{\itS}','VerticalAlignment','bottom','HorizontalAlignment','right','erase','none')
Schrift(A5(1:3,3),'_{ }[{\itF}]_1','VerticalAlignment','bottom','HorizontalAlignment','right','erase','none')
Kr(P(:,1),A5(1:3,3),A1(1:3,2))
Schrift(A1(1:3,2),'[{\itF}]_{2 }','VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Kr(P(:,2),A1(1:3,2),A2(1:3,2))
Schrift(A2(1:3,2)+[0;1;0],'[{\itF}]_3','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
drawPolygon(@plot3,[P(:,2) S A2(1:3,2)],'k')
Schrift(A2(1:3,3),'[{\itG}]_3','VerticalAlignment','top','HorizontalAlignment','right','erase','none')
Schrift(A2(1:3,4),'[{\itH}]_{3 }','VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Kr(P(:,3),A2(1:3,4),A3(1:3,2))
Schrift(A3(1:3,2),'_{  }[{\itH}]_4','VerticalAlignment','middle','HorizontalAlignment','left','erase','none')
Kr(P(:,4),A3(1:3,2),A4(1:3,2))
Schrift(A4(1:3,2),'[{\itH}]_5','VerticalAlignment','bottom','HorizontalAlignment','left','erase','none')
Schrift(A4(1:3,3),'[{\itG}]_{5 }','VerticalAlignment','bottom','HorizontalAlignment','right','erase','none')
Kr(P(:,5),A4(1:3,end-1),A5(1:3,2))
view(2)
axis(ax(1:4))
print('-deps',Name);

Name='kotA-2d-Vgb';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,P,'k','linewidth',2.0);
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(@plot3,[P(:,1) First(:,1)     P(:,2)],'k')
drawPolygon(@plot3,[P(:,2) First          P(:,3)],'k')
drawPolygon(@plot3,[P(:,3) First(:,3)     P(:,4)],'k')
drawPolygon(@plot3,[P(:,4) First(:,[3 2]) P(:,5)],'k')
drawPolygon(@plot3,[P(:,5) First(:,[2 1]) P(:,1)],'k')
drawPolygon(@plot3,First,'k')
axis('off')
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','top','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])')',sprintf('%.0f%s',round(alph(1)/deg),'^{\circ }'),'VerticalAlignment','middle','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((P(:,5)+P(:,1))/2,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','top','HorizontalAlignment','right','erase','xor')
view(2)
axis(ax(1:4))
print('-deps',Name,'-loose');

Name='kotA-3d-Lsg';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,P,'k','linewidth',2.0);
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')
A1=Abw([P(:,1) First(:,1)     P(:,2)],alph(1),0.5*[1 1 1],[]);
A2=Abw([P(:,2) First          P(:,3)],alph(2),0.9*[1 1 1],[]);
A3=Abw([P(:,3) First(:,3)     P(:,4)],alph(3),0.5*[1 1 1],[]);
A4=Abw([P(:,4) First(:,[3 2]) P(:,5)],alph(4),0.6*[1 1 1],[]);
A5=Abw([P(:,5) First(:,[2 1]) P(:,1)],alph(5),0.7*[1 1 1],2)
drawPolygon(@plot3,First,'k');
drawPolygon(@plot3,[A5(1:3,2) S P(:,1)],'k','erase','xor')
drawPolygon(@plot3,[First(:,1) S P(:,2)],'k','erase','xor')
drawPolygon(@plot3,[A2(1:3,2) S],'k','erase','xor')
Kr(P(:,1),A5(1:3,end-1),A1(1:3,2))
Kr(P(:,2),A1(1:3,end-1),A2(1:3,2))
Kr(P(:,3),A2(1:3,end-1),A3(1:3,2))
Kr(P(:,4),A3(1:3,end-1),A4(1:3,2))
Kr(P(:,5),A4(1:3,end-1),A5(1:3,2))
axis('off')
view(13,36)
axis('equal')
print('-deps',Name);

function Ab=Abw(P,alph,col,kr,ord)
if nargin<4
  kr=2:size(P,2)-1;
end
if nargin<5
  ord=2:size(P,2)-1;
end
deg=pi/180;
drawPolygon(@fill3,P,col);
v=P(:,end)-P(:,1);
v=v/norm(v);
n=[-v(2);v(1);0];
w=-cos(alph)*[-v(2);v(1);0]-sin(alph)*[0;0;1];
phi=acos(n'*w);
I=eye(3);
D=cos(phi)*I+(1-cos(phi))*v*v'-sin(phi)*[0 v(3) -v(2);-v(3) 0 v(1);v(2) -v(1) 0];
Ab=[I P(:,1);0 0 0 1]*[D zeros(3,1);0 0 0 1]*[I -P(:,1);0 0 0 1]*[P;ones(1,size(P,2))];
drawPolygon(@fill3,Ab,col,'linewidth',0.5);
drawPolygon(@plot3,Ab,'-.k','linewidth',3.0);
T=degGrid(0,phi,3);
A=zeros(4,length(T));
for k=kr
  for t=1:length(T)
    D=cos(T(t))*I+(1-cos(T(t)))*v*v'-sin(T(t))*[0 v(3) -v(2);-v(3) 0 v(1);v(2) -v(1) 0];
    A(:,t)=[I P(:,1);0 0 0 1]*[D zeros(3,1);0 0 0 1]*[I -P(:,1);0 0 0 1]*[P(:,k);1];
  end
  drawPolygon(@plot3,A,':k','linewidth',1.0)
  Spitze(A(1:3,end),[0;0;-1],6,30*deg);
end
for o=ord
  drawPolygon(@plot3,[P(:,o) P(:,1)+v*v'*(P(:,o)-P(:,1)) Ab(1:3,o)],'--k','erase','xor','linewidth',1.0)
end

function S=Aff(G,F,E,A,G1)
v=E-A;
n=[G(2)-F(2);F(1)-G(1);0];
S=A-(((A-F)'*n)/(v'*n))*v
w=S-A;
drawPolygon(@plot3,[A A+1.1*w],'k','linewidth',0.5)
drawPolygon(@plot3,[F S G1],'k','linewidth',0.5)

function Kr(M,A,B);
v=A-M;
w=B-M;
if abs(norm(v)-norm(w))>sqrt(eps)
  error('Fehler in function Kr: A und B nicht gleich weit von M entfernt');
end
v0=v/norm(v);
w0=w/norm(w);
t=degGrid(0,acos(v0'*w0));
K=repmat(M,1,length(t))+repmat(cos(t),3,1).*repmat(v,1,length(t))+repmat(sin(t),3,1).*repmat([-v(2);v(1);0],1,length(t));
drawPolygon(@plot3,K,'k','linewidth',0.5,'erase','xor')
deg=pi/180;
Spitze(K(:,end),-sin(t(end))*v+cos(t(end))*[-v(2);v(1);0],4,30*deg)

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

function t=degGrid(t1,t2,d);
if nargin<3
  d=1;
end
deg=pi/180;
delta=d*deg;
t=linspace(t1,t2,round((t2-t1)/delta));
