I have two lists of coordinates:
s1 = [(0,0), (0,1), (1,0), (1,1)] s2 = [(3,2), (1,9)]
I want to calculate the minimum distance of each point in s1 to any point in s2. for example, the results should be as follows.
result = [3.60, 3.16, 2.82, 2.23]
Question: What is the most optimized way in terms of runtime to achieve this result?
So far I have tried this, but the runtime does not promise:
import math def nearestDistance(boundary, p): minDistList = map(lambda b: (b[0] - p[0])**2 + (b[1] - p[1])**2, boundary) minDist2 = min(minDistList) return math.sqrt(float(minDist2)) d = [] for p in s1: d.append(nearestDistance(s2, p))
Should I change the structure of s1 and s2 (instead of points, use 2d arrays, for example)?
source share