1 ## Copyright (C) 1993-2012 John W. Eaton
3 ## This file is part of Octave.
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.
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.
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/>.
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
27 ## The optional argument @var{fmt} specifies the line format.
29 ## The optional return value @var{h} is a graphics handle to the created plot.
36 function retval = polar (varargin)
38 [h, varargin, nargs] = __plt_get_axis_arg__ ("polar", varargin{:});
50 if (! ischar (varargin{3}))
51 error ("polar: third argument must be a string");
53 tmp = __plr2__ (h, varargin{:});
54 maxr = max (varargin {2} (:));
56 if (ischar (varargin{2}))
57 tmp = __plr1__ (h, varargin{:});
58 if (iscomplex(varargin{1}))
59 maxr = max (imag(varargin{1})(:));
61 maxr = max (varargin{1}(:));
65 tmp = __plr2__ (h, varargin{:}, fmt);
66 maxr = max (varargin {2} (:));
70 tmp = __plr1__ (h, varargin{:}, fmt);
71 if (iscomplex(varargin{1}))
72 maxr = max (imag(varargin{1})(:));
74 maxr = max (varargin{1}(:));
80 set (h, "xlim", [-maxr, maxr], "ylim", [-maxr, maxr],
81 "xaxislocation", "zero", "yaxislocation", "zero",
82 "plotboxaspectratio", [1, 1, 1]);
87 unwind_protect_cleanup
93 function retval = __plr1__ (h, theta, fmt)
99 [nr, nc] = size (theta);
106 theta_i = imag (theta);
109 theta = real (theta);
115 retval = __plr2__ (h, theta, rho, fmt);
119 function retval = __plr2__ (h, theta, rho, fmt)
125 if (any (imag (theta)))
126 theta = real (theta);
129 if (any (imag (rho)))
133 if (isscalar (theta))
135 x = rho * cos (theta);
136 y = rho * sin (theta);
137 retval = __plt__ ("polar", h, x, y, fmt);
139 error ("__plr2__: invalid data for plotting");
141 elseif (isvector (theta))
143 if (length (theta) != length (rho))
144 error ("__plr2__: vector lengths must match");
149 if (rows (theta) == 1)
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);
163 [r_nr, r_nc] = size (rho);
171 error ("__plr2__: vector and matrix sizes must match");
173 x = diag (cos (theta)) * rho;
174 y = diag (sin (theta)) * rho;
175 retval = __plt__ ("polar", h, x, y, fmt);
177 error ("__plr2__: invalid data for plotting");
179 elseif (ismatrix (theta))
181 [r_nr, r_nc] = size (rho);
188 [t_nr, t_nc] = size (theta);
196 error ("__plr2__: vector and matrix sizes must match");
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");
206 x = rho .* cos (theta);
207 y = rho .* sin (theta);
208 retval = __plt__ ("polar", h, x, y, fmt);
210 error ("__plr2__: invalid data for plotting");
213 error ("__plr2__: invalid data for plotting");
221 %! theta = linspace (0, 2*pi, 1000);
222 %! rho = sin (7*theta);
223 %! polar (theta, rho);
227 %! theta = linspace (0, 10*pi, 1000);
228 %! rho = sin (5/4*theta);
229 %! polar (theta, rho);