function vectarrowxxl(p0,p1,attr,textannot,maxx,maxy)
%Arrowline 3-D vector plot.
%   vectarrow(p0,p1) plots a line vector with arrow pointing from point p0
%   to point p1. The function can plot both 2D and 3D vector with arrow
%   depending on the dimension of the input. Different plot options are 
%   possible. For 2D vectors textbox annotation are supported.
%
%  Author: Peter Balazs (peter.balazs@oeaw.ac.at)
%
% This file is based on 'vectarrow.m' by Rentian Xiong, found at 
% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=
% 7470&objectType=file
%
% Date: 09/05/2007
%
%   Example:
%       3D vector
%       p0 = [1 2 3];   % Coordinate of the first point p0
%       p1 = [4 5 6];   % Coordinate of the second point p1
%       vectarrow(p0,p1)
%
%       2D vector
%       p0 = [1 2];     % Coordinate of the first point p0
%       p1 = [4 5];     % Coordinate of the second point p1
%       vectarrow(p0,p1)
%
% Copyright : (c) Acoustics Research Institute, Austrian Academy of
%              Sciences
%              http://www.kfs.oeaw.ac.at


if nargin < 2
    error('Two inputs are needed: x and y coordinates');
end
if nargin < 3
    attr = 'k';
end
if nargin < 4
    textannot = '';
end
if nargin < 5
    maxx = 1;
end
if nargin < 6
    maxy = maxx;
end

if max(size(p0))==3
    if max(size(p1))==3
          x0 = p0(1);
          y0 = p0(2);
          z0 = p0(3);
          x1 = p1(1);
          y1 = p1(2);
          z1 = p1(3);
          plot3([x0;x1],[y0;y1],[z0;z1],attr);   % Draw a line between p0 and p1
          
          p = p1-p0;
          alpha = 0.1/norm(p);  
              % Size of arrow head relative to the length of the vector
              % factor 1/norm(p) tries to make arrow heads equal size
          beta = 0.1;  % Width of the base of the arrow head relative to the length
          
          hu = [x1-alpha*(p(1)+beta*(p(2)+eps)); x1; x1-alpha*(p(1)-beta*(p(2)+eps))];
          hv = [y1-alpha*(p(2)-beta*(p(1)+eps)); y1; y1-alpha*(p(2)+beta*(p(1)+eps))];
          hw = [z1-alpha*p(3);z1;z1-alpha*p(3)];
          
          hold on
          plot3(hu(:),hv(:),hw(:),attr(1),'LineWidth',3)  % Plot arrow head using only the color
          grid on
          xlabel('x')
          ylabel('y')
          zlabel('z')
          axis([-maxx maxx -maxx maxx -maxx maxx]); % 
          hold off
      else
          error('p0 and p1 must have the same dimension')
      end
  elseif max(size(p0))==2
      if max(size(p1))==2
          x0 = p0(1);
          y0 = p0(2);
          x1 = p1(1);
          y1 = p1(2);
          plot([x0;x1],[y0;y1],attr,'LineWidth',3);   % Draw a line between p0 and p1
          
          p = p1-p0;
          alpha = 0.1/norm(p);  
              % Size of arrow head relative to the length of the vector
              % factor 1/norm(p) tries to make arrow heads equal size
          beta = 0.1;  % Width of the base of the arrow head relative to the length
          
          hu = [x1-alpha*(p(1)+beta*(p(2)+eps)); x1; x1-alpha*(p(1)-beta*(p(2)+eps))];
          hv = [y1-alpha*(p(2)-beta*(p(1)+eps)); y1; y1-alpha*(p(2)+beta*(p(1)+eps))];
          hold on
          plot(hu(:),hv(:),attr(1),'LineWidth',3)  % Plot arrow head using only the color
          grid on
          xlabel('x')
          ylabel('y')
          hold off
          axis([-maxx maxx -maxx maxx]); % 
          % put text in graph
          gamma = 0.35; % parameter to shift towards center
          delta = 0.08; % parameter to shift off the line
          xcoord = (x1+maxx)/(2*(maxx+eps))*(1-gamma)+0.5*gamma; % find position of arrow
          ycoord = (y1+maxx)/(2*(maxx+eps))*(1-gamma)+0.5*gamma;
          if abs(xcoord-0.5) < abs(ycoord-0.5) % shift off arrow
              xcoord = xcoord +delta*sign(xcoord-0.5);
          else
              ycoord = ycoord +delta*sign(ycoord-0.5);
          end
          if length(textannot) > 0
             annotation('textbox',[xcoord ycoord 0.05 0.05],'String',textannot,'Edgecolor','none','Fontsize',14);
          end
      else
          error('p0 and p1 must have the same dimension')
      end
  else
      error('this function only accepts 2D or 3D vector')
  end