在VPython应用程序中,依次比较一个列表中的项目和另一个列表中的项目,然后逐个使用

2024-04-19 20:10:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Vpython中有一个由以下代码创建的对象网格:

iX = [(x - pointW // 2) * sclFact for x in range(pointW)]
iY = [(x - pointH // 2) * sclFact for x in range(pointH)]
iYr = iY[::-1]
xy = list(itertools.product(iX,iYr,))
ixyz = np.array(list(itertools.product(iX,iYr,[-0.0])))
for element in ixyz:
        cube = box(pos = element,
               size=( .1, .1, .1 ),)

ixyz列表打印将如下所示:

[[-0.5  0.  -0. ]
 [-0.5 -0.5 -0. ]
 [ 0.   0.  -0. ]
 [ 0.  -0.5 -0. ]
 [ 0.5  0.  -0. ]
 [ 0.5 -0.5 -0. ]]

我有另一个列表,z值有时会改变,在某些输入和它总是更新,它会像这样

[[-0.5        0.        -0.       ]
 [-0.5       -0.5       -0.       ]
 [ 0.         0.        -0.       ]
 [ 0.        -0.5       -0.       ]
 [ 0.5        0.        -2.3570226]
 [ 0.5       -0.5       -0.       ]]

我想根据新列表移动对象,我尝试了不同的验证,但没有成功,它总是查看第二个列表中的最后一项

while True:
  .... some code here (the one getting the new list)
  ...
  ...
  # then I added this:
      for obj in scene.objects: 
        if isinstance(obj, box):
            for i in xyz: # xyz is the new list
                if obj.pos != i:
                   obj.pos = i

此变化将使所有框成为一个框,并基于列表中的最后一个位置移动

我做错了什么还是有别的方法? 或者我应该改变创建对象并移动它们的整个过程? 我对VPython和python本身非常陌生。你知道吗

编辑 我修复了两个列表,以便更好地呈现为这样

[(-0.5,0.0,-0.0),(-0.5,-0.5,-0.0),...(0.5,-0.5,-0.0)]

Tags: the对象inposobj列表forrange
1条回答
网友
1楼 · 发布于 2024-04-19 20:10:00

您正在为更新的位置列表中的每个元素重复设置位置:

box.pos = 1
box.pos = 2
box.pos = 3 

您需要设置一次位置;因此计算一个索引:

i = 0
for obj....
    if isinstance  ...
         obj.pos = xyz [i]
         i += 1

相关问题 更多 >