]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/pol2cart.m
update packages
[CreaPhase.git] / octave_packages / m / general / pol2cart.m
1 ## Copyright (C) 2000-2012 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}] =} 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.
26 ##
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})).
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}
37 ## @end deftypefn
38
39 ## Author: Kai Habel <kai.habel@gmx.de>
40 ## Adapted-by: jwe
41
42 function [x, y, z] = pol2cart (theta, r, z)
43
44   if (nargin < 1 || nargin > 3)
45     print_usage ();
46   endif
47
48   if (nargin == 1)
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
72   endif
73
74   x = r .* cos (theta);
75   y = r .* sin (theta);
76
77   if (nargout <= 1)
78     x  = [x, y, z];
79   endif
80
81 endfunction
82
83 %!test
84 %! t = [0, 0.5, 1] * pi;
85 %! r = 1;
86 %! [x, y] = pol2cart (t, r);
87 %! assert (x, [1, 0, -1], sqrt(eps));
88 %! assert (y, [0, 1,  0], sqrt(eps));
89
90 %!test
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));
96
97 %!test
98 %! t = [0, 1, 1] * pi/4;
99 %! r = sqrt(2) * [0, 1, 2];
100 %! z = [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));
104 %! assert (z, z2);
105
106 %!test
107 %! t = 0;
108 %! r = [0, 1, 2];
109 %! z = [0, 1, 2];
110 %! [x, y, z2] = pol2cart (t, r, z);
111 %! assert (x, [0, 1, 2], sqrt(eps));
112 %! assert (y, [0, 0, 0], sqrt(eps));
113 %! assert (z, z2);
114
115 %!test
116 %! t = [1, 1, 1]*pi/4;
117 %! r = 1;
118 %! z = [0, 1, 2];
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);
122 %! assert (z, z2);
123
124 %!test
125 %! t = 0;
126 %! r = [1, 2, 3];
127 %! z = 1;
128 %! [x, y, z2] = pol2cart (t, r, z);
129 %! assert (x, [1, 2, 3], eps);
130 %! assert (y, [0, 0, 0] / sqrt(2), eps);
131 %! assert (z, z2);
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