Mercurial > hg > octave-nkf
comparison scripts/general/pol2cart.m @ 10688:7357e37f34fa
coordinate transforms: add option to operate on column matrix of coordinates.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 08 Jun 2010 21:47:05 -0700 |
parents | 742cf6388a8f |
children | be55736a0783 |
comparison
equal
deleted
inserted
replaced
10687:a8ce6bdecce5 | 10688:7357e37f34fa |
---|---|
15 ## You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {[@var{x}, @var{y}] =} pol2cart (@var{theta}, @var{r}) | 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}) | 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{}) | |
22 ## Transform polar or cylindrical to Cartesian coordinates. | 25 ## Transform polar or cylindrical to Cartesian coordinates. |
23 ## @var{theta}, @var{r} (and @var{z}) must be the same shape, or scalar. | 26 ## |
27 ## @var{theta}, @var{r}, (and @var{z}) must be the same shape, or scalar. | |
24 ## @var{theta} describes the angle relative to the positive x-axis. | 28 ## @var{theta} describes the angle relative to the positive x-axis. |
25 ## @var{r} is the distance to the z-axis (0, 0, z). | 29 ## @var{r} is the distance to the z-axis (0, 0, z). |
26 ## @seealso{cart2pol, cart2sph, sph2cart} | 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})). | |
32 ## | |
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} | |
27 ## @end deftypefn | 37 ## @end deftypefn |
28 | 38 |
29 ## Author: Kai Habel <kai.habel@gmx.de> | 39 ## Author: Kai Habel <kai.habel@gmx.de> |
30 ## Adapted-by: jwe | 40 ## Adapted-by: jwe |
31 | 41 |
32 function [x, y, z] = pol2cart (theta, r, z) | 42 function [x, y, z] = pol2cart (theta, r, z) |
33 | 43 |
34 if (nargin < 2 || nargin > 3) | 44 if (nargin < 1 || nargin > 3) |
35 error ("pol2cart: number of arguments must be 2 or 3"); | 45 print_usage (); |
36 endif | 46 endif |
37 | 47 |
38 if (nargin == 2 && nargout > 2) | 48 if (nargin == 1) |
39 error ("pol2cart: number of output arguments must not be greater than number of input arguments"); | 49 if (ismatrix (theta) && (columns (theta) == 2 || columns (theta) == 3)) |
50 if (columns (theta) == 3) | |
51 z = theta(:,3); | |
52 else | |
53 z = []; | |
54 endif | |
55 r = theta(:,2); | |
56 theta = theta(:,1); | |
57 else | |
58 error ("pol2car: matrix input must have 2 or 3 columns [THETA, R (, Z)]"); | |
59 endif | |
60 elseif (nargin == 2) | |
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"); | |
64 endif | |
65 elseif (nargin == 3) | |
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"); | |
71 endif | |
40 endif | 72 endif |
41 | 73 |
42 if ((ismatrix (theta) && ismatrix (r) && (nargin == 2 || ismatrix (z))) | 74 x = r .* cos (theta); |
43 && (size_equal (theta, r) || isscalar (theta) || isscalar (r)) | 75 y = r .* sin (theta); |
44 && (nargin == 2 || size_equal (theta, z) || isscalar (theta) || isscalar (z)) | |
45 && (nargin == 2 || size_equal (r, z) || isscalar (r) || isscalar (z))) | |
46 | 76 |
47 x = cos (theta) .* r; | 77 if (nargout <= 1) |
48 y = sin (theta) .* r; | 78 x = [x, y, z]; |
49 | |
50 else | |
51 error ("pol2cart: arguments must be matrices of same size, or scalar"); | |
52 endif | 79 endif |
53 | 80 |
54 endfunction | 81 endfunction |
55 | 82 |
56 %!test | 83 %!test |
101 %! [x, y, z2] = pol2cart (t, r, z); | 128 %! [x, y, z2] = pol2cart (t, r, z); |
102 %! assert (x, [1, 2, 3], eps); | 129 %! assert (x, [1, 2, 3], eps); |
103 %! assert (y, [0, 0, 0] / sqrt(2), eps); | 130 %! assert (y, [0, 0, 0] / sqrt(2), eps); |
104 %! assert (z, z2); | 131 %! assert (z, z2); |
105 | 132 |
133 %!test | |
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)); | |
137 | |
138 %!test | |
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)); | |
142 |