我想生成随机分布在立方体中的不重叠的相同球体
到目前为止,我正在随机使用randint生成随机x,y,z,然后将它们用作绘制球体的中心。例如,要绘制5个球体:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from random import randint
N = 5
#generate random x,y,z
import random
def RandX(start, end, num):
res = []
for j in range(num):
res.append(random.randint(start, end))
return res
num = N
start = 0
end = 100
print(RandX(start, end, num))
def RandY(start, end, num):
res = []
for j in range(num):
res.append(random.randint(start, end))
return res
num = N
start = 0
end = 100
print(RandY(start, end, num))
def RandZ(start, end, num):
res = []
for j in range(num):
res.append(random.randint(start, end))
return res
num = N
start = 0
end = 100
print(RandZ(start, end, num))
X = RandX(start, end, num)
Y = RandY(start, end, num)
Z = RandZ(start, end, num)
random = np.vstack((X, Y, Z))
#generate spheres
center = random.T
radius = 3
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in range(N):
x = radius * np.outer(np.cos(u), np.sin(v)) + center[i][0]
y = radius * np.outer(np.sin(u), np.sin(v)) + center[i][1]
z = radius * np.outer( np.ones(np.size(u)), np.cos(v) ) + center[i][2]
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='r')
plt.show()
plt.savefig('random_nano_spheres2')
这有效并且给了我,这就是我想要的 random_spheres
但我想知道
1)如何确保粒子不重叠?
2)有更简单的方法吗?
谢谢您的帮助!