问题
假设我站在机场里,给定一个地理坐标对,如何高效地确定我所在的机场?
输入
- 一个坐标对
(x,y)
,表示我所在的位置。 - 一组坐标对
[(a1,b1), (a2,b2)...]
,其中每个坐标对表示一个机场。
期望输出
一组坐标对 (a,b)
,来自机场坐标对集合,表示最接近点 (x,y)
的机场。
低效解决方案
这是我的低效尝试解决这个问题。显然,它在机场集的长度上是线性的。
shortest_distance = None
shortest_distance_coordinates = None
point = (50.776435, -0.146834)
for airport in airports:
distance = compute_distance(point, airport)
if distance < shortest_distance or shortest_distance is None:
shortest_distance = distance
shortest_distance_coordinates = airport
问题
如何改进此解决方案?这可能涉及基于我们当前所在位置的坐标对机场列表进行某种预过滤,或事先将它们按某种顺序进行排序。
compute_distance()
中执行某些非常复杂的操作,但我认为不太可能,因为您可能只是使用Haversine距离)。 - Dmitry Torba