function kotC

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

global FontSize
global ax
ax=[inf -inf inf -inf inf -inf];
close('all');

name='kotC-3d';
FontSize=30;
figure('numbertitle','off','name',name);
ax=[inf -inf inf -inf inf -inf];
hold('on')
A=[0;0;0];
a=2;
R=eye(3);
O=[0;0;0];
deg=pi/180;
B=A+a*R(:,1);
C=B+a*R(:,2);
D=A+a*R(:,2);
S=A+0.5*a*R(:,1)+0.5*a*R(:,2)+0.5*a*tan(32*deg)*R(:,3);
W=[A B C D S];
A=W(:,1); B=W(:,2); C=W(:,3); D=W(:,4); S=W(:,5); 
col=repmat(linspace(0.5,0.75,5)',1,3);
drawPolygon(@fill3,[A B C D],col(1,:),'EdgeColor','none');
drawPolygon(@fill3,[A B S],col(2,:),'EdgeColor','none');
drawPolygon(@fill3,[B C S],col(3,:),'EdgeColor','none');
drawPolygon(@fill3,[C D S],col(4,:),'EdgeColor','none');
drawPolygon(@fill3,[D A S],col(5,:),'EdgeColor','none');
M1=mean([A B]')';
M2=mean([C D]')';
M=mean([M1 M2]')';
v=0.1*(M2-M1);
w=2*(S-M);
drawPolygon(@plot3,[M1-v+w M1-v M1 S M2 M2+v M2+v+w M1-v+w],'k','erase','xor');
Schrift(M2+0.5*(M-M2)+w,'   \Sigma','HorizontalAlignment','center','VerticalAlignment','baseline')
v=0.1*(D-B)/sqrt(2);
drawPolygon(@plot3,[B-v+w B-v B S D D+v D+v+w B-v+w],'k','erase','xor');
Schrift(B+0.5*(M-B)+w,'\Phi   ','HorizontalAlignment','center','VerticalAlignment','baseline')
drawPolygon(@plot3,[S M+w],'k','erase','xor');
hold('off')
view(-56,34)
axis('equal')
axis('off')
axis(ax.*[0.8 1 0.8 1 1 0.5]);
print('-depsc',name)

name='kotC-2d';
FontSize=30;
figure('numbertitle','off','name',name);
ax=[inf -inf inf -inf inf -inf];
hold('on')
A=[0;0;0];
a=2;
R=eye(3);
O=[0;0;0];
deg=pi/180;
B=A+a*R(:,1);
C=B+a*R(:,2);
D=A+a*R(:,2);
S=A+0.5*a*R(:,1)+0.5*a*R(:,2)+0.5*a*tan(32*deg)*R(:,3);
W=[A B C D S];
A=W(:,1); B=W(:,2); C=W(:,3); D=W(:,4); S=W(:,5); 
drawPolygon(@plot3,[A B C D A],'k');
drawPolygon(@plot3,[A S C],'k');
drawPolygon(@plot3,[B S D],'k');
M1=mean([A D]')';
M2=mean([B C]')';
M=mean([M1 M2]')';
v=0.1*(M2-M1);
w=2*(S-M);
drawPolygon(@plot3,[M1-v+w M1-v M1 S M2 M2+v M2+v+w M1-v+w],'k','linewidth',2.0);
Schrift(M1-0.1*(M-M1)+w,'\Sigma''   ','HorizontalAlignment','center','VerticalAlignment','bottom')
v=0.1*(C-A)/sqrt(2);
drawPolygon(@plot3,[A-v+w A-v A S C C+v C+v+w A-v+w],'k','linewidth',2.0);
Schrift(A-0.1*(M-A)+w,'\Phi''  ','HorizontalAlignment','center','VerticalAlignment','baseline')
h=norm(S-M);
v=M+h*R(:,2)-M2;
drawPolygon(@plot3,[M2 M2+1.1*v],'k')
drawPolygon(@plot3,[M+h*R(:,2) M],'k')
bet=pi-acos(v'*R(:,1)/norm(v));
t=degGrid(0,bet);
rW=0.34*a;
K=repmat(M2,1,length(t))+rW*(-repmat(cos(t),3,1).*repmat(R(:,1),1,length(t))+repmat(sin(t),3,1).*repmat(R(:,2),1,length(t)));
drawPolygon(@plot3,K,'k')
Schrift(K(:,1),sprintf('^{ }%.0f^\\circ',bet/deg),'HorizontalAlignment','left','VerticalAlignment','bottom')
Schrift(M+0.5*h*R(:,2),'\ith','HorizontalAlignment','left','VerticalAlignment','middle')
t=degGrid(0,1.25*pi);
K=repmat(M,1,length(t))+h*(repmat(cos(t),3,1).*repmat(R(:,2),1,length(t))-repmat(sin(t),3,1).*repmat(R(:,1),1,length(t)));
drawPolygon(@plot3,K,'k')
Spitze(K(:,end),h*(-sin(t(end))*R(:,2)-cos(t(end))*R(:,1)),0.2,30*deg);
drawPolygon(@plot3,[K(:,end) A],'k')
t=degGrid(0,atan(tan(bet)/sqrt(2)));
v1=(M-A)/norm(M-A);
v2=[v1(2);-v1(1);0];
rW=0.25*a;
K=repmat(A,1,length(t))+rW*(repmat(cos(t),3,1).*repmat(v1,1,length(t))+repmat(sin(t),3,1).*repmat(v2,1,length(t)));
drawPolygon(@plot3,K,'k')
Schrift(K(:,end),'\alpha ','rotation',45-t(end)/deg/2,'HorizontalAlignment','right','VerticalAlignment','bottom')
hold('off')
view(2)
axis('equal')
axis('off')
print('-depsc',name)

name='kotC-2d-A';
FontSize=15;
figure('numbertitle','off','name',name);
ax=[inf -inf inf -inf inf -inf];
hold('on')
A=[0;0;0];
a=2;
R=eye(3);
O=[0;0;0];
deg=pi/180;
B=A+a*R(:,1);
C=B+a*R(:,2);
D=A+a*R(:,2);
S=A+0.5*a*R(:,1)+0.5*a*R(:,2)+0.5*a*tan(32*deg)*R(:,3);
W=[A B C D S];
A=W(:,1); B=W(:,2); C=W(:,3); D=W(:,4); S=W(:,5); 
drawPolygon(@plot3,[A B C D A],'k');
drawPolygon(@plot3,[A S C],'k');
drawPolygon(@plot3,[B S D],'k');
M1=mean([A D]')';
M2=mean([B C]')';
M=mean([M1 M2]')';
v=0.1*(M2-M1);
w=2*(S-M);
drawPolygon(@plot3,[M1-v+w M1-v M1 S M2 M2+v M2+v+w M1-v+w],'k','linewidth',2.0);
Schrift(M1-0.1*(M-M1)+w,'\Sigma''   ','HorizontalAlignment','center','VerticalAlignment','bottom')
v=0.1*(C-A)/sqrt(2);
drawPolygon(@plot3,[A-v+w A-v A S C C+v C+v+w A-v+w],'k','linewidth',2.0);
Schrift(A-0.1*(M-A)+w,'\Phi''  ','HorizontalAlignment','center','VerticalAlignment','baseline')
h=norm(S-M);
v=M+h*R(:,2)-M2;
drawPolygon(@plot3,[M2 M2+1.1*v],'k')
drawPolygon(@plot3,[M+h*R(:,2) M],'k')
bet=pi-acos(v'*R(:,1)/norm(v));
t=degGrid(0,bet);
rW=0.34*a;
K=repmat(M2,1,length(t))+rW*(-repmat(cos(t),3,1).*repmat(R(:,1),1,length(t))+repmat(sin(t),3,1).*repmat(R(:,2),1,length(t)));
drawPolygon(@plot3,K,'k')
Schrift(K(:,1),sprintf('^{ }%.0f^\\circ',bet/deg),'HorizontalAlignment','left','VerticalAlignment','bottom')
drawPolygon(@fill3,[M M2 M+h*R(:,2)],[1 0.75 1])
Schrift((A+B)/2,'\ita','HorizontalAlignment','center','VerticalAlignment','top')
Schrift((M+M2)/2,'{\ita}/2','HorizontalAlignment','center','VerticalAlignment','top')
Schrift((M+M1)/2,'{\ita}/2','HorizontalAlignment','center','VerticalAlignment','top')
Schrift((A+M1)/2,'{\ita}/2 ','HorizontalAlignment','right','VerticalAlignment','middle')
Schrift((A+M)/2,'\itb','HorizontalAlignment','right','VerticalAlignment','baseline')
drawPolygon(@fill3,[A M M1],[1 1 0.75])
Schrift(M+0.5*h*R(:,2),'\ith','HorizontalAlignment','left','VerticalAlignment','middle')
t=degGrid(0,1.25*pi);
K=repmat(M,1,length(t))+h*(repmat(cos(t),3,1).*repmat(R(:,2),1,length(t))-repmat(sin(t),3,1).*repmat(R(:,1),1,length(t)));
drawPolygon(@plot3,K,'k')
Spitze(K(:,end),h*(-sin(t(end))*R(:,2)-cos(t(end))*R(:,1)),0.2,30*deg);
drawPolygon(@plot3,[K(:,end) A],'k')
drawPolygon(@fill3,[A M K(:,end)],[0.75 1 1])
Schrift((K(:,end)+M)/2,'\ith ','HorizontalAlignment','right','VerticalAlignment','middle')
t=degGrid(0,atan(tan(bet)/sqrt(2)));
v1=(M-A)/norm(M-A);
v2=[v1(2);-v1(1);0];
rW=0.25*a;
K=repmat(A,1,length(t))+rW*(repmat(cos(t),3,1).*repmat(v1,1,length(t))+repmat(sin(t),3,1).*repmat(v2,1,length(t)));
drawPolygon(@plot3,K,'k')
Schrift(K(:,end),'\alpha ','rotation',45-t(end)/deg/2,'HorizontalAlignment','right','VerticalAlignment','bottom')
hold('off')
view(2)
axis('equal')
axis('off')
print('-depsc',name)

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)
global FontSize
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',FontSize,varargin{:});

function Spitze(P,v,L,phi);
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');
  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));
