7189
|
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 |
7191
|
32 function [xx, yy, zz] = sphere (h, n) |
7189
|
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; |
7191
|
43 elseif (nargin == 1) |
|
44 n = h; |
7189
|
45 else |
7191
|
46 n = 20; |
7189
|
47 endif |
|
48 |
|
49 theta = linspace (0, 2*pi, n+1); |
|
50 phi = linspace (-pi/2, pi/2, n+1); |
|
51 [theta,phi] = meshgrid (theta, phi); |
|
52 |
7191
|
53 x = cos (phi) .* cos (theta); |
|
54 y = cos (phi) .* sin (theta); |
|
55 z = sin (phi); |
7189
|
56 |
|
57 if (nargout > 0) |
|
58 xx = x; |
|
59 yy = y; |
|
60 zz = z; |
|
61 else |
|
62 if (have_h) |
|
63 oldh = gca (); |
|
64 unwind_protect |
|
65 axes (h); |
|
66 surf (x, y, z); |
|
67 unwind_protect_cleanup |
|
68 axes (oldh); |
|
69 end_unwind_protect |
|
70 else |
|
71 surf (x, y, z); |
|
72 endif |
|
73 endif |
|
74 |
|
75 endfunction |