The fastest way to calculate Euclidean distance in 2D space

What is the way to quickly determine which point q of n points in 2D space is the closest (smallest Euclidean distance) to point p, see attached baggage.

alt text

My current way to do this in Python is to keep all the distances in a list and then run

numpy.argmin(list_of_distances)

This, however, is slightly slower when calculating this for m the number of points p. Or that?

+3
source share
3 answers

This falls under the closest point query .

? ? , - , O (1).

+3

. , n * m .

+5

Put everything as soon as possible in numpy and do the calculations there. If you have a lot of points, this is much faster than calculating distances in lists:

import numpy as np

px, py
x = np.fromiter(point.x for point in points, dtype = np.float)
y = np.fromiter(point.y for point in points, dtype = np.float)

i_closest = np.argmin((x - px) ** 2 + (y - py) ** 2)
+1
source

Source: https://habr.com/ru/post/1766935/


All Articles