function Polkoord

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

clc;
close('all')
deg=pi/180;
h=0.05;

figure('numbertitle','off','name','Polkoord');
[T,R]=meshgrid([0:5:360]*deg,linspace(0,sqrt(2),50));
[X,Y]=pol2cart(T,R);
plot(X,Y,X',Y','color',0.75*[1 1 1])
hold('on')
[m,n]=size(X);
nNull=6;
plot(X(:,nNull),Y(:,nNull),'k','linewidth',2.0)
m0=round(0.65*m);
n0=round(0.35*n);
Spitze([X(m0,n0);Y(m0,n0)],[X(m0,n0);Y(m0,n0)],h,45*deg);
Schrift([X(m0,n0);Y(m0,n0)],'\itP','HorizontalAlignment','center','VerticalAlignment','bottom')
plot(X(1:m0,n0),Y(1:m0,n0),'k')
plot(X(6,round(nNull):n0),Y(6,round(nNull):n0),'k')
hold('off')
axis('equal')
axis([-0.7 1 -0.7 1])
axis('off')
Schrift([X(m0,n0);Y(m0,n0)]/2,'\itr','rotation',atan2(Y(m0,n0),X(m0,n0))/deg+180,...
  'HorizontalAlignment','center','VerticalAlignment','bottom')
Schrift([0;0],'\itt','HorizontalAlignment','center','VerticalAlignment','bottom')
Schrift([0;0],'\itO','HorizontalAlignment','center','VerticalAlignment','top')
alpha=atan2(Y(m,nNull),X(m,nNull));
Schrift([cos(alpha);sin(alpha)]/sqrt(2),{'Nullrichtung' '(Polarachse)'},'FontSize',24,'Rotation',alpha/deg,...
  'HorizontalAlignment','center','VerticalAlignment','top')
print('-depsc','Polkoord');

global ax
ax=[-0.2 1 -0.2 1];
O=[0;0];

figure('numbertitle','off','name','Mathematische Orientierung');
[T,R]=meshgrid([0:5:360]*deg,0:0.05:max(abs(ax(1:2)))*sqrt(2));
[X,Y]=pol2cart(T,R);
plot(X,Y,X',Y','color',0.75*[1 1 1])
hold('on')
drawPolygon(@plot,[[ax(1);0] [ax(2);0]],'k');
drawPolygon(@plot,[[0;0] [ax(2);0]],'k','linewidth',2.0);
Spitze([ax(2);0],[1;0],h,45*deg)
drawPolygon(@plot,[[0;ax(3)] [0;ax(4)]],'k');
Schrift([ax(2);0],'\itx','HorizontalAlignment','right','VerticalAlignment','bottom');
Spitze([0;ax(4)],[0;1],h,45*deg)
Schrift([0;ax(4)+h/2],' \ity','HorizontalAlignment','left','VerticalAlignment','top');
t=50*deg;
r=1.1;
P=r*[cos(t);sin(t)];
Schrift(P,'{\itP}({\itp}_1,{\itp}_2)','HorizontalAlignment','left','VerticalAlignment','middle');
drawPolygon(@plot,[[P(1);0] [0;P(2)]],'+k')
Schrift([P(1);0],'{\itp}_1','HorizontalAlignment','center','VerticalAlignment','top');
Schrift([0;P(2)],'{\itp}_{2 }','HorizontalAlignment','right','VerticalAlignment','middle');
Spitze([0;0],[-1;0],h/1.2,45*deg)
Spitze([P(1);0],[1;0],h/1.2,45*deg)
Schrift([P(1)/2;0],'{\itr} cos({\itt})','FontSize',30,'HorizontalAlignment','center','VerticalAlignment','bottom');
Spitze([P(1);0],[0;-1],h/1.2,45*deg)
Spitze([P(1);P(2)],[0;1],h/1.2,45*deg)
Schrift([P(1);P(2)/2;0],'{\itr} sin({\itt})','rotation',90,...
  'FontSize',30,'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(P/2,'\itr','rotation',t/deg,...
  'HorizontalAlignment','center','VerticalAlignment','bottom')
