function Kotrafo2d

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

clc;
close('all')
deg=pi/180;
global ax
ax=[inf -inf inf -inf];
xMax=4.9;
yMax=3.2;
O=[0;0];
OS=[3;1];
P=[3.75;2.5];
phi=50*deg;
xS=[ cos(phi);sin(phi)];
yS=[-sin(phi);cos(phi)];
q=1.4;
Schrift(P,'\itP','HorizontalAlignment','left','VerticalAlignment','bottom');
Schrift([P(1) 0],'{\itp}_1','HorizontalAlignment','center','VerticalAlignment','top');
Schrift([0 P(2)],'{\itp}_2','HorizontalAlignment','right','VerticalAlignment','middle');
hold('on')
drawPolygon(@plot,[[P(1);0] P [0;P(2)]],'--k')
PS=[xS'*(P-OS);yS'*(P-OS)]/q
drawPolygon(@plot,[OS+q*PS(1)*xS P OS+q*PS(2)*yS],'--k')
Schrift(OS+q*PS(1)*xS,'{\itp}_1''','rotation',phi/deg,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(OS+q*PS(2)*yS,'_{ }{\itp}_2''','rotation',phi/deg-90,'HorizontalAlignment','left','VerticalAlignment','bottom');
h=0.1;
xSMax=OS+xS*(xMax-OS(1))/cos(phi);
ySMax=OS+yS*(yMax-OS(2))/cos(phi);
drawPolygon(@plot,[O [xMax;0]],'k')
drawPolygon(@plot,[O [0;yMax]],'k')
drawPolygon(@plot,[OS xSMax],'k')
drawPolygon(@plot,[OS ySMax],'k')
drawPolygon(@plot,[O OS],'k')
drawPolygon(@plot,[O+[0;1] O O+[1;0]],'k','linewidth',1.5)
drawPolygon(@plot,[OS+q*xS OS OS+q*yS],'k','linewidth',1.5)
Ticks(O,[1;0],[1:floor(xMax)],'HorizontalAlignment','center','VerticalAlignment','top');
Ticks(O,[0;1],[1:floor(yMax)],'HorizontalAlignment','right','VerticalAlignment','middle');
Ticks(OS,q*xS,[1:floor((xMax-OS(1))/cos(phi)/q)],'rotation',phi/deg,...
  'HorizontalAlignment','center','VerticalAlignment','top');
Ticks(OS,q*yS,[1:floor((yMax-OS(2))/cos(phi)/q)],'rotation',phi/deg,...
  'HorizontalAlignment','left','VerticalAlignment','middle');
Spitze(OS,OS,h,45*pi/180)
Spitze([xMax;0],[1;0],h,45*pi/180)
Schrift([xMax;0],'_{ }{\itx}_1','HorizontalAlignment','left','VerticalAlignment','middle');
Spitze([1;0],[1;0],h,45*pi/180)
Schrift([0.5;0],'{\it\bfe}_1','HorizontalAlignment','center','VerticalAlignment','top');
Spitze([0;yMax],[0;1],h,45*pi/180)
Schrift([0;yMax],'{\itx}_2','HorizontalAlignment','center','VerticalAlignment','bottom');
Spitze([0;1],[0;1],h,45*pi/180)
Schrift([0;0.5],'{\it\bfe}_2','rotation',90,'HorizontalAlignment','center','VerticalAlignment','bottom');
Spitze(xSMax,xS,h,45*pi/180)
Schrift(O,'\itO','HorizontalAlignment','right','VerticalAlignment','top');
Schrift(OS/2,'{\it O}''-{\it O}','rotation',atan2(OS(2),OS(1))/deg,'HorizontalAlignment','center','VerticalAlignment','bottom');
drawPolygon(@fill,[OS+q*xS [OS(1)+q*xS(1);OS(2)] [OS(1)+q*yS(1);OS(2)] OS+q*yS OS],0.875*[1 1 1],'erase','none')
drawPolygon(@plot,[OS+q*xS [OS(1)+q*xS(1);OS(2)] [OS(1)+q*yS(1);OS(2)] OS+q*yS],'k')
Schrift(xSMax,'_{ }{\itx}_1''','rotation',phi/deg,'HorizontalAlignment','left','VerticalAlignment','middle');
Spitze(OS+q*xS,xS,h,45*pi/180)
Schrift(OS+q*xS/2,'{\it\bfe}_1''','rotation',phi/deg,'HorizontalAlignment','center','VerticalAlignment','top');
Spitze(ySMax,yS,h,45*pi/180)
Schrift(ySMax,'{\itx}_2''','rotation',phi/deg,'HorizontalAlignment','center','VerticalAlignment','bottom');
Spitze(OS+q*yS,yS,h,45*pi/180)
Schrift(OS+q*yS/2,'{\it\bfe}_2''','rotation',phi/deg-90,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(OS,'_{ }\itO''','HorizontalAlignment','center','VerticalAlignment','top');
Schrift(OS,'^{    }\alpha','HorizontalAlignment','left','VerticalAlignment','bottom');
Kreisbogen(OS+[1;0]/3,OS,OS+xS/3,0,'k')
Schrift(OS+q*yS-0.75*h*[0;1],'_{ }\alpha','HorizontalAlignment','left','VerticalAlignment','top');
Kreisbogen(OS+q*yS-[0;1]/3,OS+q*yS,OS+q*yS-yS/3,0,'k')
Schrift(OS+[q*xS(1);0]/2,'{\itq} cos \alpha',...
  'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(OS+[q*xS(1);q*xS(2)/2],'{\itq} sin \alpha','rotation',90',...
  'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(OS+[q*yS(1);0]/2,'{\itq} sin \alpha',...
  'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(OS+[q*yS(1);q*yS(2)/2],'{\itq} cos \alpha','rotation',90',...
  'HorizontalAlignment','center','VerticalAlignment','bottom');
drawPolygon(@plot,[OS P],'k');
Spitze(P,P-OS,h,45*pi/180)
Schrift((P+OS)/2,'{\itP}-{\itO}''','rotation',atan2(P(2)-OS(2),P(1)-OS(1))/deg,...
  'HorizontalAlignment','right','VerticalAlignment','bottom');
hold('off')
axis('off')
axis('equal')
axis(ax+0.5*h*[-1 1 -1 1])
print('-depsc','Kotrafo2d')

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 Ticks(P,e,N,varargin);
for n=N
  drawPolygon(@plot,P+n*e,'ok','Markersize',4);
  Schrift(P+n*e,sprintf('_{  }%d^{  }',n),varargin{:});
end

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