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

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

[nk,n] = size (xy) ;
[ne,n] = size (km) ;
% Elemente (Staebe):
for i=1:ne
   k1 = km(i,1) ;
   k2 = km(i,2) ;
   plot ([xy(k1,1) xy(k2,1)],[xy(k1,2) xy(k2,2)] , '-ok') ;
   if nargin > 4 & strfind(adds,'e') ~= 0
     text ((xy(k1,1)+xy(k2,1))/2 , (xy(k1,2)+xy(k2,2))/2 , ...
            int2str(i) , 'Color' , 'r') ;
   end
   hold on ;
end

% Knoten:
plot (xy(:,1) , xy(:,2) , 'ro') ;
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 ;
minr = min(ranx,rany)*1.5 ;
fwid = minr/20 ;

% 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 ;
    elseif (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

% Kraefte:
for i=1:nk
    x = xy(i,1) ;
    y = xy(i,2) ;
    if bk(i,1) ~= 0
        if bk(i,1) > 0
           o1 = minr*0.6 ; o2 = minr*0.9 ; 
        else
           o1 = -minr*0.6 ; o2 = -minr*0.9 ; 
        end
        fill ([x      x+o1   x+o1     x+o2 x+o1     x+o1   x     ] , ...
              [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 = -minr*0.3 ; o2 = -minr*0.9 ; 
        else
           o1 = minr*0.3 ; o2 = minr*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

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