Home > matGeom > geom3d > cart2sph2d.m

cart2sph2d

PURPOSE ^

CART2SPH2D Convert cartesian coordinates to spherical coordinates in degrees.

SYNOPSIS ^

function varargout = cart2sph2d(x, y, z)

DESCRIPTION ^

CART2SPH2D Convert cartesian coordinates to spherical coordinates in degrees.

   [THETA PHI RHO] = cart2sph2d([X Y Z])
   [THETA PHI RHO] = cart2sph2d(X, Y, Z)

   The following convention is used:
   THETA is the colatitude, in degrees, 0 for north pole, 180 degrees for
   south pole, 90 degrees for points with z=0.
   PHI is the azimuth, in degrees, defined as matlab cart2sph: angle from
   Ox axis, counted counter-clockwise.
   RHO is the distance of the point to the origin.
   Discussion on choice for convention can be found at:
   http://www.physics.oregonstate.edu/bridge/papers/spherical.pdf

   Example:
     cart2sph2d([1 0 0])
     ans =
       90   0   1

     cart2sph2d([1 1 0])
     ans =
       90   45   1.4142

     cart2sph2d([0 0 1])
     ans =
       0    0    1

     % check consistency with sph2cart2d
     sph2cart2d(cart2sph2d(30, 40, 5))
     ans =
         30.0000   40.0000    5.0000

   See also:
     angles3d, sph2cart2d, cart2sph, cart2sph2

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = cart2sph2d(x, y, z)
0002 %CART2SPH2D Convert cartesian coordinates to spherical coordinates in degrees.
0003 %
0004 %   [THETA PHI RHO] = cart2sph2d([X Y Z])
0005 %   [THETA PHI RHO] = cart2sph2d(X, Y, Z)
0006 %
0007 %   The following convention is used:
0008 %   THETA is the colatitude, in degrees, 0 for north pole, 180 degrees for
0009 %   south pole, 90 degrees for points with z=0.
0010 %   PHI is the azimuth, in degrees, defined as matlab cart2sph: angle from
0011 %   Ox axis, counted counter-clockwise.
0012 %   RHO is the distance of the point to the origin.
0013 %   Discussion on choice for convention can be found at:
0014 %   http://www.physics.oregonstate.edu/bridge/papers/spherical.pdf
0015 %
0016 %   Example:
0017 %     cart2sph2d([1 0 0])
0018 %     ans =
0019 %       90   0   1
0020 %
0021 %     cart2sph2d([1 1 0])
0022 %     ans =
0023 %       90   45   1.4142
0024 %
0025 %     cart2sph2d([0 0 1])
0026 %     ans =
0027 %       0    0    1
0028 %
0029 %     % check consistency with sph2cart2d
0030 %     sph2cart2d(cart2sph2d(30, 40, 5))
0031 %     ans =
0032 %         30.0000   40.0000    5.0000
0033 %
0034 %   See also:
0035 %     angles3d, sph2cart2d, cart2sph, cart2sph2
0036 %
0037 
0038 % ------
0039 % Author: David Legland
0040 % e-mail: david.legland@inrae.fr
0041 % Created: 2011-06-29,    using Matlab 7.9.0.529 (R2009b)
0042 % Copyright 2011 INRA - Cepia Software Platform.
0043 
0044 % if data are grouped, extract each coordinate
0045 if nargin == 1
0046     y = x(:, 2);
0047     z = x(:, 3);
0048     x = x(:, 1);
0049 end
0050 
0051 % cartesian to spherical conversion
0052 hxy     = hypot(x, y);
0053 rho     = hypot(hxy, z);
0054 theta   = 90 - atan2(z, hxy) * 180 / pi;
0055 phi     = atan2(y, x) * 180 / pi;
0056 
0057 % format output
0058 if nargout <= 1
0059     varargout{1} = [theta phi rho];
0060     
0061 elseif nargout == 2
0062     varargout{1} = theta;
0063     varargout{2} = phi;
0064     
0065 else
0066     varargout{1} = theta;
0067     varargout{2} = phi;
0068     varargout{3} = rho;
0069 end
0070

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019