9

I have implemented a 2-dimensional k-d tree in Javascript (check it out on GitHub), and I am using it for nearest-neighbor searches alongside D3.

I learned that there is a quadtree implementation in D3, but also discovered that the API documentation is sparse and Google searches are not fruitful. I would rather use a well-traveled library than my own reinvented wheel when possible.

How do you perform a nearest neighbor search using D3's quadtree? By nearest neighbor, I mean:

  • Populate the quadtree with 2-dimensional points
  • Search for the quadtree-contained point closest to a new point that does not necessarily exist in the quadtree
4
  • Out of curiosity and nothing else, what are you using a JS K-D Tree for? Oct 4, 2012 at 13:31
  • @Sajjan I have <circle>s in an <svg> and on mousemove the circle nearest the mouse position is highlighted. It is very smooth and scales well because nearest neighbor searches in a 2 dimensional K-D tree are O(log n). Oct 4, 2012 at 21:39
  • Cool! Would it be possible to share your code (unless of course it is proprietary or private),I think I could learn a lot from it. Oct 5, 2012 at 19:30
  • @SajjanSarkar added link to the content of my question, enjoy! Oct 7, 2012 at 7:10

1 Answer 1

6

The brushing demo doesn't actually find the nearest neighbor, but rather finds quadtree points contained in a given rectangle. (Try brushing an empty rectangle and it doesn't necessarily visit its nearest neighbors.)

I forked an example that efficiently finds the nearest neighbor in the quadtree to an arbitrary point - see http://bl.ocks.org/patricksurry/6478178

1
  • Is nearest neighbour search is possible with latitude and longitude on leaflet.js maps?
    – gkuhu
    Nov 2, 2018 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.