changelog shortlog tags changeset files revisions annotate raw

scripts/general/sph2cart.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (49 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009 Kai Habel
2##
3## This file is part of Octave.
4##
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
9##
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
20## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r})
21## Transform spherical to Cartesian coordinates.
22## @var{x}, @var{y} and @var{z} must be the same shape, or scalar.
23## @var{theta} describes the angle relative to the positive x-axis.
24## @var{phi} is the angle relative to the xy-plane.
25## @var{r} is the distance to the origin (0, 0, 0).
26## @seealso{pol2cart, cart2pol, cart2sph}
27## @end deftypefn
28
29## Author: Kai Habel <kai.habel@gmx.de>
30## Adapted-by: jwe
31
32function [x, y, z] = sph2cart (theta, phi, r)
33
34 if (nargin != 3)
35 print_usage ();
36 endif
37
38 if ((ismatrix (theta) && ismatrix (phi) && ismatrix (r))
39 && (size_equal (theta, phi) || isscalar (theta) || isscalar (phi))
40 && (size_equal (theta, r) || isscalar (theta) || isscalar (r))
41 && (size_equal (phi, r) || isscalar (phi) || isscalar (r)))
42
43 x = r .* cos (phi) .* cos (theta);
44 y = r .* cos (phi) .* sin (theta);
45 z = r .* sin (phi);
46
47 else
48 error ("sph2cart: arguments must be matrices of same size, or scalar");
49 endif
50
51endfunction
52
53%!test
54%! t = [0, 0, 0];
55%! p = [0, 0, 0];
56%! r = [0, 1, 2];
57%! [x, y, z] = sph2cart (t, p, r);
58%! assert (x, r);
59%! assert (y, [0, 0, 0]);
60%! assert (z, [0, 0, 0]);
61
62%!test
63%! t = 0;
64%! p = [0, 0, 0];
65%! r = [0, 1, 2];
66%! [x, y, z] = sph2cart (t, p, r);
67%! assert (x, r);
68%! assert (y, [0, 0, 0]);
69%! assert (z, [0, 0, 0]);
70
71%!test
72%! t = [0, 0, 0];
73%! p = 0;
74%! r = [0, 1, 2];
75%! [x, y, z] = sph2cart (t, p, r);
76%! assert (x, r);
77%! assert (y, [0, 0, 0]);
78%! assert (z, [0, 0, 0]);
79
80%!test
81%! t = [0, 0.5, 1]*pi;
82%! p = [0, 0, 0];
83%! r = 1;
84%! [x, y, z] = sph2cart (t, p, r);
85%! assert (x, [1, 0, -1], eps);
86%! assert (y, [0, 1, 0], eps);
87%! assert (z, [0, 0, 0], eps);
88