Get the shortest distance from a point

前端 未结 8 606
长发绾君心
长发绾君心 2021-01-01 06:36

I have this table in sqlite

Locations
ID
Lat ( latitude)
Lon ( longitude) 
Type
Name
City

I have for example 100 records what I need is to

8条回答
  •  清歌不尽
    2021-01-01 07:07

    Let me make sure this is right: You have point a, and a table of points a[]. Currently, you do something like:

    • loop over b[]
      • get distance from b[i] to a
      • if distance is less than minimumDistance
        • set minimumDistance = distance
        • set closestPoint = i
    • return closestPoint

    If so, you're finding it in O(n) time, which can't really be improved upon much. You have to check all the points to see which are the closest.

    However, as Matthew points out, you can prune n by assigning points to a grid. This can drastically reduce the amount of points needed to compare. The expense is a bit of time preprocessing, and slightly more complicated logic. If you have a long list of points(or the distance() function takes a long time), you'll definitely want to do something like this.

提交回复
热议问题