function a_head(x,y,uM,vM,scale,line_thickness,marker_size,head_prop,color)
% a_head(x,y,uM,vM,scale,line_thickness,marker_size,head_prop,color)
%
%plots like squiver, but with arrowheads.  You choose the scale
%
%x,y are mx1 and nx1 vectors and uM, vM are mxn matrices 
% -- or --
%x,y,u,v are all nx1 vectors
%scale sets the length of the lines
%head_prop sets the length of the arrowheads as a fraction of the longest vector
%color is a string to define the color of the arrows (eg: 'g')
%NOTE: hold is OFF after this plot, so put "hold on" again or you'll
%	lose your plot.

[urows,ucols]=size(uM);      
[vrows,vcols]=size(vM);
x=x(:);y=y(:);

n=length(x);
m=length(y);

%first make sure whether it's vectors or matrices:
if ((n~=m) | ((ucols>1) & (urows>1)) | ((vcols>1) & (vrows>1)) )
	%then it's matrices:

	[xM,yM]=meshgrid(x,y);
	lx=reshape((flipud(xM))',m*n,1);
	ly=reshape((flipud(yM))',m*n,1);
	lx=reshape(xM,m*n,1);
	ly=reshape(yM,m*n,1);
	lu=reshape(uM,m*n,1);
	lv=reshape(vM,m*n,1);
	
else %assume vectors
   
   lu=uM(:);lv=vM(:);lx=x(:);ly=y(:);	
   n1 = find(~isnan(uM) & ~isnan(vM));
   lu = lu(n1); lv = lv(n1); lx = lx(n1); ly = ly(n1);
	clear pi
	x=[lx lx+scale*lu]';y=[ly ly+scale*lv]';
	h=plot(x,y,color);set(h,'LineWidth',line_thickness)
	hold on
	h=plot(lx,ly,'g.');set(h,'MarkerSize',marker_size);
	
	
	headx=lx+scale*lu;
	heady=ly+scale*lv;
	uvangle=angle([lu + sqrt(-1)*lv]);% in radians
	
	d_angle=35*pi/180; %+- radians from uvangle to arrowhead angles
	a=sqrt(lu.^2+lv.^2);
   arrowlength=head_prop*max(a);
	angle1=uvangle + pi - d_angle ;
	angle2=uvangle + pi + d_angle;
   
   u1=arrowlength.*cos(angle1);v1=arrowlength.*sin(angle1);
	u2=arrowlength.*cos(angle2);v2=arrowlength.*sin(angle2);
	
	x=[headx  headx+scale*u1]';y=[heady heady+scale*v1]';
	h=plot(x,y,color);set(h,'LineWidth',line_thickness)
	x=[headx  headx+scale*u2]';y=[heady heady+scale*v2]';
   h=plot(x,y,color);set(h,'LineWidth',line_thickness)
end
	
