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,它会为您提供地图坐标。您需要使用的是逆映射。

您可以尝试这样做:

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)

10-07 19:11
查看更多