changeset 30871:1bddc0ddea74

cylinder.m: Accept scalar radius input for Matlab compatibility. * NEWS: Announce change in Matlab compatibility section. * cylinder.m: Check for scalar R input and expand to 2-term form if necessary. Add input validation for input R. Add BIST tests for 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.
author Rik <rik@octave.org>
date Tue, 29 Mar 2022 16:15:21 -0700
parents b7826af05108
children 2e358088e442
files scripts/plot/draw/cylinder.m
diffstat 1 files changed, 17 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/cylinder.m	Tue Mar 29 16:09:52 2022 -0700
+++ b/scripts/plot/draw/cylinder.m	Tue Mar 29 16:15:21 2022 -0700
@@ -57,7 +57,7 @@
 ## @seealso{ellipsoid, rectangle, sphere}
 ## @end deftypefn
 
-function [xx, yy, zz] = cylinder (varargin)
+function [x, y, z] = cylinder (varargin)
 
   [hax, args, nargs] = __plt_get_axis_arg__ ("cylinder", varargin{:});
 
@@ -74,21 +74,24 @@
     n = args{2};
   endif
 
-  if (length (r) < 2)
-    error ("cylinder: length (R) must be larger than 2");
+  if (! isvector (r))
+    error ("cylinder: R must be a scalar or vector");
+  endif
+  if (isscalar (r))
+    r .*= [1 1];  # expand single radius specification to required 2-term form
   endif
 
   phi = linspace (0, 2*pi, n+1);
   idx = 1:length (r);
   [phi, idx] = meshgrid (phi, idx);
-  z = (idx - 1) / (length (r) - 1);
+  zz = (idx - 1) / (length (r) - 1);
   r = r(idx);
-  [x, y] = pol2cart (phi, r);
+  [xx, yy] = pol2cart (phi, r);
 
   if (nargout > 0)
-    xx = x;
-    yy = y;
-    zz = z;
+    x = xx;
+    y = yy;
+    z = zz;
   else
     oldfig = [];
     if (! isempty (hax))
@@ -96,7 +99,7 @@
     endif
     unwind_protect
       hax = newplot (hax);
-      surf (x, y, z);
+      surf (xx, yy, zz);
     unwind_protect_cleanup
       if (! isempty (oldfig))
         set (0, "currentfigure", oldfig);
@@ -113,3 +116,8 @@
 %! [x, y, z] = cylinder (10:-1:0, 50);
 %! surf (x, y, z);
 %! title ("cylinder() with linearly shrinking radius produces a cone");
+
+## Test input validation
+%!error <Invalid call> cylinder (1,2,3)
+%!error <R must be a scalar> cylinder ([])
+%!error <R must be a scalar or vector> cylinder (ones (2,2))