%  Zeichnen eines Scheiben-Modells
%
%  Input:     xy   -  Knotenkoordinaten                      
%             km   -  Koinzidenzmatrix                          
%             kr   -  Randbedingungen                           
%             bk   -  Knotenlasten
%             adds -  String (optional) = k zeichnet Knotennummern,
%                                         e zeichnet Elementnummern

function draw2Dnet (xy , km , kr , bk , adds)

[nk,nn] = size (xy) ;
[ne,ke] = size (km) ;

x = zeros (ke,1) ;
y = zeros (ke,1) ;

if ke == 3                      % Der Vektor seq legt fest,
   seq = [1 2 3] ;              % in welcher Reihenfolge die
elseif ke == 4                  % Punkte eines Elements als
   seq = [1 2 4 3] ;            % Polygon gezeichnet werden.
elseif ke == 6
   seq = [1 4 2 5 3 6] ;
elseif ke == 8
   seq = [1 5 2 8 4 6 3 7] ;
elseif ke == 9
   seq = [1 4 5 2 6 7 3 8 9] ;
else
   error ('Function draw2Dnet: Unbekannter Elementtyp')
end

for i=1:ne
   xmid = 0 ;
   ymid = 0 ;
   for j=1:ke
      k = km(i,seq(j)) ;
      x(j) = xy(k,1) ;
      y(j) = xy(k,2) ;
      xmid = xmid + x(j) ;
      ymid = ymid + y(j) ;
   end
   if nargin > 4 & strfind(adds,'f') > 0
      fill (x , y , 'y') ;
   else
      plot (vertcat(x,x(1)) , vertcat(y,y(1))) ;
   end
   if nargin > 4 & strfind(adds,'e') > 0
     text (xmid/ke , ymid/ke , int2str(i) , 'Color' , 'r') ;
   end
   hold on ;
end

% Knoten:
if nargin > 4 & strfind(adds,'o') > 0
   plot (xy(:,1) , xy(:,2) , 'ro') ;
end
if nargin > 4 & strfind(adds,'k') > 0
  for i=1:nk
    text (xy(i,1) , xy(i,2) , int2str(i) , 'Color' , 'b' , 'HorizontalAlignment' , 'Right') ;
  end
end
hold on ;

% Vorbereitung fuer Lager und Kraefte:
minx = min (xy(:,1)) ;
maxx = max (xy(:,1)) ;
miny = min (xy(:,2)) ;
maxy = max (xy(:,2)) ;
ranx = (maxx-minx)*0.2 ;
rany = (maxy-miny)*0.2 ;
lagh = (ranx+rany)/6 ;
maxr = max(ranx,rany) ;
fwid = maxr/12 ;

% Lager:
for i=1:nk
    if (kr(i,2) == 1) 
       fill ([xy(i,1) xy(i,1)+lagh/1.7 xy(i,1)-lagh/1.7], ...
             [xy(i,2) xy(i,2)-lagh xy(i,2)-lagh] , 'b') ;
       hold on ;
       if (kr(i,1) == 0) 
          plot ([xy(i,1)+lagh/1.7 xy(i,1)-lagh/1.7], ...
                [xy(i,2)-lagh*1.2 xy(i,2)-lagh*1.2] , '-k') ;
       end
       hold on ;
    else if (kr(i,1) == 1) 
       fill ([xy(i,1) xy(i,1)-lagh xy(i,1)-lagh], ...
             [xy(i,2) xy(i,2)-lagh/1.7 xy(i,2)+lagh/1.7] , 'b') ;
       hold on ;
       plot ([xy(i,1)-lagh*1.2 xy(i,1)-lagh*1.2], ...
             [xy(i,2)-lagh/1.7 xy(i,2)+lagh/1.7] , '-k') ;
       hold on ;
      end
    end 
end

% Kraefte:
for i=1:nk
    x = xy(i,1) ;
    y = xy(i,2) ;
    if bk(i,1) ~= 0
        if bk(i,1) > 0
           o1 = maxr*0.6 ; o2 = maxr*0.9 ; 
        else
           o1 = -maxr*0.6 ; o2 = -maxr*0.9 ; 
        end
        xx = x - o2 ;
        fill ([xx     xx+o1  xx+o1    xx+o2 xx+o1    xx+o1  xx    ] , ...
              [y+fwid y+fwid y+2*fwid y     y-2*fwid y-fwid y-fwid] , 'm') ;
        hold on ;
    end 
    if bk(i,2) ~= 0
        if bk(i,2) > 0
           o1 = -maxr*0.3 ; o2 = -maxr*0.9 ; 
        else
           o1 = maxr*0.3 ; o2 = maxr*0.9 ; 
        end
        fill ([x x+2*fwid x+fwid x+fwid x-fwid x-fwid x-2*fwid] , ...
              [y y+o1     y+o1   y+o2   y+o2   y+o1   y+o1    ] , 'm') ;
        hold on ;
    end 
end
hold on ;

% Knotennummern:
if nargin > 4 & strfind(adds,'k') ~= []
  for i=1:nk
    text (xy(i,1) , xy(i,2) , int2str(i) , 'Color' , 'b' , 'HorizontalAlignment' , 'Right') ;
  end
end

axis ([minx-ranx maxx+ranx miny-rany maxy+rany]) ;
axis equal ;
