在 vispy库我有一个显示为标记的点列表,我想更改最接近单击点的点的颜色(或只是获取其索引)。
我可以通过 event.pos 获取点击点的像素,但我需要它的实际坐标来将其与其他坐标进行比较(或获取其他标记的像素点以将其与事件位置进行比较)。
我有这段代码来获取最近的点索引。它接受数组和一个点的输入(单击一个)
def get_nearest_index(pos,p0):
count=0
col =[(1,1,1,0.5) for i in pos]
dist= math.inf
ind=0
for i in range(len(pos)):
d = (pos[i][0]-p0[0])**2+(pos[i][1]-p0[1])**2
if d<dist:
ind=i
dist=d
return ind
但问题是我必须在同一坐标系中传递它们。
打印出 event.pos
返回像素,例如:[319 313]
,而我在 pos
数组中的位置是:
[[-0.23801816 0.55117583 -0.56644607]
[-0.91117247 -2.28957391 -1.3636486 ]
[-1.81229627 0.50565064 -0.06175591]
[-1.79744952 0.48388072 -0.00389405]
[ 0.33729051 -0.4087148 0.57522977]]
所以我需要将其中一个转换为另一个。转换如
tf = view.scene.transform
p0 = tf.map(pixel_pt)
print(str(pixel_pt) + "--->"+str(p0))
打印出 [285 140 0 1]--->[ 4.44178173e+04 -1.60156369e+04 0.00000000e+00 1.00000000e+00]
,这与点相距甚远。
最佳答案
当将像素转换为本地坐标时,您正在使用transform.map,它根据vispy tutorial ,为您提供 map 坐标。您需要使用的是逆映射。
你可以尝试这样做:
tf = view.scene.transform
point = tf.imap(event.pos)
print(str(event.pos) + "--->"+str(point))
同样,如果您需要转换特定的标记集,这将是一个更好的方法。
ct = markers.node_transform(markers.root_node)
point = ct.imap(event.pos)
关于python - 如何在vispy中选择距离点击点最近的标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59475980/