Schrift([1.3*h;0],'\itt','HorizontalAlignment','left','VerticalAlignment','bottom')
drawPolygon(@plot,[[P(1);0] P [0;P(2)]],'k')
drawPolygon(@plot,[O P],'k')
Kreisbogen(3*[h;0],O,3*h*[cos(t);sin(t)],0,'k','linewidth',1.0)
Spitze(3*h*[cos(t);sin(t)],[-sin(t);cos(t)],0.75*h,45*pi/180)
hold('off')
axis('off')
axis('equal')
axis(ax)
print('-depsc','Polkoord-Math')

figure('numbertitle','off','name','Geodätische Orientierung');
plot(X,Y,X',Y','color',0.75*[1 1 1])
hold('on')
drawPolygon(@plot,[[ax(1);0] [ax(2);0]],'k');
Spitze([ax(2);0],[1;0],h,45*deg)
drawPolygon(@plot,[[0;ax(3)] [0;ax(4)]],'k');
drawPolygon(@plot,[[0;0] [0;ax(4)]],'k','linewidth',2.0);
Schrift([ax(2);0],'_{ }\ity','HorizontalAlignment','right','VerticalAlignment','bottom');
Spitze([0;ax(4)],[0;1],h,45*deg)
Schrift([0;ax(4)+h/2],' \itx','HorizontalAlignment','left','VerticalAlignment','top');
Schrift(P,'{\itP}({\itp}_1,{\itp}_2)','HorizontalAlignment','left','VerticalAlignment','middle');
drawPolygon(@plot,[[P(1);0] [0;P(2)]],'+k')
Schrift([P(1);0],'{\itp}_2','HorizontalAlignment','center','VerticalAlignment','top');
Schrift([0;P(2)],'{\itp}_{1 }','HorizontalAlignment','right','VerticalAlignment','middle');
Spitze([0;P(2)],[-1;0],h/1.2,45*deg)
Spitze([P(1);P(2)],[1;0],h/1.2,45*deg)
Schrift([P(1)/2;P(2)],'{\itr} sin({\itt})','FontSize',30,'HorizontalAlignment','center','VerticalAlignment','bottom');
Spitze([0;0],[0;-1],h/1.2,45*deg)
Spitze([0;P(2)],[0;1],h/1.2,45*deg)
Schrift([0;P(2)/2;0],'{\itr} cos({\itt})','rotation',90,...
  'FontSize',30,'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(P/2,'\itr','rotation',t/deg,...
  'HorizontalAlignment','center','VerticalAlignment','bottom')
Schrift([0.2;0.5]*h,'\itt','HorizontalAlignment','left','VerticalAlignment','bottom')
drawPolygon(@plot,[[P(1);0] P [0;P(2)]],'k')
drawPolygon(@plot,[O P],'k')
Kreisbogen(3*h*[cos(t);sin(t)],O,3*[0;h],0,'k','linewidth',1.0)
Spitze(3*h*[cos(t);sin(t)],[sin(t);-cos(t)],0.75*h,45*pi/180)
hold('off')
axis('off')
axis('equal')
axis(ax)
print('-depsc','Polkoord-Geod')

ax=[-1.1 0.3 -1.1 0.3];

