nashynash
nashynash

Reputation: 375

Generate equidistant points on a sphere [MATLAB]

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,'*')

But this is what I get enter image description here

Can anyone tell where what mistake I am making in my code?

Upvotes: 0

Views: 2134

Answers (1)

TroyHaskin
TroyHaskin

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.

Three Views of the single, closed curve

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');

Spherical shape plot

Upvotes: 3

Related Questions