function OrthZerl

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

close('all')
global ax
ax=[inf -inf inf -inf];
deg=pi/180;
A=[0;0];
v=[2;1];
B=A+v;
v0=v/norm(v);
vL=5;
v=vL*v0;
drawPolygon(@plot,[A-0.1*v A+1.1*v],'k');
hold('on')
alpha=50*deg;
aL=3;
phi=atan2(v(2),v(1))+alpha;
a=aL*[cos(phi);sin(phi)];
proj=v0*v0'*a;
orth=a-proj;
drawPolygon(@plot,[A A+proj A+proj+orth A],'k','linewidth',2.0);
drawPolygon(@plot,[B A+v],'k','linewidth',2.0);
h=0.5;
Spitze(A+v,v,h,25*deg);
Spitze(A+a,a,h,25*deg);
Spitze(A+a,orth,h/2,40*deg);
Spitze(A+proj,proj,h/2,40*deg);
Schrift(B+(B-A)/2,'\it\bfv','rotation',atan(v(2)/v(1))/deg,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(A+proj/2,'{\itProj_{\bfv}}({\it\bfa})    ','rotation',atan(v(2)/v(1))/deg,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(A+proj+orth/2,'{\itOrth_{\bfv}}({\it\bfa})','rotation',atan(orth(2)/orth(1))/deg,...
  'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(A+a/2,'\bf\ita','rotation',atan(a(2)/a(1))/deg,'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(A,'^{    }\alpha','FontSize',15,'rotation',atan(v(2)/v(1))/deg,'HorizontalAlignment','left','VerticalAlignment','bottom');
Kreisbogen(A+1.5*h*v/norm(v),A,A+1.5*h*a/norm(a),0,'k');

A(1)=ax(2)+0.5*(ax(2)-ax(1));
vL=norm(v)-norm(proj);
v=[2;1];
B=A-v;
v0=v/norm(v);
v=vL*v0;
drawPolygon(@plot,[B-0.1*v A+1.1*v],'k');
hold('on')
alpha=pi-alpha;
aL=3;
phi=atan2(v(2),v(1))+alpha;
a=aL*[cos(phi);sin(phi)];
proj=v0*v0'*a;
orth=a-proj;
drawPolygon(@plot,[A A+proj A+proj+orth A],'k','linewidth',2.0);
drawPolygon(@plot,[A+0.1*v A+v],'k','linewidth',2.0);
Spitze(A+v,v,h,25*deg);
Spitze(A+a,a,h,25*deg);
Spitze(A+a,orth,h/2,40*deg);
Spitze(A+proj,proj,h/2,40*deg);
Schrift(A+v/2,'\it\bfv','rotation',atan(v(2)/v(1))/deg,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(A+proj/2,'_{      }{\itProj_{\bfv}}({\it\bfa})','rotation',atan(v(2)/v(1))/deg,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(A+proj+orth/2,'{\itOrth_{\bfv}}({\it\bfa})','rotation',atan(orth(2)/orth(1))/deg,...
  'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(A+a/2,'{\bf\ita}','rotation',atan(a(2)/a(1))/deg,'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(A,'^{  }\alpha','FontSize',15,'rotation',atan(v(2)/v(1))/deg,'HorizontalAlignment','left','VerticalAlignment','bottom');
Schrift(A,'\pi-\alpha^{    }','FontSize',15,'rotation',atan(v(2)/v(1))/deg,'HorizontalAlignment','right','VerticalAlignment','bottom');
Kreisbogen(A+2*h*v/norm(v),A,A-2*h*v/norm(v),0,'k');

hold('off')
axis('off')
axis('equal')
axis(ax)
print('-depsc','OrthZerl')

function drawPolygon(draw,P,varargin);
global ax
feval(draw,P(1,:),P(2,:),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

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

function Spitze(P,v,L,phi);
if nargin<=2
  L=1;
end
if nargin<=3
  phi=7.5*pi/180;
end
ta=tan(phi/2);
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
n=[-v(2,:);v(1,:)];
Q=P-v+ta*n;
R=P-v-ta*n;
for k=1:size(P,2);
  fill([P(1,k) Q(1,k) R(1,k)],[P(2,k) Q(2,k) R(2,k)],'k')
end

function Kreisbogen(A,M,B,offset,varargin)
deg=pi/180;
[t1,r1]=cart2pol(A(1)-M(1),A(2)-M(2));
[t2,r2]=cart2pol(B(1)-M(1),B(2)-M(2));
if abs(r1-r2)>sqrt(eps)
  error('Kreisbogen: A und B liegen nicht auf einem Kreis mit Mittelpunkt M')
end
r=(r1+r2)/2;
t1=t1-offset;
t2=t2+offset;
if t2<t1
  t2=t2+2*pi;
end
t=linspace(t1,t2,round((t2-t1)/deg));
drawPolygon(@plot,repmat(M,size(t))+r*[cos(t);sin(t)],varargin{:});
