function kotP

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

clc;
deg=pi/180;
close('all')
global ax
P=[0;0;2];
ax=[-3 3 -2 2 0 P(3)];

Name='kotP-3d';
figure('numbertitle','off','name',Name);
drawPolygon(@fill3,[ax([1 2 2 1])
                    ax([3 3 4 4])
                    [0 0 0 0]],0.875*[1 1 1],'EdgeColor','none')
hold('on')
drawPolygon(@plot3,[P [P(1:2);0]],'k','erase','xor')
L=P(3)/7;
Spitze(P,[0;0;1],L,30*deg)
Spitze([P(1:2);0],-[0;0;1],L,30*deg)
Schrift(ax([1;3;5]),'\Pi    ','HorizontalAlignment','right','VerticalAlignment','baseline')
Schrift(P,'\itP','HorizontalAlignment','center','VerticalAlignment','baseline')
Schrift([P(1:2);0],'{\itP}''','HorizontalAlignment','center','VerticalAlignment','top')
Schrift([P(1:2);P(3)/2],'_{ }\ita','HorizontalAlignment','left','VerticalAlignment','middle')
hold('off')
view(-29,14);
axis('equal')
axis([ax(1:5) 0.7*ax(6)])
axis('off')
print('-deps',Name)

Name='kotP-2d';
figure('numbertitle','off','name',Name);
drawPolygon(@plot3,[ax([1 2 2 1 1])
                    ax([3 3 4 4 3])
                    [0 0 0 0 0]],'k')
hold('on')
Schrift(ax([1;3;5]),'\Pi ','HorizontalAlignment','right','VerticalAlignment','baseline')
drawPolygon(@plot3,P,'ok','MarkerSize',10,'erase','xor')
Schrift([P(1:2);0],' {\itP}''({\ita})','HorizontalAlignment','left','VerticalAlignment','middle')
view(2);
M0=[ax(1)+0.1*(ax(2)-ax(1));ax(3)+0.15*(ax(4)-ax(3));0];
drawPolygon(@plot3,[M0 M0+[P(3);0;0]],'-+k')
Schrift(mean([M0 M0+[P(3);0;0]]')','\ita','HorizontalAlignment','center','VerticalAlignment','top')
hold('off')
axis('equal')
axis([ax(1:4)])
axis('off')
print('-deps',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)
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',36,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
