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