# How to draw smooth balls (spheres) and sticks (cylinders) around my XYZ points.

19 views (last 30 days)
ankit agrawal on 9 Apr 2015
Commented: Chris McComb on 10 Apr 2015
Hi,
I generate the xyz coordinate of a molecule using simulation in C++. Now i have 250 atoms of xyz coordinate in one molecule. (the file attached) I want to draw the smooth surface over my atoms. So, I had written MATLAB code to draw blobby surface in my molecules. The code is following
x=v(:,1);
y=v(:,2);
z=v(:,3);
[xg,yg,zg] = meshgrid(min(x)-2 : 1 : max(x)+2,min(y)-2 : 1 : max(y)+2,min(z)-2 : 1 : max(z)+2);
xd=size(xg,1);yd=size(xg,2);zd=size(xg,3);
for ii=1:xd
for jj=1:yd
for kk=1:zd
blobby=0;
for pp=1:length(v)
c=[xg(ii,jj,kk),yg(ii,jj,kk),zg(ii,jj,kk)] - [x(pp) y(pp) z(pp)];
blobby=blobby+exp(B*( (sum(c.^2 )/(Radius^2)) -1 )) ;
end
G(ii,jj,kk)=blobby;
end
end
end
%isosurface(xg,yg,zg,G)
p=patch(isosurface(xg,yg,zg,G));
isonormals(xg,yg,zg,G,p);
set(p,'FaceColor','red','EdgeColor','none');
%daspect([2 2 2])
view(3); axis tight
camlight; lighting phong
The figure is attached for blobby parameter B = -1 and Radius =1.5 Some of the atoms are very far in my case so its not covered in single surface. If i increase the Radius and decrease B then it becomes too much smooth (it becomes sphere).
Now i wanted some new technique like to connect the bond in cylinder and atoms in sphere such that overall structure should be smooth. There is representation in vmd called CPK where i can make such image. But i cannot make this molecule smooth. Is anyone know how to make such figures in MATLAB with good smoothness?
ankit agrawal on 10 Apr 2015
Hi Chris McComb The atoms are bonded in order of appearance in the data file. Like atom i is bonded to i+1, where i range from 1 to 249.

Chris McComb on 10 Apr 2015
Edited: Chris McComb on 10 Apr 2015
This should do what you're looking for regarding the spheres (but see my above comment for cylinders).
% Parameters
SPHERE_RES = 20; % resolution for your spheres
% Make base sphere data
[xb, yb, zb] = sphere(SPHERE_RES);
% Make a figure
figure;
hold on;
% Plot spheres
for i=1:1:length(v)
end
% Make sure they're smooth and shaded
light;
lighting gouraud;
##### 2 CommentsShow 1 older commentHide 1 older comment
Chris McComb on 10 Apr 2015
Happy to help! What you're seeing are the edges between panels on the surface. The way I handled that for the spheres was just to make them transparent. In your code, just replace
surf(xc,yc,zc);
with
surf(xc, yc, zc, 'facecolor', 'b', 'edgealpha', 0);
This will make the surface blue ('b'), and make the edges 0% opaque.

### Categories

Find more on Lighting, Transparency, and Shading in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!