7120
|
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 ## |
7121
|
49 ## @example |
7120
|
50 ## @group |
7121
|
51 ## [x, y] = meshgrid (1:2:20); |
|
52 ## quiver (x, y, sin (2*pi*x/10), sin (2*pi*y/10)); |
|
53 ## @end group |
7120
|
54 ## @end example |
|
55 ## |
|
56 ## @seealso{plot} |
|
57 ## @end deftypefn |
|
58 |
|
59 function retval = quiver (varargin) |
7121
|
60 |
7120
|
61 if (nargin < 2) |
|
62 print_usage (); |
|
63 elseif (isscalar (varargin{1}) && ishandle (varargin{1})) |
|
64 h = varargin {1}; |
|
65 if (! strcmp (get (h, "type"), "axes")) |
|
66 error ("quiver: expecting first argument to be an axes object"); |
|
67 endif |
|
68 oldh = gca (); |
|
69 unwind_protect |
|
70 axes (h); |
|
71 newplot (); |
|
72 tmp = __quiver__ (h, varargin{2:end}); |
|
73 unwind_protect_cleanup |
|
74 axes (oldh); |
|
75 end_unwind_protect |
|
76 else |
|
77 newplot (); |
|
78 tmp = __quiver__ (gca (), varargin{:}); |
|
79 endif |
|
80 |
|
81 if (nargout > 0) |
|
82 retval = tmp; |
|
83 endif |
7121
|
84 |
7120
|
85 endfunction |
|
86 |
|
87 function hlist = __quiver__ (varargin) |
|
88 h = varargin {1}; |
|
89 |
|
90 s = 1; |
|
91 arrowsize = 0.33; |
|
92 |
|
93 firstnonnumeric = Inf; |
7121
|
94 for i = 2:nargin |
|
95 if (! isnumeric (varargin {i})) |
7120
|
96 firstnonnumeric = i; |
|
97 break; |
|
98 endif |
|
99 endfor |
|
100 |
|
101 if (nargin < 5 || firstnonnumeric < 5) |
|
102 u = varargin{2}; |
|
103 v = varargin{3}; |
|
104 if (nargin == 4 && isnumeric (varargin{4}) && isscalar (varargin{4})) |
|
105 s = varargin{4}; |
|
106 iarg = 5; |
|
107 else |
|
108 iarg = 4; |
|
109 endif |
7121
|
110 [x, y] = meshgrid (1:size(u,1), 1:size(u,2)); |
7120
|
111 else |
|
112 x = varargin{2}; |
|
113 y = varargin{3}; |
|
114 u = varargin{4}; |
|
115 v = varargin{5}; |
|
116 if (isvector(x) && isvector(y) && (!isvector (u) || !isvector (v))) |
|
117 [x, y] = meshgrid (x, y); |
|
118 endif |
|
119 if (nargin > 5 && isnumeric (varargin{6}) && isscalar (varargin{6})) |
|
120 s = varargin{6}; |
|
121 iarg = 7; |
|
122 else |
|
123 iarg = 6; |
|
124 endif |
|
125 endif |
|
126 |
|
127 have_filled = false; |
|
128 have_line_spec = false; |
|
129 while (iarg <= nargin) |
|
130 arg = varargin {iarg++}; |
|
131 if (ischar (arg) && strncmp (tolower (arg), "filled", 6)) |
|
132 have_filled = true; |
|
133 elseif ((isstr (arg) || iscell (arg)) |
|
134 && ! have_line_spec) |
|
135 [linespec, valid] = __pltopt__ ("quiver", arg, false); |
|
136 if (valid) |
|
137 have_line_spec = true; |
|
138 if (strncmp (linespec.linestyle, "none", 4)) |
|
139 linespec.linestyle = "-"; |
|
140 endif |
|
141 else |
|
142 error ("quiver: invalid linespec"); |
|
143 endif |
|
144 else |
|
145 error ("quiver: unrecognized argument"); |
|
146 endif |
|
147 endwhile |
|
148 |
|
149 if (s) |
|
150 ## Scale the arrows to fit in the grid |
|
151 dx = (max(x(:)) - min(x(:))) ./ size (x, 2); |
|
152 dy = (max(y(:)) - min(y(:))) ./ size (y, 1); |
|
153 len = max (sqrt (u(:).^2 + dy(:).^2)); |
|
154 if (len > 0) |
|
155 s = s / sqrt (2) * sqrt (dx.^2 + dy.^2) / len; |
|
156 u = s * u; |
|
157 v = s * v; |
|
158 endif |
|
159 endif |
|
160 |
|
161 x = x(:); |
|
162 y = y(:); |
|
163 xend = x + u(:); |
|
164 yend = y + v(:); |
|
165 |
|
166 hstate = get (h, "nextplot"); |
|
167 unwind_protect |
|
168 if (have_line_spec) |
|
169 h1 = plot ([x.'; xend.'; NaN(1, length (x))](:), |
|
170 [y.'; yend.'; NaN(1, length (y))](:), |
|
171 "linestyle", linespec.linestyle); |
|
172 else |
|
173 h1 = plot ([x.'; xend.'; NaN(1, length (x))](:), |
|
174 [y.'; yend.'; NaN(1, length (y))](:)); |
|
175 endif |
|
176 hold on; |
|
177 |
|
178 xtmp = x + u(:) .* (1 - arrowsize); |
|
179 ytmp = y + v(:) .* (1 - arrowsize); |
|
180 xarrw1 = xtmp + (y - yend) * arrowsize / 3; |
|
181 xarrw2 = xtmp - (y - yend) * arrowsize / 3; |
7130
|
182 yarrw1 = ytmp - (x - xend) * arrowsize / 3; |
|
183 yarrw2 = ytmp + (x - xend) * arrowsize / 3; |
7120
|
184 |
|
185 if (have_line_spec) |
|
186 if (isfield (linespec, "marker") && |
|
187 ! strncmp (linespec.marker, "none", 4)) |
|
188 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
189 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:), |
|
190 "linestyle", "none"); |
|
191 else |
|
192 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
193 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:), |
|
194 "linestyle", linespec.linestyle); |
|
195 endif |
|
196 else |
|
197 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
198 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:)); |
|
199 endif |
|
200 |
|
201 if (! have_line_spec || (isfield (linespec, "marker") && |
|
202 strncmp (linespec.marker, "none", 4))) |
|
203 h3 = plot (x, y, "linestyle", "none", "marker", "none"); |
|
204 else |
|
205 h3 = plot (x, y, "linestyle", "none", "marker", linespec.marker); |
|
206 endif |
|
207 if (have_filled) |
|
208 ## FIXME gnuplot doesn't respect the markerfacecolor field |
|
209 set(h3, "markerfacecolor", get (h1, "color")); |
|
210 endif |
|
211 unwind_protect_cleanup |
|
212 set (h, "nextplot", hstate); |
|
213 end_unwind_protect |
|
214 |
|
215 hlist = [h1; h2; h3]; |
7121
|
216 |
7120
|
217 endfunction |
|
218 |
|
219 %!demo |
|
220 %! [x,y] = meshgrid(1:2:20); |
|
221 %! quiver(x,y,sin(2*pi*x/10),sin(2*pi*y/10)) |
|
222 |
|
223 %!demo |
|
224 %! axis("equal"); |
|
225 %! x=linspace(0,3,80); y=sin(2*pi*x); theta=2*pi*x+pi/2; |
|
226 %! quiver(x,y,sin(theta)/10,cos(theta)/10); |
|
227 %! hold on; plot(x,y,"r"); hold off; |