gpt4 book ai didi

python计算距离最近的xy点

转载 作者:行者123 更新时间:2023-11-28 22:18:57 24 4
gpt4 key购买 nike

所以我有一个点列表

["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"]

起点为

["2.2 4.6"]

现在我要做的是获取离起点最近的点,然后是离起点最近的点,依此类推。

所以我开始计算距离

def dist(p1,p2):
return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)

但同样,我试图获得最接近起点的点,然后是最接近起点的点,依此类推。

好的,因为你提示我没有显示足够的代码?

fList = ["2.5 3.6", "9.5 7.5", "10.2 19.1", "9.7 10.2",  "5.5 6.5", "7.8 9.8"]
def distance(points):
p0, p1 = points
return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)

min_pair = min(itertools.combinations(fList, 2), key=distance)
min_distance = distance(min_pair)

print min_pair
print min_distance

所以我通过了我的起点我得到了

([2.2, 4.6], [2.5, 3.6])

所以现在我需要使用 2.5、3.6 作为起点并找到下一个最接近的点等等

有没有人做过类似的事情?

最佳答案

一种可能是使用广度优先搜索来扫描所有元素,并为从队列中弹出的每个元素找到最近的点:

import re, collections
import math

s = ["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"]
def cast_data(f):
def wrapper(*args, **kwargs):
data, [start] = args
return list(map(lambda x:' '.join(map(str, x)), f(list(map(lambda x:list(map(float, re.findall('[\d\.]+', x))), data)), list(map(float, re.findall('[\d\.]+', start))))))
return wrapper

@cast_data
def bfs(data, start, results=[]):
queue = collections.deque([start])
while queue and data:
result = queue.popleft()
possible = min(data, key=lambda x:math.hypot(*[c-d for c, d in zip(result, x)]))
if possible not in results:
results.append(possible)
queue.append(possible)
data = list(filter(lambda x:x != possible, data))
return results

print(bfs(s, ["2.2 4.6"]))

输出:

['2.5 3.6', '5.5 6.5', '7.8 9.8', '9.7 10.2', '9.5 7.5', '10.2 19.1']

结果是使用 math.hypot 确定的最近点列表。

关于python计算距离最近的xy点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50159610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com