This is a code for generating random sized spheres with mayavi,
I want to make the spheres to be connected with each other by the surface or with a bond line:
- Spheres must be at random positions in 3D space
- Spheres must be with the same radius
from mayavi import mlab
import numpy as np
[phi,theta] = np.mgrid[0:2*np.pi:12j,0:np.pi:12j]
x = np.cos(phi)*np.sin(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(theta)
def plot_sphere(p):
r,a,b,c = p
r=1
return mlab.mesh(r*x+a, r*y+b, r*z )
for k in range(8):
c = np.random.rand(4)
c[0] /= 10.
plot_sphere(c)
mlab.show()
From sphere equation:
So when passing arguments to
mlab.mesh
we would like to set[x_0, y_0, z_0]
for each sphere such as they are at different positions from the axis.The problem was that the numbers generated by
np.random.rand(4)
are random, but not distinct.Let's modify so that the arguments
[x_0, y_0, z_0]
are random and distinct:sample
to get distinct index numbers in a cubeindex_to_3d
the index to an(x, y, z)
coordinatesThe radius,
r
, can be adjusted to have more or less spacing between the spheres.Spheres at 3D space
Code:
Output:
Spheres cluster
Let's utilize
gauss
to create coordinates for the cluster points.Code:
Output: