1 ## Copyright (C) 2000-2012 Kai Habel
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {[@var{x}, @var{y}] =} pol2cart (@var{theta}, @var{r})
21 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} pol2cart (@var{theta}, @var{r}, @var{z})
22 ## @deftypefnx {Function File} {[@var{x}, @var{y}] =} pol2cart (@var{p})
23 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} pol2cart (@var{p})
24 ## @deftypefnx {Function File} {@var{C} =} pol2cart (@dots{})
25 ## Transform polar or cylindrical to Cartesian coordinates.
27 ## @var{theta}, @var{r}, (and @var{z}) must be the same shape, or scalar.
28 ## @var{theta} describes the angle relative to the positive x-axis.
29 ## @var{r} is the distance to the z-axis (0, 0, z).
30 ## If called with a single matrix argument then each row of @var{p}
31 ## represents the polar/(cylindrical) coordinate (@var{x}, @var{y} (, @var{z})).
33 ## If only a single return argument is requested then return a matrix
34 ## @var{C} where each row represents one Cartesian coordinate
35 ## (@var{x}, @var{y} (, @var{z})).
36 ## @seealso{cart2pol, sph2cart, cart2sph}
39 ## Author: Kai Habel <kai.habel@gmx.de>
42 function [x, y, z] = pol2cart (theta, r, z)
44 if (nargin < 1 || nargin > 3)
49 if (ismatrix (theta) && (columns (theta) == 2 || columns (theta) == 3))
50 if (columns (theta) == 3)
58 error ("pol2car: matrix input must have 2 or 3 columns [THETA, R (, Z)]");
61 if (! ((ismatrix (theta) && ismatrix (r))
62 && (size_equal (theta, r) || isscalar (theta) || isscalar (r))))
63 error ("pol2cart: arguments must be matrices of same size, or scalar");
66 if (! ((ismatrix (theta) && ismatrix (r) && ismatrix (z))
67 && (size_equal (theta, r) || isscalar (theta) || isscalar (r))
68 && (size_equal (theta, z) || isscalar (theta) || isscalar (z))
69 && (size_equal (r, z) || isscalar (r) || isscalar (z))))
70 error ("pol2cart: arguments must be matrices of same size, or scalar");
84 %! t = [0, 0.5, 1] * pi;
86 %! [x, y] = pol2cart (t, r);
87 %! assert (x, [1, 0, -1], sqrt(eps));
88 %! assert (y, [0, 1, 0], sqrt(eps));
91 %! t = [0, 1, 1] * pi/4;
92 %! r = sqrt(2) * [0, 1, 2];
93 %! [x, y] = pol2cart (t, r);
94 %! assert (x, [0, 1, 2], sqrt(eps));
95 %! assert (y, [0, 1, 2], sqrt(eps));
98 %! t = [0, 1, 1] * pi/4;
99 %! r = sqrt(2) * [0, 1, 2];
101 %! [x, y, z2] = pol2cart (t, r, z);
102 %! assert (x, [0, 1, 2], sqrt(eps));
103 %! assert (y, [0, 1, 2], sqrt(eps));
110 %! [x, y, z2] = pol2cart (t, r, z);
111 %! assert (x, [0, 1, 2], sqrt(eps));
112 %! assert (y, [0, 0, 0], sqrt(eps));
116 %! t = [1, 1, 1]*pi/4;
119 %! [x, y, z2] = pol2cart (t, r, z);
120 %! assert (x, [1, 1, 1] / sqrt(2), eps);
121 %! assert (y, [1, 1, 1] / sqrt(2), eps);
128 %! [x, y, z2] = pol2cart (t, r, z);
129 %! assert (x, [1, 2, 3], eps);
130 %! assert (y, [0, 0, 0] / sqrt(2), eps);
134 %! P = [0, 0; pi/4, sqrt(2); pi/4, 2*sqrt(2)];
135 %! C = [0, 0; 1, 1; 2, 2];
136 %! assert (pol2cart(P), C, sqrt(eps));
139 %! P = [0, 0, 0; pi/4, sqrt(2), 1; pi/4, 2*sqrt(2), 2];
140 %! C = [0, 0, 0; 1, 1, 1; 2, 2, 2];
141 %! assert (pol2cart(P), C, sqrt(eps));