%  This mfile plots an arrow with tail at the point (x0, y0) and
%  tip at the point  (x0+a, y0+b).  Writing P = [x0, y0] and 
%  V = [a,b], the the complete call is   arrow(P,V, color,width).
%  The third and the fourth argument, color and width,
%  is optional.  If the call is simply arrow(P,V), the
%  arrow will be black and the size of the arrow is 20% of the length of vector.
%  To get a red arrow and the absolute size 1.5, use  arrow(P,V, 'r',1.5).
%
%  NOTE: The arrow function should be used between 'hold on' and 'hold off'.

function y = arrow(P,V,color,alen)

  l = max(norm(V), eps);

  if nargin < 3
     color = 'b';
     alen = 0.075 * l;
  elseif nargin < 4
     alen = 0.075 * l;
  end

  x0 = P(1); y0 = P(2);
  a = V(1); b = V(2);

  h = l - alen ; v = alen/sqrt(3);

  a1 = (a*h -b*v)/l;
  b1 = (b*h +a*v)/l;

  a2 = (a*h +b*v)/l;
  b2 = (b*h -a*v)/l;   

  plot([x0,x0+a,x0+a1,x0+a,x0+a2],[y0,y0+b,y0+b1,y0+b,y0+b2],color)

