function Hesse2d

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

global ax
ax=[inf -inf inf -inf];
close('all')
deg=pi/180;
A=[0;0];
a=0.5;
b=1.3;
h=0.2;
v=[11;4];
v=v/norm(v);
n=[-v(2);v(1)];
A=A+a*v;

figure('numbertitle','off','name','positiv');
P=A+0.9*(b-a)*v+0.3*n;
drawPolygon(@plot,A,'.k','Markersize',30);
hold('on')
Schrift(A-a*v,'\itg','HorizontalAlignment','center','VerticalAlignment','bottom')
drawPolygon(@plot,[A P],'k');
drawPolygon(@plot,[A A+(a-h)*n],'k');
drawPolygon(@plot,[A+(a-h)*n A+(a+0.25*h)*n],'--k');
Spitze(P,P-A,0.25*h,45*deg);
Spitze(A+(a-h)*n,n,0.25*h,45*deg);
Schrift((A+P)/2,'_{    }{\itP}-{\itP}_0','FontSize',30,'Rotation',atan2(P(2)-A(2),P(1)-A(1))/deg,...
  'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(A+0.5*(a-h)*n,'\it\bfn','Rotation',atan2(v(2),v(1))/deg,'HorizontalAlignment','right','VerticalAlignment','middle');
drawPolygon(@plot,[A-(a+h)*v A+(b+h)*v],'k','linewidth',2.0);
Schrift(A,'{\itP}_{0 }','HorizontalAlignment','center','VerticalAlignment','top')
Schrift(P,'{\itP}','HorizontalAlignment','center','VerticalAlignment','baseline')
Kreisbogen(A+h*(P-A)/norm(P-A),A,A+h*n,0,'k')
Pg=A+v*v'*(P-A);
Schrift(Pg,'{\itP_g}','HorizontalAlignment','center','VerticalAlignment','top')
drawPolygon(@plot,[P Pg],'k');
Spitze(P,n,0.25*h,45*deg);
Schrift((P+Pg)/2-0.125*h*n,'{\itProj}_{\it\bfn}({\bf\itP}-{\bf\itP}_0)','FontSize',30,'Rotation',atan2(v(2),v(1))/deg,...
  'HorizontalAlignment','left','VerticalAlignment','middle');
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','Hesse2d-2')

figure('numbertitle','off','name','null');
P=A+(b-a)*v;
drawPolygon(@plot,A,'.k','Markersize',30);
hold('on')
Schrift(A-a*v,'\itg','HorizontalAlignment','center','VerticalAlignment','bottom')
drawPolygon(@plot,P,'.k','Markersize',30);
drawPolygon(@plot,[A-0.5*h*n P-0.5*h*n],'k');
drawPolygon(@plot,[A A+(a-h)*n],'k');
drawPolygon(@plot,[A+(a-h)*n A+(a+0.25*h)*n],'--k');
Spitze(P-0.5*h*n,v,0.25*h,45*deg);
Spitze(A+(a-h)*n,n,0.25*h,45*deg);
Schrift((A+P)/2-0.5*h*n,'{\itP}-{\itP}_0','FontSize',30,'Rotation',atan2(v(2),v(1))/deg,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(A+0.5*(a-h)*n,'\it\bfn','Rotation',atan2(v(2),v(1))/deg,'HorizontalAlignment','right','VerticalAlignment','middle');
drawPolygon(@plot,[A-(a+h)*v A+(b+h)*v],'k','linewidth',2.0);
Schrift(A,'{\itP}_{0 }','HorizontalAlignment','center','VerticalAlignment','top')
Schrift(P,'{\itP}','HorizontalAlignment','center','VerticalAlignment','bottom')
Kreisbogen(A+h*v,A,A+h*n,0,'k')
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','Hesse2d-1')

figure('numbertitle','off','name','negativ');
P=A+0.9*(b-a)*v-0.3*n;
drawPolygon(@plot,A,'.k','Markersize',30);
hold('on')
Schrift(A-a*v,'\itg','HorizontalAlignment','center','VerticalAlignment','bottom')
drawPolygon(@plot,[A P],'k');
drawPolygon(@plot,[A A+(a-h)*n],'k');
drawPolygon(@plot,[A+(a-h)*n A+(a+0.25*h)*n],'--k');
Spitze(P,P-A,0.25*h,45*deg);
Spitze(A+(a-h)*n,n,0.25*h,45*deg);
Schrift((A+P)/2,'{\itP}-{\itP}_0','FontSize',30,'Rotation',atan2(P(2)-A(2),P(1)-A(1))/deg,...
  'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(A+0.5*(a-h)*n,'\it\bfn','Rotation',atan2(v(2),v(1))/deg,'HorizontalAlignment','right','VerticalAlignment','middle');
drawPolygon(@plot,[A-(a+h)*v A+(b+h)*v],'k','linewidth',2.0);
Schrift(A,'{\itP}_{0 }','HorizontalAlignment','center','VerticalAlignment','top')
Schrift(P,'{\itP}','HorizontalAlignment','center','VerticalAlignment','top')
Kreisbogen(A+h*(P-A)/norm(P-A),A,A+h*n,0,'k')
Pg=A+v*v'*(P-A);
Schrift(Pg,'{\itP_g   }','HorizontalAlignment','center','VerticalAlignment','bottom')
drawPolygon(@plot,[P Pg],'k');
Spitze(P,-n,0.25*h,45*deg);
Schrift((P+Pg)/2-0.125*h*n,'{\itProj}_{\it\bfn}({\bf\itP}-{\bf\itP}_0)','FontSize',30,'Rotation',atan2(v(2),v(1))/deg,...
  'HorizontalAlignment','left','VerticalAlignment','middle');
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','Hesse2d-3')

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