function [A,N] = strut(B,V,F,fr)
%
%   [A,N] = strut(B,V,F,fr)
%    
%      Graphs a two or three-dimensional structure,
%          computes the incidence matrix and determines its stability
%
%  B - 2 column matrix giving the edges
%        the ith row of B says which two nodes are connected by edge i
%  V - matrix whose rows are (x,y) or (x,y,z) coordinates of nodes
%  F - vector of fixed nodes
%  fr - gives fraction of distance along edge to write edge number
%        default fraction is 4/7
%
%  A - incidence matrix for structure
%  N = null(A,'r') - basis for instabilities
%
%   See also STRUTMECH, MOTION, MOTIONS, DYNAM
%

if nargin < 2, error('Not enough input arguments.'); end
if nargin < 3, F = []; end
if nargin < 4, fr = 4/7; end
rad = .08;

nedge = size(B,1);
nvert = size(V,1);

d = size(V,2);

A = zeros(nedge,d*nvert);

clf;

minV = min(V); maxV = max(V);
dV = max(V) - min(V); dmax = max(dV);
delV = (dmax - dV)/2; 
mm = [minV - delV - .5;maxV + delV + .5];
axis(mm(:));

for i=1:nedge,
  m = B(i,1); n = B(i,2);
  x1 = V(m,:); x2 = V(n,:);
  unit = (x2 - x1)/norm(x2 - x1);
  vm = [d*(m-1)+1:d*m]; vn = [d*(n-1)+1:d*n];
  A(i,vm) = - unit;
  A(i,vn) =   unit;

  linevec(x1,x2);
    xm = (1-fr)*x1 + fr*x2;
  textvec(xm,i,'b');
end

nfix = size(F,2);
fix = fliplr(sort(F));

vs = zeros(1,nvert);
if size(F,2) ~= 0, vs(1,F) = 1; end

for m=nvert:-1:1
  x1 = V(m,:); 
   if vs(m) == 0
    circle(x1,rad);
   else
    vm = [d*(m-1)+1:d*m];
    A(:,vm) = [];
    triangle(x1,rad,'c');
   end
 textvec(x1,m,'r'); 
end

N = null(A,'r');
n = size(N,2);
disp(' ')
if n == 0
  disp('Structure is rigid!')
  disp(' ')
else
  disp('Number of motions =')
  disp(n)
end


