]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/sphere.m
update packages
[CreaPhase.git] / octave_packages / m / plot / sphere.m
1 ## Copyright (C) 2007-2012 Michael Goffioul
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} {[@var{x}, @var{y}, @var{z}] =} sphere (@var{n})
21 ## @deftypefnx {Function File} {} sphere (@var{h}, @dots{})
22 ## Generate three matrices in @code{meshgrid} format, such that
23 ## @code{surf (@var{x}, @var{y}, @var{z})} generates a unit sphere.
24 ## The matrices of @code{@var{n}+1}-by-@code{@var{n}+1}.  If @var{n} is
25 ## omitted then a default value of 20 is assumed.
26 ##
27 ## Called with no return arguments, @code{sphere} call directly
28 ## @code{surf (@var{x}, @var{y}, @var{z})}.  If an axes handle is passed
29 ## as the first argument, the surface is plotted to this set of axes.
30 ## @seealso{peaks}
31 ## @end deftypefn
32
33 function [xx, yy, zz] = sphere (varargin)
34
35   [h, varargin, nargin] = __plt_get_axis_arg__ ((nargout > 0), "sphere",
36                                                 varargin{:});
37   if (nargin > 1)
38     print_usage ();
39   elseif (nargin == 1)
40     n = varargin{1};
41   else
42     n = 20;
43   endif
44
45   theta = linspace (0, 2*pi, n+1);
46   phi = linspace (-pi/2, pi/2, n+1);
47   [theta,phi] = meshgrid (theta, phi);
48
49   x = cos (phi) .* cos (theta);
50   y = cos (phi) .* sin (theta);
51   z = sin (phi);
52
53   if (nargout > 0)
54     xx = x;
55     yy = y;
56     zz = z;
57   else
58     surf (h, x, y, z);
59   endif
60
61 endfunction