为此,您可以使用两点之间的欧几里得距离(在 2D 中基本上简化为毕达哥拉定理)。
假设您有这样的海龟坐标(与您提供的坐标样本格式相同,例如 (X,Y) 以像素为单位):
turtle_coords = (10.0, 10.0)
您可以编写一个函数来计算海龟与任何其他点之间的距离(您也可以使用 numpy 包中的sqrt 函数)。此函数将海龟坐标 (turtle_coords) 作为一对 (X_turtle, Y_turtle) 并将任何兴趣点坐标 (point_coords) 再次作为一对 (X_point, Y_point)。在这些对中,您可以使用 Couple[0] 访问每个 X 值,使用 Couple[1] 访问 Y 值。距离函数如下:
def distance(turtle_coords, point_coords):
return ((turtle_coords[0] - point_coords[0])**2 + (turtle_coords[1] - point_coords[1])**2)**0.5
然后,您可以遍历 list_of_coords 变量的所有坐标,以创建海龟与每个兴趣点之间的距离列表。
distances = []
for point_coords in list_of_coords:
distances.append(distance(turtle_coords, point_coords))
然后,您将获得每个提供的坐标与海龟坐标之间的距离列表。然后,您可以评估海龟是否足够接近某些坐标。假设你有一个距离阈值:
distance_threshold = 10 # Say 10 pixels
close_to_coords = []
for distance in distances:
close_to_coords.append(distance <= distance_threshold)
# You get a list like [False, False, True, False , etc...]