Mercurial > octave
comparison scripts/plot/draw/sphere.m @ 30872:2e358088e442
sphere.m: Add input validation of input N.
* sphere.m: Check that input N is a real scalar > 0. Add BIST checks for new
input validation. Rename output variables "xx,yy,zz" to "x,y,z" to match
documentation. Rename internal variables "x,y,z" to "xx,yy,zz". Add %!demo.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 30 Mar 2022 07:57:20 -0700 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
comparison
equal
deleted
inserted
replaced
30871:1bddc0ddea74 | 30872:2e358088e442 |
---|---|
51 ## @end group | 51 ## @end group |
52 ## @end example | 52 ## @end example |
53 ## @seealso{cylinder, ellipsoid, rectangle} | 53 ## @seealso{cylinder, ellipsoid, rectangle} |
54 ## @end deftypefn | 54 ## @end deftypefn |
55 | 55 |
56 function [xx, yy, zz] = sphere (varargin) | 56 function [x, y, z] = sphere (varargin) |
57 | 57 |
58 [hax, varargin, nargin] = __plt_get_axis_arg__ ("sphere", varargin{:}); | 58 [hax, varargin, nargin] = __plt_get_axis_arg__ ("sphere", varargin{:}); |
59 | 59 |
60 if (nargin > 1) | 60 if (nargin > 1) |
61 print_usage (); | 61 print_usage (); |
62 elseif (nargin == 1) | 62 elseif (nargin == 1) |
63 n = varargin{1}; | 63 n = varargin{1}; |
64 if (! (isreal (n) && isscalar (n) && n > 0)) | |
65 error ("sphere: N must be a real scalar > 0"); | |
66 endif | |
64 else | 67 else |
65 n = 20; | 68 n = 20; |
66 endif | 69 endif |
67 | 70 |
68 theta = linspace (0, 2*pi, n+1); | 71 theta = linspace (0, 2*pi, n+1); |
69 phi = linspace (-pi/2, pi/2, n+1); | 72 phi = linspace (-pi/2, pi/2, n+1); |
70 [theta,phi] = meshgrid (theta, phi); | 73 [theta, phi] = meshgrid (theta, phi); |
71 | 74 |
72 x = cos (phi) .* cos (theta); | 75 xx = cos (phi) .* cos (theta); |
73 y = cos (phi) .* sin (theta); | 76 yy = cos (phi) .* sin (theta); |
74 z = sin (phi); | 77 zz = sin (phi); |
75 | 78 |
76 if (nargout > 0) | 79 if (nargout > 0) |
77 xx = x; | 80 x = xx; |
78 yy = y; | 81 y = yy; |
79 zz = z; | 82 z = zz; |
80 else | 83 else |
81 oldfig = []; | 84 oldfig = []; |
82 if (! isempty (hax)) | 85 if (! isempty (hax)) |
83 oldfig = get (0, "currentfigure"); | 86 oldfig = get (0, "currentfigure"); |
84 endif | 87 endif |
85 unwind_protect | 88 unwind_protect |
86 hax = newplot (hax); | 89 hax = newplot (hax); |
87 | 90 |
88 surf (x, y, z); | 91 surf (xx, yy, zz); |
89 unwind_protect_cleanup | 92 unwind_protect_cleanup |
90 if (! isempty (oldfig)) | 93 if (! isempty (oldfig)) |
91 set (0, "currentfigure", oldfig); | 94 set (0, "currentfigure", oldfig); |
92 endif | 95 endif |
93 end_unwind_protect | 96 end_unwind_protect |
94 endif | 97 endif |
95 | 98 |
96 endfunction | 99 endfunction |
100 | |
101 | |
102 %!demo | |
103 %! clf; | |
104 %! colormap ("default"); | |
105 %! [x, y, z] = sphere (40); | |
106 %! surf (3*x, 3*y, 3*z); | |
107 %! axis equal; | |
108 %! title ("sphere of radius 3"); | |
109 | |
110 ## Test input validation | |
111 %!error <Invalid call> sphere (-1,1) | |
112 %!error <N must be a real scalar> sphere (2i) | |
113 %!error <N must be a real scalar> sphere ([]) | |
114 %!error <N must be a real scalar> sphere (ones (2,2)) | |
115 %!error <N must be a real scalar . 0> sphere (-1) |