comparison scripts/plot/sphere.m @ 7189:e8d953d03f6a

[project @ 2007-11-26 20:42:09 by dbateman]
author dbateman
date Mon, 26 Nov 2007 20:42:11 +0000
parents
children b48a21816f2e
comparison
equal deleted inserted replaced
7188:fdd7cd70dc14 7189:e8d953d03f6a
1 ## Copyright (C) 2007 Michael Goffioul
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 ## 02110-1301 USA
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sphere (@var{n})
20 ## @deftypefnx {Function File} {} sphere (@var{h}, @dots{})
21 ## Generates three matrices in @code{meshgrid} format, such that
22 ## @code{surf (@var{x}, @var{y}, @var{z})} generates a unit sphere.
23 ## The matrices of @code{@var{n}+1}-by-@code{@var{n}+1}. If @var{n} is
24 ## omitted then a default value of 20 is assumed.
25 ##
26 ## Called with no return arguments, @code{sphere} call directly
27 ## @code{surf (@var{x}, @var{y}, @var{z})}. If an axes handle is passed
28 ## as the first argument, the the surface is plotted to this set of axes.
29 ## @seealso{peaks}
30 ## @end deftypefn
31
32 function [xx,yy,zz] = sphere (h, n)
33
34 have_h = false;
35 if (nargin > 1 && isscalar (h) && ishandle (h))
36 if (! strcmp (get (h, "type"), "axes"))
37 error ("sphere: expecting first argument to be an axes object");
38 endif
39 if (nargin == 1)
40 n = 20;
41 endif
42 have_h = true;
43 else
44 if (nargin == 1)
45 n = h;
46 else
47 n = 20;
48 endif
49 endif
50
51 theta = linspace (0, 2*pi, n+1);
52 phi = linspace (-pi/2, pi/2, n+1);
53 [theta,phi] = meshgrid (theta, phi);
54
55 x = cos(phi).*cos(theta);
56 y = cos(phi).*sin(theta);
57 z = sin(phi);
58
59 if (nargout > 0)
60 xx = x;
61 yy = y;
62 zz = z;
63 else
64 if (have_h)
65 oldh = gca ();
66 unwind_protect
67 axes (h);
68 surf (x, y, z);
69 unwind_protect_cleanup
70 axes (oldh);
71 end_unwind_protect
72 else
73 surf (x, y, z);
74 endif
75 endif
76
77 endfunction