for point in random_gujiapoints: 现在我想要在lunk_pos中找到与同x和同y方向上距离point最近的四个点,并分别求其同x和同y的距离
时间: 2024-04-14 09:28:14 浏览: 19
可以通过以下代码来实现在 `lunk_pos` 中找到与同 x 和同 y 方向上距离 `point` 最近的四个点,并分别求其同 x 和同 y 的距离:
```python
def collect_points(random_gujiapoints, lunk_pos):
result = []
for point in random_gujiapoints:
dirx = [] # 相同x
diry = [] # 相同y
x, y = point
# 找出同 x 方向上距离最近的四个点
closest_x = sorted([(pos, abs(y - pos[1])) for pos in lunk_pos if pos[0] == x], key=lambda x: x[1])[:4]
dirx.extend(closest_x)
# 找出同 y 方向上距离最近的四个点
closest_y = sorted([(pos, abs(x - pos[0])) for pos in lunk_pos if pos[1] == y], key=lambda x: x[1])[:4]
diry.extend(closest_y)
distances_x = [pz.EuclideanDis(pos[0], point) for pos, _ in dirx]
distances_y = [pz.EuclideanDis(pos[0], point) for pos, _ in diry]
result.append((distances_x, distances_y))
return result
```
这里的代码与之前的代码类似,只是将获取最近的两个点改为获取最近的四个点,并且分别计算了同 x 和同 y 方向上的距离。返回结果为一个列表,每个元素包含了同 x 和同 y 方向上的距离。