1########################################################################
2##
3## Copyright (C) 2007-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} ellipsoid (@var{xc}, @var{yc}, @var{zc}, @var{xr}, @var{yr}, @var{zr}, @var{n})
28## @deftypefnx {} {} ellipsoid (@dots{}, @var{n})
29## @deftypefnx {} {} ellipsoid (@var{hax}, @dots{})
30## @deftypefnx {} {[@var{x}, @var{y}, @var{z}] =} ellipsoid (@dots{})
31## Plot a 3-D ellipsoid.
32##
33## The inputs @var{xc}, @var{yc}, @var{zc} specify the center of the ellipsoid.
34## The inputs @var{xr}, @var{yr}, @var{zr} specify the semi-major axis lengths.
35##
36## The optional input @var{n} determines the number of faces around the
37## circumference of the cylinder.  The default value is 20.
38##
39## If the first argument @var{hax} is an axes handle, then plot into this axes,
40## rather than the current axes returned by @code{gca}.
41##
42## If outputs are requested @code{ellipsoid} returns three matrices in
43## @code{meshgrid} format, such that @code{surf (@var{x}, @var{y}, @var{z})}
44## generates the ellipsoid.
45## @seealso{cylinder, rectangle, sphere}
46## @end deftypefn
47
48function [xx, yy, zz] = ellipsoid (varargin)
49
50  [hax, varargin, nargin] = __plt_get_axis_arg__ ("ellipsoid", varargin{:});
51
52  if (nargin != 6 && nargin != 7)
53    print_usage ();
54  endif
55
56  xc = varargin{1};
57  yc = varargin{2};
58  zc = varargin{3};
59  xr = varargin{4};
60  yr = varargin{5};
61  zr = varargin{6};
62
63  if (nargin == 6)
64    n = 20;
65  else
66    n = varargin{7};
67  endif
68
69  theta = linspace (0, 2 * pi, n + 1);
70  phi = linspace (-pi / 2, pi / 2, n + 1);
71  [theta, phi] = meshgrid (theta, phi);
72
73  x = xr .* cos (phi) .* cos (theta) + xc;
74  y = yr .* cos (phi) .* sin (theta) + yc;
75  z = zr .* sin (phi) + zc;
76
77  if (nargout > 0)
78    xx = x;
79    yy = y;
80    zz = z;
81  else
82    oldfig = [];
83    if (! isempty (hax))
84      oldfig = get (0, "currentfigure");
85    endif
86    unwind_protect
87      hax = newplot (hax);
88
89      surf (x, y, z);
90    unwind_protect_cleanup
91      if (! isempty (oldfig))
92        set (0, "currentfigure", oldfig);
93      endif
94    end_unwind_protect
95  endif
96
97endfunction
98
99
100%!demo
101%! clf;
102%! ellipsoid (0, 0, 1, 2, 3, 4, 20);
103%! title ("ellipsoid()");
104