RANDOMANGLE3D Return a 3D angle uniformly distributed on unit sphere. usage [THETA PHI] = randomAngle3d Generate an angle unformly distributed on the surface of the unit sphere. "Mathematical" convention is used: theta is the colatitude (angle with vertical axis, 0 for north pole, +pi for south pole, pi/2 for points at equator) with z=0. phi is the same as matlab cart2sph: angle from Ox axis, counted positively counter-clockwise. [THETA PHI] = randomAngle3d(N) generates N random angles (N is a scalar). The result is a N-by-2 array. Example: % Draw some points on the surface of a sphere figure; drawSphere; hold on; drawPoint3d(pts, '.'); axis equal; See also: angles3d, sph2cart2, cart2sph2 ------ Author: David Legland e-mail: david.legland@grignon.inra.fr created the 18/02/2005. Copyright INRA - Cepia Software platform
0001 function varargout = randomAngle3d(varargin) 0002 %RANDOMANGLE3D Return a 3D angle uniformly distributed on unit sphere. 0003 % 0004 % usage 0005 % [THETA PHI] = randomAngle3d 0006 % Generate an angle unformly distributed on the surface of the unit 0007 % sphere. 0008 % 0009 % "Mathematical" convention is used: theta is the colatitude (angle with 0010 % vertical axis, 0 for north pole, +pi for south pole, pi/2 for points at 0011 % equator) with z=0. 0012 % phi is the same as matlab cart2sph: angle from Ox axis, counted 0013 % positively counter-clockwise. 0014 % 0015 % [THETA PHI] = randomAngle3d(N) 0016 % generates N random angles (N is a scalar). The result is a N-by-2 0017 % array. 0018 % 0019 % Example: 0020 % % Draw some points on the surface of a sphere 0021 % figure; 0022 % drawSphere; hold on; 0023 % drawPoint3d(pts, '.'); 0024 % axis equal; 0025 % 0026 % See also: 0027 % angles3d, sph2cart2, cart2sph2 0028 % 0029 % ------ 0030 % Author: David Legland 0031 % e-mail: david.legland@grignon.inra.fr 0032 % created the 18/02/2005. 0033 % Copyright INRA - Cepia Software platform 0034 0035 % HISTORY 0036 % 2007-01-04 change angle order, update doc 0037 % 2011-06-27 fix bug in input parsing, add doc 0038 0039 0040 N = 1; 0041 if ~isempty(varargin) 0042 N = varargin{1}; 0043 end 0044 0045 phi = 2*pi*rand(N, 1); 0046 theta = asin(2*rand(N, 1)-1) + pi/2; 0047 0048 if nargout<2 0049 var = [theta phi]; 0050 varargout{1} = var; 0051 else 0052 varargout{1} = theta; 0053 varargout{2} = phi; 0054 end