function Winkelorientierung

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

clc;
close('all')
global ax
ax=[inf -inf inf -inf];
deg=pi/180;
a=[3;4]/2.5;
b=[-3;1]/2.5;

figure('numbertitle','off','name','nichtorientiert');
Polygon([a [0;0] b],'k','linewidth',1.5)
hold('on')
h=0.5;
Spitze(a,a,h/2,60*deg);
Spitze(b,b,h/2,60*deg);
Schrift(a/2,'\bf\ita','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(a(2),a(1))/deg)
Schrift(b/2,'\bf\itb','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(b(2),b(1))/deg+180)
Kreisbogen(h*a/norm(a),[0;0],h*b/norm(b),0,'k')
hold('off')
axis('off')
axis('equal')
axis(ax+0.5*[-h h -h h])
print('-depsc','Winkelorientierung-nicht','-loose')

figure('numbertitle','off','name','math.a->b');
Polygon([a [0;0] b],'k','linewidth',1.5)
hold('on')
h=0.5;
Spitze(a,a,h/2,60*deg);
Spitze(b,b,h/2,60*deg);
Schrift(a/2,'\bf\ita','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(a(2),a(1))/deg)
Schrift(b/2,'\bf\itb','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(b(2),b(1))/deg+180)
Kreisbogen(0.7*h*a/norm(a),[0;0],0.7*h*b/norm(b),0,'k')
Spitze(0.7*h*b/norm(b),[-b(2);b(1)],h/3,60*deg)
yS=[min(a(1),b(1))-h;max(a(2),b(2))+h];
xS=[max(a(1),b(1))+h;              -h];
Polygon([yS [yS(1);xS(2)] xS],'k')
Spitze(xS,[1;0],h,45*deg);
Spitze(yS,[0;1],h,45*deg);
Schrift(xS+[0;h/4],'\itx','HorizontalAlignment','right','VerticalAlignment','bottom');
Schrift(yS+[h/2;h/2],'\ity','HorizontalAlignment','left','VerticalAlignment','top');
hold('off')
axis('off')
axis('equal')
axis(ax+0.5*[-h h -h h])
print('-depsc','Winkelorientierung-ab-math','-loose')

figure('numbertitle','off','name','math.b->a');
Polygon([a [0;0] b],'k','linewidth',1.5)
hold('on')
h=0.5;
Spitze(a,a,h/2,60*deg);
Spitze(b,b,h/2,60*deg);
Schrift(a/2,'\bf\ita','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(a(2),a(1))/deg)
Schrift(b/2,'\bf\itb','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(b(2),b(1))/deg+180)
Kreisbogen(0.7*h*b/norm(b),[0;0],0.7*h*a/norm(a),0,'k')
Spitze(0.7*h*a/norm(a),[-a(2);a(1)],h/3,60*deg)
yS=[min(a(1),b(1))-h;max(a(2),b(2))+h];
xS=[max(a(1),b(1))+h;              -h];
Polygon([yS [yS(1);xS(2)] xS],'k')
Spitze(xS,[1;0],h,45*deg);
Spitze(yS,[0;1],h,45*deg);
Schrift(xS+[0;h/4],'\itx','HorizontalAlignment','right','VerticalAlignment','bottom');
Schrift(yS+[h/2;h/2],'\ity','HorizontalAlignment','left','VerticalAlignment','top');
hold('off')
axis('off')
axis('equal')
axis(ax+0.5*[-h h -h h])
print('-depsc','Winkelorientierung-ba-math','-loose')

figure('numbertitle','off','name','geod.a->b');
Polygon([a [0;0] b],'k','linewidth',1.5)
hold('on')
h=0.5;
Spitze(a,a,h/2,60*deg);
Spitze(b,b,h/2,60*deg);
Schrift(a/2,'\bf\ita','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(a(2),a(1))/deg)
Schrift(b/2,'\bf\itb','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(b(2),b(1))/deg+180)
Kreisbogen(0.7*h*b/norm(b),[0;0],0.7*h*a/norm(a),0,'k')
Spitze(0.7*h*b/norm(b),[b(2);-b(1)],h/3,60*deg)
yS=[min(a(1),b(1))-h;max(a(2),b(2))+h];
xS=[max(a(1),b(1))+h;              -h];
Polygon([yS [yS(1);xS(2)] xS],'k')
Spitze(xS,[1;0],h,45*deg);
Spitze(yS,[0;1],h,45*deg);
Schrift(xS+[0;h/4],'\ity','HorizontalAlignment','right','VerticalAlignment','bottom');
Schrift(yS+[h/2;h/2],'\itx','HorizontalAlignment','left','VerticalAlignment','top');
hold('off')
axis('off')
axis('equal')
axis(ax+0.5*[-h h -h h])
print('-depsc','Winkelorientierung-ab-geod','-loose')

figure('numbertitle','off','name','geod.b->a');
Polygon([a [0;0] b],'k','linewidth',1.5)
hold('on')
h=0.5;
Spitze(a,a,h/2,60*deg);
Spitze(b,b,h/2,60*deg);
Schrift(a/2,'\bf\ita','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(a(2),a(1))/deg)
Schrift(b/2,'\bf\itb','HorizontalAlignment','center','VerticalAlignment','bottom','rotation',atan2(b(2),b(1))/deg+180)
Kreisbogen(0.7*h*a/norm(a),[0;0],0.7*h*b/norm(b),0,'k')
Spitze(0.7*h*a/norm(a),[a(2);-a(1)],h/3,60*deg)
yS=[min(a(1),b(1))-h;max(a(2),b(2))+h];
xS=[max(a(1),b(1))+h;              -h];
Polygon([yS [yS(1);xS(2)] xS],'k')
Spitze(xS,[1;0],h,45*deg);
Spitze(yS,[0;1],h,45*deg);
Schrift(xS+[0;h/4],'\ity','HorizontalAlignment','right','VerticalAlignment','bottom');
Schrift(yS+[h/2;h/2],'\itx','HorizontalAlignment','left','VerticalAlignment','top');
hold('off')
axis('off')
axis('equal')
axis(ax+0.5*[-h h -h h])
print('-depsc','Winkelorientierung-ba-geod','-loose')

function Polygon(P,varargin);
global ax
plot(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',60,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));
Polygon(repmat(M,size(t))+r*[cos(t);sin(t)],varargin{:});
