]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/polar.m
update packages
[CreaPhase.git] / octave_packages / m / plot / polar.m
1 ## Copyright (C) 1993-2012 John W. Eaton
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} {} polar (@var{theta}, @var{rho})
21 ## @deftypefnx {Function File} {} polar (@var{theta}, @var{rho}, @var{fmt})
22 ## @deftypefnx {Function File} {} polar (@var{h}, @dots{})
23 ## @deftypefnx {Function File} {@var{h} =} polar (@dots{})
24 ## Create a two-dimensional plot from polar coordinates @var{theta} and
25 ## @var{rho}.
26 ##
27 ## The optional argument @var{fmt} specifies the line format.
28 ##
29 ## The optional return value @var{h} is a graphics handle to the created plot.
30 ##
31 ## @seealso{plot}
32 ## @end deftypefn
33
34 ## Author: jwe
35
36 function retval = polar (varargin)
37
38   [h, varargin, nargs] = __plt_get_axis_arg__ ("polar", varargin{:});
39
40   if (nargs < 1)
41     print_usage();
42   endif
43
44   oldh = gca ();
45   unwind_protect
46     axes (h);
47     newplot ();
48
49     if (nargs == 3)
50       if (! ischar (varargin{3}))
51         error ("polar: third argument must be a string");
52       endif
53       tmp = __plr2__ (h, varargin{:});
54       maxr = max (varargin {2} (:));
55     elseif (nargs == 2)
56       if (ischar (varargin{2}))
57         tmp = __plr1__ (h, varargin{:});
58         if (iscomplex(varargin{1}))
59           maxr = max (imag(varargin{1})(:));
60         else
61           maxr = max (varargin{1}(:));
62         endif
63       else
64         fmt = "";
65         tmp = __plr2__ (h, varargin{:}, fmt);
66         maxr = max (varargin {2} (:));
67       endif
68     elseif (nargs == 1)
69       fmt = "";
70       tmp = __plr1__ (h, varargin{:}, fmt);
71       if (iscomplex(varargin{1}))
72         maxr = max (imag(varargin{1})(:));
73       else
74         maxr = max (varargin{1}(:));
75       endif
76     else
77       print_usage ();
78     endif
79
80     set (h, "xlim", [-maxr, maxr], "ylim", [-maxr, maxr],
81          "xaxislocation", "zero", "yaxislocation", "zero",
82          "plotboxaspectratio", [1, 1, 1]);
83
84     if (nargout > 0)
85       retval = tmp;
86     endif
87   unwind_protect_cleanup
88     axes (oldh);
89   end_unwind_protect
90
91 endfunction
92
93 function retval = __plr1__ (h, theta, fmt)
94
95   if (nargin != 3)
96     print_usage ();
97   endif
98
99   [nr, nc] = size (theta);
100   if (nr == 1)
101     theta = theta';
102     tmp = nr;
103     nr = nc;
104     nc = tmp;
105   endif
106   theta_i = imag (theta);
107   if (any (theta_i))
108     rho = theta_i;
109     theta = real (theta);
110   else
111     rho = theta;
112     theta = (1:nr)';
113   endif
114
115   retval = __plr2__ (h, theta, rho, fmt);
116
117 endfunction
118
119 function retval = __plr2__ (h, theta, rho, fmt)
120
121   if (nargin != 4)
122     print_usage ();
123   endif
124
125   if (any (imag (theta)))
126     theta = real (theta);
127   endif
128
129   if (any (imag (rho)))
130     rho = real (rho);
131   endif
132
133   if (isscalar (theta))
134     if (isscalar (rho))
135       x = rho * cos (theta);
136       y = rho * sin (theta);
137       retval = __plt__ ("polar", h, x, y, fmt);
138     else
139       error ("__plr2__: invalid data for plotting");
140     endif
141   elseif (isvector (theta))
142     if (isvector (rho))
143       if (length (theta) != length (rho))
144         error ("__plr2__: vector lengths must match");
145       endif
146       if (rows (rho) == 1)
147         rho = rho';
148       endif
149       if (rows (theta) == 1)
150         theta = theta';
151       endif
152       x = rho .* cos (theta);
153       y = rho .* sin (theta);
154       retval = __plt__ ("polar", h, x, y, fmt);
155     elseif (ismatrix (rho))
156       [t_nr, t_nc] = size (theta);
157       if (t_nr == 1)
158         theta = theta';
159         tmp = t_nr;
160         t_nr = t_nc;
161         t_nc = tmp;
162       endif
163       [r_nr, r_nc] = size (rho);
164       if (t_nr != r_nr)
165         rho = rho';
166         tmp = r_nr;
167         r_nr = r_nc;
168         r_nc = tmp;
169       endif
170       if (t_nr != r_nr)
171         error ("__plr2__: vector and matrix sizes must match");
172       endif
173       x = diag (cos (theta)) * rho;
174       y = diag (sin (theta)) * rho;
175       retval = __plt__ ("polar", h, x, y, fmt);
176     else
177       error ("__plr2__: invalid data for plotting");
178     endif
179   elseif (ismatrix (theta))
180     if (isvector (rho))
181       [r_nr, r_nc] = size (rho);
182       if (r_nr == 1)
183         rho = rho';
184         tmp = r_nr;
185         r_nr = r_nc;
186         r_nc = tmp;
187       endif
188       [t_nr, t_nc] = size (theta);
189       if (r_nr != t_nr)
190         theta = theta';
191         tmp = t_nr;
192         t_nr = t_nc;
193         t_nc = tmp;
194       endif
195       if (r_nr != t_nr)
196         error ("__plr2__: vector and matrix sizes must match");
197       endif
198       diag_r = diag (rho);
199       x = diag_r * cos (theta);
200       y = diag_r * sin (theta);
201       retval = __plt__ ("polar", h, x, y, fmt);
202     elseif (ismatrix (rho))
203       if (! size_equal (rho, theta))
204         error ("__plr2__: matrix dimensions must match");
205       endif
206       x = rho .* cos (theta);
207       y = rho .* sin (theta);
208       retval = __plt__ ("polar", h, x, y, fmt);
209     else
210       error ("__plr2__: invalid data for plotting");
211     endif
212   else
213     error ("__plr2__: invalid data for plotting");
214   endif
215
216 endfunction
217
218
219 %!demo
220 %! clf
221 %! theta = linspace (0, 2*pi, 1000);
222 %! rho = sin (7*theta);
223 %! polar (theta, rho);
224
225 %!demo
226 %! clf
227 %! theta = linspace (0, 10*pi, 1000);
228 %! rho = sin (5/4*theta);
229 %! polar (theta, rho);
230