2 回答

TA贡献1895条经验 获得超3个赞
由于您已经在使用 numpy,请尝试以矢量化方式重写您的操作,而不是使用循环。
# choose appropriate dtype for better perf
dtype = np.float32
# take all indices in an array
indices = np.indices((ysize, xsize), dtype=dtype).T
points = np.array(list(lis.keys()), dtype=dtype)
# squared distance from all indices to all points
dist = (indices[..., np.newaxis] - points.T) ** 2
dist = dist.sum(axis=-2)
# squared circle radii
dist_thresh = np.array(list(lis.values()), dtype=dtype) ** 2
intersect = np.all(dist <= dist_thresh, axis=-1)
在我的机器上,这比 for 循环版本快 60 倍左右。
它仍然是一个蛮力版本,可能对所有坐标进行许多不必要的计算。问题中没有给出圆圈,因此很难对它们进行推理。如果它们覆盖的区域相对较小,那么如果考虑较小的区域,问题的解决速度会快得多(仍然是计算上的,而不是分析上的)。例如,代替测试所有坐标,可以使用圆的边界框的交点,这可以显着减少计算负载。
添加回答
举报