1########################################################################
2##
3## Copyright (C) 2007-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} cylinder
28## @deftypefnx {} {} cylinder (@var{r})
29## @deftypefnx {} {} cylinder (@var{r}, @var{n})
30## @deftypefnx {} {} cylinder (@var{hax}, @dots{})
31## @deftypefnx {} {[@var{x}, @var{y}, @var{z}] =} cylinder (@dots{})
32## Plot a 3-D unit cylinder.
33##
34## The optional input @var{r} is a vector specifying the radius along the
35## unit z-axis.  The default is [1 1] indicating radius 1 at @code{Z == 0}
36## and at @code{Z == 1}.
37##
38## The optional input @var{n} determines the number of faces around the
39## circumference of the cylinder.  The default value is 20.
40##
41## If the first argument @var{hax} is an axes handle, then plot into this axes,
42## rather than the current axes returned by @code{gca}.
43##
44## If outputs are requested @code{cylinder} returns three matrices in
45## @code{meshgrid} format, such that @code{surf (@var{x}, @var{y}, @var{z})}
46## generates a unit cylinder.
47##
48## Example:
49##
50## @example
51## @group
52## [x, y, z] = cylinder (10:-1:0, 50);
53## surf (x, y, z);
54## title ("a cone");
55## @end group
56## @end example
57## @seealso{ellipsoid, rectangle, sphere}
58## @end deftypefn
59
60function [xx, yy, zz] = cylinder (varargin)
61
62  [hax, args, nargs] = __plt_get_axis_arg__ ("cylinder", varargin{:});
63
64  if (nargs == 0)
65    r = [1, 1];
66    n = 20;
67  elseif (nargs == 1)
68    r = args{1};
69    n = 20;
70  elseif (nargs == 2)
71    r = args{1};
72    n = args{2};
73  else
74    print_usage ();
75  endif
76
77  if (length (r) < 2)
78    error ("cylinder: length (R) must be larger than 2");
79  endif
80
81  phi = linspace (0, 2*pi, n+1);
82  idx = 1:length (r);
83  [phi, idx] = meshgrid (phi, idx);
84  z = (idx - 1) / (length (r) - 1);
85  r = r(idx);
86  [x, y] = pol2cart (phi, r);
87
88  if (nargout > 0)
89    xx = x;
90    yy = y;
91    zz = z;
92  else
93    oldfig = [];
94    if (! isempty (hax))
95      oldfig = get (0, "currentfigure");
96    endif
97    unwind_protect
98      hax = newplot (hax);
99      surf (x, y, z);
100    unwind_protect_cleanup
101      if (! isempty (oldfig))
102        set (0, "currentfigure", oldfig);
103      endif
104    end_unwind_protect
105  endif
106
107endfunction
108
109
110%!demo
111%! clf;
112%! colormap ("default");
113%! [x, y, z] = cylinder (10:-1:0, 50);
114%! surf (x, y, z);
115%! title ("cylinder() with linearly shrinking radius produces a cone");
116