【发布时间】:2021-11-11 06:58:05
【问题描述】:
如何从等距点的排序 numpy 数组中有效地找到给定半径和中心的圆内的点集?
例如,这是我的代码以及我目前如何在半径内提取这些点。
import numpy as np
n_points = 10000
x_lim = [0, 100]
y_lim = [0, 100]
x, y = np.meshgrid(np.linspace(*x_lim, n_points), np.linspace(*y_lim, n_points))
xy = np.vstack((x.flatten(), y.flatten())).T
# Current approach
radius = 5
point = np.array([50, 35], dtype=float)
# Indexes of those points within a circle of radius centered at point
idxs = np.linalg.norm(point - xy, axis=-1) < radius
points_within_circle = xy[idxs]
如何更有效地计算这些索引?我想是因为数组是结构化的并且在每个点之间有一个固定的距离,我应该能够利用它来消除大部分检查。
【问题讨论】: