## Copyright (C) 1996 John W. Eaton
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.

## usage: quiver (u, v, x, y)
##
## Plot the (u,v) components of a vector field in a (x,y) meshgrid.
##
## You can try:
##
##   a = b = 1:10;
##   [x,y] = meshgrid(a,b);
##   u = rand (10,10);
##   v = rand (10,10);
##   quiver(u,v,x,y)
##
## See also: plot, semilogx, semilogy, loglog, polar, mesh, contour,
##           bar, stairs, gplot, gsplot, replot, xlabel, ylabel, title

## Author: Roberto A. F. Almeida (roberto@calvin.ocfis.furg.br)

function quiver (u, v, x, y)

  if (nargin != 4)
    usage ("quiver (u, v, x, y)");
  endif
  
  if ((rows (u) != rows (v)) || (columns (u) != columns (v)))
    error ("u, v, x and y must all have the same size.");
  endif
  
  if ((rows (x) != rows (y)) || (columns (x) != columns (y)))
    error ("u, v, x and y must all have the same size.");
  endif

  if ((rows (u) != rows (x)) || (columns (u) != columns (x)))
    error ("u, v, x and y must all have the same size.");
  endif


  ## Calculating the grid limits.
  
  minx = min (min (x));
  maxx = max (max (x));
  miny = min (min (y));
  maxy = max (max (y));
  
  max_arrow = max ( [max (max (u)) , max (max (v)) , abs (min (min (u))) , abs (min (min (v)))] );
  border = max_arrow * 1.2;
  
  limits = [ minx - border, maxx + border, miny - border, maxy + border ];

  axis (limits);
  
  
  ## Ok, now plot the arrows.
  
  lenx = columns (x);
  leny = rows (y);
  
  gset noarrow;
  gplot 0 title "";
  
  for j = 1:lenx
    for i = 1:leny
      command = sprintf ("gset arrow from %f,%f to %f,%f", x(i,j),y(i,j),x(i,j)+u(i,j),y(i,j)+v(i,j));
      eval (command);
    endfor
  endfor
  
  replot;
      
endfunction
