Reputation: 375
I want to generate equidistant points on a sphere (surface of the sphere). I have come up with this code.
n = 30; % number of points
r = 10; % radius of the sphere
thetha = 0:pi/(n/2):2*pi;
phi = -pi:2*pi/n:pi;
xp = r.*sin(phi).*cos(thetha);
yp = r.*sin(thetha).*sin(phi);
zp = r.*cos(phi);
figure;plot3(xp,yp,zp,'*')
Can anyone tell where what mistake I am making in my code?
Upvotes: 0
Views: 2134
Reputation: 8401
You're only generating one path: a figure eight combination of a single closed circle in the x-y plane with single cosine along the z.
To get a full sphere shape, permutations of the two paths must be taken. This can be accomplished with meshgrid
:
[t,p] = meshgrid(thetha,phi);
xp = r.*sin(p).*cos(t);
yp = r.*sin(t).*sin(p);
zp = r.*cos(p);
plot3(xp,yp,zp,'-*');
grid('on');
box('on');
axis('square');
Upvotes: 3