# Cylindric to spheric conversion matlab

Mar 1st, 2015
256
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. %cylyndric: (rho, alpha, z)
2. %  rho   - the distance from Z axis
3. %  alpha - angle around the Z axis
4. %  z     - the distance above the XY plane
5.
6. %spheric:   (r,   alpha, epsilon)
7. %  r - the diameter of the sphere
8. %  alpha - angle around the Z axis
9. %  epsilon - angle from the XY plane
10.
11. function [r, alpha, epsilon] = mycyl2sph(varargin)
12.     switch nargin
13.         %One parameter - matrix full of points
14.         case 1
15.             Xi = varargin{1};
16.             [h, w] = size(Xi);
17.             r = zeros(h, w);
18.             for i = 1:w
19.                 point = Xi(:,i);
20.                 [point(1), point(2), point(3)] = mycyl2sph(point(1), point(2),point(3));
21.                 r(:,i) = point;
22.             end
23.         case 3
24.             rho = varargin{1};
25.             %Alpha will be unchanged
26.             alpha = varargin{2};
27.             z = varargin{3};
28.
29.             r = sqrt(rho^2 + z^2);
30.             %Special cases where rho is 0 - in that case we can't divide
31.             %with it
32.             if(rho==0)
33.               %If z is non-zero epsilon will point straight up or down
34.               if(z>0)
35.                 epsilon=pi/2;
36.               elseif(z<0)
37.                 epsilon=-pi/2;
38.               %If z is zero, everything is zero...
39.               else
40.                 epsilon=0;
41.               end
42.             %Normal calculation using tangents
43.             else
44.               epsilon = atan(z/rho);
45.             end
46.     end
47. end