0

I have a set of points denoted by (x y) coordinates in a space with a specified width and height, let's call this set s. Given any point p (p is not necessarily in s), I want to find the point q in s, such that the distance between p and q is minimized (i.e., find the closest point to p, that is in s).

Intuitively I feel as though I need to parse the entirety of s, and find the smallest distance by simply evaluating them all. For my application, this is unfortunately super slow.

I am wondering if there are any better ways to do this search? The only better way I can think is to organize the set s into a quadtree, find the leaf node that houses p, and check this leaf node's neighbour nodes for potential candidates for q. But that will be a lot of implementation :P

ronalama
  • 39
  • 1
  • Far easier to implement than a quadtree would be a simple grid. – High Performance Mark Nov 14 '22 at 15:38
  • @HighPerformanceMark but then would that offer any optimization in runtime for point lookups? – ronalama Nov 14 '22 at 18:01
  • Yes, of course. You only have to check the distance to points in neighbouring grid cells. A regular grid has most of the advantages of the quadtree you propose, but is far simpler to program. – High Performance Mark Nov 14 '22 at 19:50
  • 1
    The technical term is "nearest neighbor query", you should find many answers to that question here or anywhere on the web. As for the proposed solution: I'd argue the best solution depends on the number of coordinates. For 1-10, brute force may be best. For 10-1000 (10000?), a grid is probably the best solution. For 10000+ I'd go with a spatial index (R-Tree, kd-tree, quadtree). If you can use a GPU, there are brute force algorithms that beat spatial indexes even for 10000+ points. – TilmannZ Nov 15 '22 at 10:27
  • Thanks everyone. What I am doing is representing a monochrome bitmap of an image as a discrete function where each x,y coordinate pair reflecting a pixel in the image that is "ON" is a datapoint in the set of points. What I am currently doing is given p, start circling around p outwards and test those points for candidates of q, and first found point q in s is the closest point. But this only works for integers... which is actually fine for my application. I think I will try the quadtree approach since I already have an efficient implementation of neighbour finding and such. Thanks! @TilmannZ – ronalama Nov 15 '22 at 20:37

0 Answers0