【发布时间】:2020-04-03 20:22:56
【问题描述】:
我正在尝试用 Python 编写一个羊群心态模型 - “boids”。我在使用 vpython 3d 建模时遇到问题,因为它似乎无法一次显示多个球体,除非它们在代码中彼此相邻。
我的代码相关部分的基本内容是:
def uniform_distr(x1, x2):
x1, x2 = float(x1), float(x2)
return x1 + random() * (x2 - x1)
def random_position_vector():
return vector(uniform_distr(-1, 1), uniform_distr(-1, 1), uniform_distr(-1, 1))
def random_unit_vector():
return norm(vector(random(), random(), random()))
class Boid:
def __init__(self,
initial_velocity = random_unit_vector(),
initial_position = random_position_vector()):
self.velocity = initial_velocity
self.position = initial_position
self.model = sphere(pos = self.position, radius = 0.05, color = color.red,
make_trail = False)
boid_list = [Boid(), Boid(), Boid()]
我希望它显示三个随机放置的球体,但我只得到一个。我可以通过运行获得三个球体:
Ball1 = sphere(pos = vector(1,0,0), radius = 0.05)
Ball2 = sphere(pos = vector(0,1,0), radius = 0.05)
Ball3 = sphere(pos = vector(0,0,1), radius = 0.05)
造成这种情况的问题是什么?我已经阅读了很多文档,但我不明白为什么前面的代码中只显示了一个!
【问题讨论】:
标签: python 3d vpython 3d-modelling