figure('numbertitle','off','name','(-1,-1)');
[T,R]=meshgrid([0:5:360]*deg,0:0.05:max(abs(ax(1:2)))*sqrt(2));
[X,Y]=pol2cart(T,R);
plot(X,Y,X',Y','color',0.75*[1 1 1])
hold('on')
drawPolygon(@plot,[[ax(1);0] [ax(2);0]],'k');
drawPolygon(@plot,[[0;0] [ax(2);0]],'k','linewidth',2.0);
Spitze([ax(2);0],[1;0],h,45*deg)
drawPolygon(@plot,[[0;ax(3)] [0;ax(4)]],'k');
Schrift([ax(2);0],'\itx','HorizontalAlignment','left','VerticalAlignment','middle');
Spitze([0;ax(4)],[0;1],h,45*deg)
Schrift([0;ax(4)+h/2],'\ity','HorizontalAlignment','center','VerticalAlignment','bottom');
t=225*deg;
r=1;
P=r*[cos(t);sin(t)];
Schrift(P,'{\itP}(-1,-1)','HorizontalAlignment','center','VerticalAlignment','top');
drawPolygon(@plot,[[P(1);0] [0;P(2)]],'+k')
Schrift([P(1);0],'-1','HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift([0;P(2)],'_{ }-1','HorizontalAlignment','left','VerticalAlignment','middle');
Schrift([0;0],'225^o','rotation',22.5,'HorizontalAlignment','center','VerticalAlignment','bottom')
drawPolygon(@plot,[[P(1);0] P [0;P(2)]],'k')
drawPolygon(@plot,[O P],'k')
Kreisbogen(4*[h;0],O,4*h*[cos(t);sin(t)],0,'k','linewidth',2.0)
Spitze(4*h*[cos(t);sin(t)],[-sin(t);cos(t)],1.5*h,45*pi/180)
hold('off')
axis('off')
axis('equal')
axis(ax)
print('-depsc','Polkoord-11')

ax=[-9 9 -5 7];
h=0.25;

figure('numbertitle','off','name','Aufgabe');
x=[3 8 0 -8 -7 -4  0  3];
y=[0 6 5  6  0 -3 -4 -4];
[t,r]=cart2pol(x,y);
t=t+2*pi*(t<0);
[X,Y]=meshgrid(ax(1):ax(2),ax(3):ax(4));
plot(X,Y,X',Y','color',0.75*[1 1 1])
hold('on')
drawPolygon(@plot,[[ax(1);0] [ax(2);0]],'color',0.5*[1 1 1]);
Spitze([ax(2);0],[1;0],h,45*deg)
drawPolygon(@plot,[[0;ax(3)] [0;ax(4)]],'color',0.5*[1 1 1]);
Schrift([ax(2);0],'\itx','FontSize',15,'HorizontalAlignment','left','VerticalAlignment','middle');
Spitze([0;ax(4)],[0;1],h,45*deg)
Schrift([0;ax(4)+h/2],'\ity','FontSize',15,'HorizontalAlignment','center','VerticalAlignment','bottom');
for k=1:length(x)
  drawPolygon(@plot,[0 x(k);0 y(k)],'k','linewidth',2.0);
  if x(k)>0
    H='left';
  elseif x(k)<0
    H='right';
  else
    H='center';
  end
  if y(k)<0
    V='top';
  else
    V='bottom';
  end
  Schrift([x(k);y(k)],sprintf('(%d,%d)',x(k),y(k)),'FontSize',15,'HorizontalAlignment',H,'VerticalAlignment',V)
  if x(k)>=0
    H='right';
  else
    H='left';
  end
  if abs(t(k)/deg-round(t(k)/deg))<sqrt(eps)
    n='0';
  else
    n='4';
  end
  [td,tm,ts]=rad2dms(t(k));
  if (tm==0) & (ts==0)
    Schrift((r(k)-0.5)*[cos(t(k));sin(t(k))],{sprintf('{\\itt}=%.0fº',td) sprintf('{\\itr}=%g^{ }',r(k))},...
      'rotation',t(k)/deg-180*(x(k)<0),'FontSize',15,'HorizontalAlignment',H,'VerticalAlignment','middle')
  else
    Schrift(r(k)*[cos(t(k));sin(t(k))],{sprintf('{\\itt}=%.0fº%02.0f''%02.0f''''',td,tm,ts) sprintf('{\\itr}=%g^{ }',r(k))},...
      'rotation',t(k)/deg-180*(x(k)<0),'FontSize',15,'HorizontalAlignment',H,'VerticalAlignment','middle')
  end
  Kreisbogen(0.65*(k+1)*h*[1;0],[0;0],0.65*(k+1)*h*[cos(t(k));sin(t(k))],0,'k')
  if t(k)
    Spitze(0.65*(k+1)*h*[cos(t(k));sin(t(k))],[-sin(t(k));cos(t(k))],0.75*h,45*deg)
  end
end
plot([x;zeros(size(x))],[y;zeros(size(y))],'k')
hold('off')
axis('off')
axis('equal')
axis(ax)
print('-depsc','Polkoord-Aufg')

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',36,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{:});

function [d,m,s]=rad2dms(rad)
D=rad/pi*180;
d=fix(D);
M=(D-d)*60;
m=fix(M);
s=(M-m)*60;
