function FlaecheGaussX

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

clc;
close('all')
global ax
deg=pi/180;
nEck=inline('repmat(r,2,1).*[cos(t);sin(t)]','r','t');
ax=[inf -inf inf -inf];
t=linspace(0,2*pi,12);
Q=nEck(3+cos(5.2*t),t);
h=0.2;
P=h*round(Q(:,[1:3 5 4 6:end])/h);
drawPolygon(@fill,P(:,[1:end 1]),0.875*[1 1 1],'linewidth',1.5)
hold('on')

A=[0;0]; v=[1;0]; v=v/norm(v);
AAA=repmat(A,1,length(t));
[s I]=sort(v'*(P-AAA));
Pg=AAA+v*s;
drawPolygon(@plot,[Pg(:,1)-0.2*v Pg(:,end)+0.4*v],'k')
for k=1:length(s)
  drawPolygon(@plot,[P(:,I(k)) Pg(:,k)],'k')
end

kBsp=5;
Schrift(P(:,I(kBsp)+1),'{\itP}_{{\itk}+1 }','HorizontalAlignment','right','VerticalAlignment','bottom');
Schrift(P(:,I(kBsp))+0.05*(P(:,I(kBsp)+1)-P(:,I(kBsp))),'\itP_k','HorizontalAlignment','left','VerticalAlignment','bottom');
Q=A+v*v'*(P(:,I(kBsp)  )-A);
R=A+v*v'*(P(:,I(kBsp)+1)-A);
Schrift((Q+R)/2-[0;0.075],'_{  }|{\itx}_{{\itk}+1}-{\itx_k}|','HorizontalAlignment','center','VerticalAlignment','top','Rotation',Rot(Q,R))
Schrift((Q+P(:,I(kBsp)  ))/2+[0.075;0],'_{ }\ity_k','HorizontalAlignment','left','VerticalAlignment','middle')
w=180+atan2(P(2,I(kBsp)+1)-P(2,I(kBsp)),P(1,I(kBsp)+1)-P(1,I(kBsp)))/deg
Schrift((R+P(:,I(kBsp)+1))/2+[0.075;0],'_{ }{\ity}_{{\itk}+1}','Rotation',w/2,...
  'HorizontalAlignment','left','VerticalAlignment','middle')
PolygonM([Q P(:,I(kBsp)) P(:,I(kBsp)+1) R Q],0.2,'ok','MarkerSize',7)
Spitze([ax(2);0],[1;0],0.2,45*pi/deg);
Schrift([ax(2);0],'\itx','HorizontalAlignment','left','VerticalAlignment','middle');

hold('off')
axis('equal')
axis(ax)
axis('off')
print('-deps','FlaecheGaussX')

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 PolygonM(P,d,varargin)
for k=2:size(P,2)
  v=P(:,k)-P(:,k-1);
  t=linspace(0,1,round(norm(v)/d));
  drawPolygon(@plot,repmat(P(:,k-1),1,length(t))+repmat(t,length(P(:,k-1)),1).*repmat(v,1,length(t)),varargin{:})
end

function Schrift(P,txt,varargin)
text(P(1),P(2),txt,'FontName','TimesNewRoman','FontSize',30,varargin{:});

function r=Rot(a,b)
deg=pi/180;
r=atan2(b(2)-a(2),b(1)-a(1))/deg;
if r<0
  r=r+180;
end
if r>90
  r=r-180;
end

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
