4 ms·
Solve the problem "find me the all restaurants within 1 mile of of my location" efficiently in a database with restaurants and their lat-long coordinates. Brut
by thehappypm 5y ago
Solve the problem "find me the all restaurants within 1 mile of of my location" efficiently in a database with restaurants and their lat-long coordinates.
Brute force solution: iterate over all possible restaurants, compute their distance to your location, then return the list that meet the criteria.
Better solution: cut the world into 1-mile square grids, and assign each restaurant a grid square index. Search for all restaurants in your grid square, plus all adjacent grid squares to that (because you might be on the edge of your square), and filter out the ones that are more than 1 mile away. This is a pretty good solution, but you're searching a square area for a circle of restaurants. Any restaurants in the corners of the square are wasting your time -- you're never within 1 mile of the corner.
So, if you could search a circular area, you'd have no corners. Using tessellated hexagons means your search space is more circular, so it's more efficient.