簡體   English   中英

python計算距離最近的xy點

[英]python calculate distance closest xy points

所以我有一個要點清單

["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確定的最接近點列表。

您可以嘗試以下代碼。 簡單多了。 使用比較器根據到起點的距離對列表進行排序(2.2,4.6)

import math
data = ["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"]
data.sort(key=lambda x: math.sqrt((float(x.split(" ")[0]) - 2.2)**2 +
                                  (float(x.split(" ")[1]) -4.6)**2))
print(data)

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

您可以簡單地根據自己想要的鍵對列表進行排序 -fe通過距離函數進行排序

import math

def splitFloat(x):
    """Split each element of x on space and convert into float-sublists"""
    return list(map(float,x.split()))

def dist(p1, p2):
    # you could remove the sqrt for computation benefits, its a symetric func
    # that does not change the relative ordering of distances
    return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)

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

s = splitFloat("2.2 4.6")       # your start point
p = [splitFloat(x) for x in p]  # your list of points

# sort by distance between each individual x and s
p.sort(key = lambda x:dist(x,s))

d = [ (dist(x,s),x) for x in p]  # create tuples with distance for funsies
print(p)
print(d)

輸出:

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

[(1.0440306508910546, [2.5, 3.6]), (3.8078865529319543, [5.5, 6.5]),
 (7.64198926981712, [7.8, 9.8]), (7.854934754662192, [9.5, 7.5]), 
 (9.360021367496977, [9.7, 10.2]), (16.560495161679196, [10.2, 19.1])]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM