[英]How to calculate distance between n coordinates in python
I am working on a python
project where from a function I am getting coordinate values x, y
in a dict
like below:我正在研究python
项目,其中从dict
我得到坐标值x, y
如下所示:
centroid_dict = {0: (333, 125), 1: (288, 52), 2: (351, 41)}
where 0, 1, 2
are the objectId
and (333, 125), (288, 52), (351, 41)
are their (x, y)
coordinate values respectively.其中0, 1, 2
是objectId
, (333, 125), (288, 52), (351, 41)
分别是它们的(x, y)
坐标值。 I need to calculate the distance between each coordinate which means:我需要计算每个坐标之间的距离,这意味着:
0 - 1 -> ((333, 125) - (288, 52))
0 - 2 -> ((333, 125) - (351, 41))
1 - 0 -> ((288, 52) - (333, 125))
1 - 2 -> ((288, 52) - (351, 41))
2 - 0 -> ((351, 41) - (333, 125))
2 - 1 -> ((351, 41) - (288, 52))
To calculate the distance, I can use:要计算距离,我可以使用:
def calculateDistance(x1, y1, x2, y2):
dist = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return dist
but I am not able to think of any logic which can calculate distance between each points as the length of dict might increase in future.但我想不出任何可以计算每个点之间距离的逻辑,因为 dict 的长度将来可能会增加。 As of now, it is 3
but it can be 10
.截至目前,它是3
但它可以是10
。 Can anyone please help me give some ideas on it.任何人都可以帮我提供一些想法。 Thanks谢谢
You can use combinations from itertools to form a new dictionary with every pair of objects as key:您可以使用来自 itertools 的组合来形成一个以每对对象为键的新字典:
from itertools import combinations:
distances = dict()
for (id1,p1),(id2,p2) in combinations(centroid_dict.items(),2):
dx,dy = p1[0]-p2[0], p1[1]-p2[1]
distances[id1,id2] = distances[id2,id1] = math.sqrt(dx*dx+dy*dy)
The drawback of this approach is that it will systematically calculate all distances and your program may not need to access all of those values.这种方法的缺点是它会系统地计算所有距离,您的程序可能不需要访问所有这些值。 A more efficient approach could be to use a dictionary as a cache of distance and obtain them "on demand" using a function:一种更有效的方法是使用字典作为距离缓存,并使用 function“按需”获取它们:
distanceCache = dict()
def getDist(id1,id2):
if id1 > id2: return getDist(id2,id1) # only store each pair once
if (id1,id1) in distanceCache: return distanceCache[id1,id2]
x1,y1 = centroid_dict[id1]
x2,y2 = centroid_dict[id2]
dx,dy = x1-x2,y1-y2
return distanceCache.setDefault((id1,id2),math.sqrt(dx*dx+dy*dy))
This will allow you to simply clear the cache when object locations are changed without incurring an immediate delay of O(n^2) time这将允许您在 object 位置更改时简单地清除缓存,而不会立即延迟 O(n^2) 时间
Note that you could also use the points (positions) themselves as key to the cache and also use an LRU cache (from functools)请注意,您也可以使用点(位置)本身作为缓存的键,也可以使用 LRU 缓存(来自 functools)
from functools import lru_cache
import math
@lru_cache(1024)
def getDist(p1,p2):
dx,dy = p1[0]-p2[0],p1[1]-p2[1]
return math.sqrt(dx*dx+dy*dy)
def getObjectDist(id1,id2):
return getDist(centroid_dict[id1],centroid_dict[id2])
using solution from here , it is kd tree graph problem使用here的解决方案,这是kd树图问题
Find the Euclidean distances between four 2-D coordinates:求四个二维坐标之间的欧几里得距离:
from scipy.spatial import distance
coords = [(35.0456, -85.2672),
(35.1174, -89.9711),
(35.9728, -83.9422),
(36.1667, -86.7833)]
distance.cdist(coords, coords, 'euclidean')
array([[ 0. , 4.7044, 1.6172, 1.8856],
[ 4.7044, 0. , 6.0893, 3.3561],
[ 1.6172, 6.0893, 0. , 2.8477],
[ 1.8856, 3.3561, 2.8477, 0. ]])
You can do something like this.你可以做这样的事情。 No import required.无需导入。
def dist(key1,key2):
return calculateDistance(*centroid_dict[key1],*centroid_dict[key2])
all_dist = []
for key1 in centroid_dict:
all_dist.extend([dist(key1,key2) for key2 in centroid_dict if not key1==key2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.