How can I place spheres along a circle/ring ?

Hi everyone! I am working on a project where I have a sphere.Now I want to replicate the sphere and place them in a circular manner(3D plot).I would be glad, If someone could help me with this!
Eg: Imagine showing the position of the earth in its orbit(Circular orbit in this case) every month of the year. Now I imagine, we would have 12 spheres in a orbit.

3 Comments

Easy way is to use built-in Matlab functions. Here is the website for it.
You can also use the sphere equation in polar coordinates, where you can find an example here .
Shifting the center of the sphere is as easy as adding a bias to the respective coordinate (x, y and/or z)
@Aquatris, why don't you write that as an answer rather than a comment?
@Guillaume, cause those are not my answers and in my opinion, it would be unethical to copy paste others work. That is why I just referenced them as a comment.

Sign in to comment.

 Accepted Answer

See example below. All relevant functions used to generate this example can be found here .
% Unit sphere
TR=IcosahedronMesh;
TR=SubdivideSphericalMesh(TR,5);
[Tri,X]=GetMeshData(TR);
% Reference meridian
t=linspace(0,pi,5E2)';
M=zeros(numel(t),3);
M(:,2)=sin(t);
M(:,3)=cos(t);
% Circular orbit
t=linspace(0,2*pi,1E3+1)';
R=10; % radius of the orbit
O=R*[cos(t) sin(t)];
% 12 equaly spaced points along the orbit
t_o=linspace(0,2*pi,13)'; t_o(end)=[];
C=R*[cos(t_o) sin(t_o)];
C(:,3)=0;
% Visualize orbit
figure('color','w')
axis equal off
hold on
plot(O(:,1),O(:,2),'--b','LineWidth',2)
view([20 20])
% Visualize sphere at 12 equally-spaced positions along the orbit
for i=1:size(C,1)
% Rotation around z-axis
c=cos(t_o(i));
s=sin(t_o(i));
R=[c -s 0;s c 0;0 0 1];
% Rotate and translate mesh
Xi=bsxfun(@plus,(R*X')',C(i,:));
h=trimesh(triangulation(Tri,Xi));
set(h,'EdgeColor','none','FaceColor',0.75*[1 1 1],'FaceAlpha',0.9);
% Rotate and translate reference meridian
Mi=bsxfun(@plus,(R*M')',C(i,:));
plot3(Mi(:,1),Mi(:,2),Mi(:,3),'-k','LineWidth',2)
end
zoom(2)

7 Comments

Hello Mr.Semechko! Thank you so much for helping with the code. Could you please tell me how can I modify the radius of the sphere ?
Let's say 'rad' is the variable specifying the radius of the sphere, then replace the following two lines of the code
Xi=bsxfun(@plus,(R*X')',C(i,:));
Mi=bsxfun(@plus,(R*M')',C(i,:));
with
Xi=bsxfun(@plus,(rad*R*X')',C(i,:));
Mi=bsxfun(@plus,(rad*R*M')',C(i,:));
Make sure to adjust the radius of the "orbit" accordingly, so there is no overlap between the spheres.
Thank you so much ! This works :) Last question , is there a way by which I can delete(or make invisible) those black curved lines on the spheres?
@Shankar, is there a way by which I can delete(or make invisible) those black curved lines on the spheres
Well, yes, don't do the step under
% Rotate and translate reference meridian
@Anton, I can see that your code may useful to others. Why don't you post it on the FileExchange which would make it easier to find and possibly more permanent?
Hey Guillaume! That did not help . When I don't use the steps under "% Rotate and translate reference meridian", the sphere goes away and only the black curved lines stay. I want the sphere to stay and the black line to disappear.Is there also a possibility to make the sphere black in color. Could you please help?
The code under
% Rotate and translate mesh
draws the spheres. The code under
% Rotate and translate reference meridian
plots the black line. You must have suppressed the wrong section.
As for the properties of the sphere they're all defined by
set(h,'EdgeColor','none','FaceColor',0.75*[1 1 1],'FaceAlpha',0.9);
Change the FaceColor to black if you want black:
set(h,'EdgeColor','none','FaceColor', 'k');
@ Guillaume, all functions used in this demo can be found on FEX as part of 'Suite of functions to perform uniform sampling of a sphere'

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!