function OrthZerlAufg

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

clc;
close('all')
global ax
ax=[inf -inf inf -inf];
deg=pi/180;
v=[2;3];
phi=30*deg;
a=ceil([cos(phi) -sin(phi);sin(phi) cos(phi)]*v);
b=[-a(1);a(2)];
c=2*v;
d=[-v(2);v(1)];
e=1.5*d;
A=[a b c d e];
v0=v/norm(v);
P=v0*v0'*A;
Q=(eye(2)-v0*v0')*A;
h=0.3;
ax=[floor(min([A(1,:) P(1,:) Q(1,:)])) ceil(max([A(1,:) P(1,:) Q(1,:)])) ...
  floor(min([A(2,:) P(2,:) Q(2,:)])) ceil(max([A(2,:) P(2,:) Q(2,:)])) ]+[-1 1 -1 1];
[X,Y]=meshgrid(floor(ax(1)):ceil(ax(2)),floor(ax(3)):ceil(ax(4)));
plot(X,Y,X',Y','color',0.75*[1 1 1],'linewidth',2.0);
hold('on')
plot(ax(1:2),[0 0],'k',[0 0],ax(3:4),'k','linewidth',1.0)
Spitze([ax(2);0],[1;0],h,45*pi/180)
Schrift([ax(2);0],'\itx','HorizontalAlignment','left','VerticalAlignment','middle');
Spitze([0;ax(4)],[0;1],h,45*pi/180)
Schrift([0;ax(4)+h/2],'\ity','HorizontalAlignment','center','VerticalAlignment','bottom');
zStr='';
for k=1:size(A,2)
  drawPolygon(@plot,[[0;0] A(:,k)],'k')
  Spitze(A(:,k),A(:,k),h,30*deg);
  s=char(abs('a')+k-1);
  if A(1,k)>0
    Schrift(A(:,k),['\bf\it' s '_{ }'],'rotation',atan((A(2,k)/A(1,k)))/deg-90,'HorizontalAlignment','right','VerticalAlignment','top');
  else
    Schrift(A(:,k),['\bf\it' s '_{ }'],'rotation',atan((A(2,k)/A(1,k)))/deg+90,'HorizontalAlignment','right','VerticalAlignment','top');
  end
  drawPolygon(@plot,[[0;0] P(:,k)],'k')
  sP=['{\itProj_{\bfv}}({\it\bf' s '})'];
  if norm(P(:,k))>sqrt(eps)
    if norm(P(:,k)-A(:,k))<sqrt(eps)
      sP=['=' sP];
      vA='top';
    else
      Spitze(P(:,k),P(:,k),h/2,60*deg);
      vA='middle';
    end
    Schrift(P(:,k),['_{ }' sP],'rotation',atan(v(2)/v(1))/deg-90,...
      'HorizontalAlignment','left','VerticalAlignment',vA);
  else
    zStr=[zStr sP '_{ }=_{ }'];
  end
  drawPolygon(@plot,[P(:,k) A(:,k)],'k')
  sQ=['{\itOrth_{\bfv}}({\it\bf' s '})'];
  if norm(Q(:,k))>sqrt(eps)
    if norm(Q(:,k)-A(:,k))<sqrt(eps)
      sQ=['=' sQ];
    else
      Spitze(A(:,k),Q(:,k),h/2,60*deg);
    end
    Schrift(A(:,k),['_{ }' sQ],'rotation',atan(v(2)/v(1))/deg,...
      'HorizontalAlignment','left','VerticalAlignment','top');
  else
    zStr=[zStr sQ '_{ }=_{ }'];
  end
end
zStr=[zStr '\it\bfo'];
eq=ceil(mean((find(zStr=='='))));
drawPolygon(@plot,[0;0],'.k','MarkerSize',30);
Schrift([h/2;0],{zStr(1:eq-1) zStr(eq:end)},'HorizontalAlignment','left','VerticalAlignment','top');
Spitze(v,v,h,30*deg);
drawPolygon(@plot,[[0;0] v],'k','linewidth',2.0)
Schrift(v,'\bf\itv^{ }','rotation',atan((v(2)/v(1)))/deg-90,'HorizontalAlignment','right','VerticalAlignment','top');
axis('equal')
hold('off')
axis('off')
print('-depsc','OrthZerlAufg')

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