]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/cylinder.m
update packages
[CreaPhase.git] / octave_packages / m / plot / cylinder.m
1 ## Copyright (C) 2007-2012 Michael Goffioul and Kai Habel
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} {} cylinder
21 ## @deftypefnx {Function File} {} cylinder (@var{r})
22 ## @deftypefnx {Function File} {} cylinder (@var{r}, @var{n})
23 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} cylinder (@dots{})
24 ## @deftypefnx {Function File} {} cylinder (@var{ax}, @dots{})
25 ## Generate three matrices in @code{meshgrid} format, such that
26 ## @code{surf (@var{x}, @var{y}, @var{z})} generates a unit cylinder.
27 ## The matrices are of size @code{@var{n}+1}-by-@code{@var{n}+1}.
28 ## @var{r} is a vector containing the radius along the z-axis.
29 ## If @var{n} or @var{r} are omitted then default values of 20 or [1 1]
30 ## are assumed.
31 ##
32 ## Called with no return arguments, @code{cylinder} calls directly
33 ## @code{surf (@var{x}, @var{y}, @var{z})}.  If an axes handle @var{ax}
34 ## is passed as the first argument, the surface is plotted to this set
35 ## of axes.
36 ##
37 ## Examples:
38 ##
39 ## @example
40 ## @group
41 ## [x, y, z] = cylinder (10:-1:0, 50);
42 ## surf (x, y, z);
43 ## title ("a cone");
44 ## @end group
45 ## @end example
46 ## @seealso{sphere}
47 ## @end deftypefn
48
49 function [xx, yy, zz] = cylinder (varargin)
50
51   [ax, args, nargs] = __plt_get_axis_arg__ ((nargout > 0), "cylinder",
52                                             varargin{:});
53
54   if (nargs == 0)
55     n = 20;
56     r = [1, 1];
57   elseif (nargs == 1)
58     n = 20;
59     r = args{1};
60   elseif (nargs == 2)
61     r = args{1};
62     n = args{2};
63   else
64     print_usage ();
65   endif
66
67   if (length (r) < 2)
68     error ("cylinder: length(R) must be larger than 2");
69   endif
70
71   phi = linspace (0, 2*pi, n+1);
72   idx = 1:length(r);
73   [phi, idx] = meshgrid(phi, idx);
74   z = (idx - 1) / (length(r) - 1);
75   r = r(idx);
76   [x, y] = pol2cart (phi, r);
77
78   if (nargout > 0)
79     xx = x;
80     yy = y;
81     zz = z;
82   else
83     surf (ax, x, y, z);
84   endif
85
86 endfunction
87
88 %!demo
89 %! clf
90 %! [x, y, z] = cylinder (10:-1:0,50);
91 %! surf (x, y, z);
92 %! title ("a cone")