# HG changeset patch # User Rik # Date 1648652240 25200 # Node ID 2e358088e442852a248fbefb3e8aff19777b8f5c # Parent 1bddc0ddea74e0c779e88d79f76d293ee7691abb 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. diff -r 1bddc0ddea74 -r 2e358088e442 scripts/plot/draw/sphere.m --- a/scripts/plot/draw/sphere.m Tue Mar 29 16:15:21 2022 -0700 +++ b/scripts/plot/draw/sphere.m Wed Mar 30 07:57:20 2022 -0700 @@ -53,7 +53,7 @@ ## @seealso{cylinder, ellipsoid, rectangle} ## @end deftypefn -function [xx, yy, zz] = sphere (varargin) +function [x, y, z] = sphere (varargin) [hax, varargin, nargin] = __plt_get_axis_arg__ ("sphere", varargin{:}); @@ -61,22 +61,25 @@ print_usage (); elseif (nargin == 1) n = varargin{1}; + if (! (isreal (n) && isscalar (n) && n > 0)) + error ("sphere: N must be a real scalar > 0"); + endif else n = 20; endif theta = linspace (0, 2*pi, n+1); phi = linspace (-pi/2, pi/2, n+1); - [theta,phi] = meshgrid (theta, phi); + [theta, phi] = meshgrid (theta, phi); - x = cos (phi) .* cos (theta); - y = cos (phi) .* sin (theta); - z = sin (phi); + xx = cos (phi) .* cos (theta); + yy = cos (phi) .* sin (theta); + zz = sin (phi); if (nargout > 0) - xx = x; - yy = y; - zz = z; + x = xx; + y = yy; + z = zz; else oldfig = []; if (! isempty (hax)) @@ -85,7 +88,7 @@ unwind_protect hax = newplot (hax); - surf (x, y, z); + surf (xx, yy, zz); unwind_protect_cleanup if (! isempty (oldfig)) set (0, "currentfigure", oldfig); @@ -94,3 +97,19 @@ endif endfunction + + +%!demo +%! clf; +%! colormap ("default"); +%! [x, y, z] = sphere (40); +%! surf (3*x, 3*y, 3*z); +%! axis equal; +%! title ("sphere of radius 3"); + +## Test input validation +%!error sphere (-1,1) +%!error sphere (2i) +%!error sphere ([]) +%!error sphere (ones (2,2)) +%!error sphere (-1)