comparison scripts/plot/quiver.m @ 7120:a2174fb073d4

[project @ 2007-11-07 21:26:43 by jwe]
author jwe
date Wed, 07 Nov 2007 21:26:43 +0000
parents
children c0d9ac299176
comparison
equal deleted inserted replaced
7119:d22ad51b9cf8 7120:a2174fb073d4
1 ## Copyright (C) 2007 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} quiver (@var{u}, @var{v})
21 ## @deftypefnx {Function File} {} quiver (@var{x}, @var{y}, @var{u}, @var{v})
22 ## @deftypefnx {Function File} {} quiver (@dots{}, @var{s})
23 ## @deftypefnx {Function File} {} quiver (@dots{}, @var{style})
24 ## @deftypefnx {Function File} {} quiver (@dots{}, 'filled')
25 ## @deftypefnx {Function File} {} quiver (@var{h}, @dots{})
26 ## @deftypefnx {Function File} {@var{h} =} quiver (@dots{})
27 ##
28 ## Plot the @code{(@var{u}, @var{v})} components of a vector field in
29 ## an @code{(@var{x}, @var{y})} meshgrid. If the grid is uniform, you can
30 ## specify @var{x} and @var{y} as vectors.
31 ##
32 ## If @var{x} and @var{y} are undefined they are assumed to be
33 ## @code{(1:@var{m}, 1:@var{n})} where @code{[@var{m}, @var{n}] =
34 ## size(@var{u})}.
35 ##
36 ## The variable @var{s} is a scalar defining a scaling factor to use for
37 ## the arrows of the field relative to the mesh spacing. A value of 0
38 ## disables all scaling. The default value is 1.
39 ##
40 ## The style to use for the plot can be defined with a line style @var{style}
41 ## in a similar manner to the line styles used with the @code{plot} command.
42 ## If a marker is specified then markers at the grid points of the vectors are
43 ## printed rather than arrows. If the argument 'filled' is given then the
44 ## markers as filled.
45 ##
46 ## The optional return value @var{h} provides a list of handles to the
47 ## the parts of the vector field (body, arrow and marker).
48 ##
49 ## @group
50 ## @example
51 ## [x,y] = meshgrid(1:2:20);
52 ## quiver(x,y,sin(2*pi*x/10),sin(2*pi*y/10))
53 ## @end example
54 ## @end group
55 ##
56 ## @seealso{plot}
57 ## @end deftypefn
58
59 function retval = quiver (varargin)
60 if (nargin < 2)
61 print_usage ();
62 elseif (isscalar (varargin{1}) && ishandle (varargin{1}))
63 h = varargin {1};
64 if (! strcmp (get (h, "type"), "axes"))
65 error ("quiver: expecting first argument to be an axes object");
66 endif
67 oldh = gca ();
68 unwind_protect
69 axes (h);
70 newplot ();
71 tmp = __quiver__ (h, varargin{2:end});
72 unwind_protect_cleanup
73 axes (oldh);
74 end_unwind_protect
75 else
76 newplot ();
77 tmp = __quiver__ (gca (), varargin{:});
78 endif
79
80 if (nargout > 0)
81 retval = tmp;
82 endif
83 endfunction
84
85 function hlist = __quiver__ (varargin)
86 h = varargin {1};
87
88 s = 1;
89 arrowsize = 0.33;
90
91 firstnonnumeric = Inf;
92 for i = 2 : nargin
93 if (!isnumeric (varargin {i}))
94 firstnonnumeric = i;
95 break;
96 endif
97 endfor
98
99 if (nargin < 5 || firstnonnumeric < 5)
100 u = varargin{2};
101 v = varargin{3};
102 if (nargin == 4 && isnumeric (varargin{4}) && isscalar (varargin{4}))
103 s = varargin{4};
104 iarg = 5;
105 else
106 iarg = 4;
107 endif
108 [x, y] = meshgrid (1 : size(u,1), 1 : size(u,2));
109 else
110 x = varargin{2};
111 y = varargin{3};
112 u = varargin{4};
113 v = varargin{5};
114 if (isvector(x) && isvector(y) && (!isvector (u) || !isvector (v)))
115 [x, y] = meshgrid (x, y);
116 endif
117 if (nargin > 5 && isnumeric (varargin{6}) && isscalar (varargin{6}))
118 s = varargin{6};
119 iarg = 7;
120 else
121 iarg = 6;
122 endif
123 endif
124
125 have_filled = false;
126 have_line_spec = false;
127 while (iarg <= nargin)
128 arg = varargin {iarg++};
129 if (ischar (arg) && strncmp (tolower (arg), "filled", 6))
130 have_filled = true;
131 elseif ((isstr (arg) || iscell (arg))
132 && ! have_line_spec)
133 [linespec, valid] = __pltopt__ ("quiver", arg, false);
134 if (valid)
135 have_line_spec = true;
136 if (strncmp (linespec.linestyle, "none", 4))
137 linespec.linestyle = "-";
138 endif
139 else
140 error ("quiver: invalid linespec");
141 endif
142 else
143 error ("quiver: unrecognized argument");
144 endif
145 endwhile
146
147 if (s)
148 ## Scale the arrows to fit in the grid
149 dx = (max(x(:)) - min(x(:))) ./ size (x, 2);
150 dy = (max(y(:)) - min(y(:))) ./ size (y, 1);
151 len = max (sqrt (u(:).^2 + dy(:).^2));
152 if (len > 0)
153 s = s / sqrt (2) * sqrt (dx.^2 + dy.^2) / len;
154 u = s * u;
155 v = s * v;
156 endif
157 endif
158
159 x = x(:);
160 y = y(:);
161 xend = x + u(:);
162 yend = y + v(:);
163
164 hstate = get (h, "nextplot");
165 unwind_protect
166 if (have_line_spec)
167 h1 = plot ([x.'; xend.'; NaN(1, length (x))](:),
168 [y.'; yend.'; NaN(1, length (y))](:),
169 "linestyle", linespec.linestyle);
170 else
171 h1 = plot ([x.'; xend.'; NaN(1, length (x))](:),
172 [y.'; yend.'; NaN(1, length (y))](:));
173 endif
174 hold on;
175
176 xtmp = x + u(:) .* (1 - arrowsize);
177 ytmp = y + v(:) .* (1 - arrowsize);
178 xarrw1 = xtmp + (y - yend) * arrowsize / 3;
179 xarrw2 = xtmp - (y - yend) * arrowsize / 3;
180 yarrw1 = ytmp + (x - xend) * arrowsize / 3;
181 yarrw2 = ytmp - (x - xend) * arrowsize / 3;
182
183 if (have_line_spec)
184 if (isfield (linespec, "marker") &&
185 ! strncmp (linespec.marker, "none", 4))
186 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:),
187 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:),
188 "linestyle", "none");
189 else
190 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:),
191 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:),
192 "linestyle", linespec.linestyle);
193 endif
194 else
195 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:),
196 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:));
197 endif
198
199 if (! have_line_spec || (isfield (linespec, "marker") &&
200 strncmp (linespec.marker, "none", 4)))
201 h3 = plot (x, y, "linestyle", "none", "marker", "none");
202 else
203 h3 = plot (x, y, "linestyle", "none", "marker", linespec.marker);
204 endif
205 if (have_filled)
206 ## FIXME gnuplot doesn't respect the markerfacecolor field
207 set(h3, "markerfacecolor", get (h1, "color"));
208 endif
209 unwind_protect_cleanup
210 set (h, "nextplot", hstate);
211 end_unwind_protect
212
213 hlist = [h1; h2; h3];
214 endfunction
215
216 %!demo
217 %! [x,y] = meshgrid(1:2:20);
218 %! quiver(x,y,sin(2*pi*x/10),sin(2*pi*y/10))
219
220 %!demo
221 %! axis("equal");
222 %! x=linspace(0,3,80); y=sin(2*pi*x); theta=2*pi*x+pi/2;
223 %! quiver(x,y,sin(theta)/10,cos(theta)/10);
224 %! hold on; plot(x,y,"r"); hold off;