0

Lets say I have a number of points on a 2D canvas. Presumably there's a way that avoids searching through all of the coordinates for the nearest one to a particular set of coordinates (eg a mouse click). Is there?

Thanks.

Richard
  • 4,809
  • 3
  • 27
  • 46
  • Generally it can be achieved, for example by indexing the points with a B-tree. But I don't know what kind of features that canvas you use has got, maybe it has builtin function for something like that. Is it an HTML5 canvas? – Zoltan.Tamasi Mar 02 '13 at 14:54
  • 1
    Have you checked http://stackoverflow.com/questions/1901139/closest-point-to-a-given-point – Shiva Kumar Mar 02 '13 at 15:00

1 Answers1

0

The best algorithm depends on the number of points you expect to compare, how often these points are moved and searched, how important speed of re-indexing and searching are, and what language you're using. As @Sylvanus pointed out, there may be language or library calls that can help. In all likelihood a quad tree will be the easiest to understand, while being about as efficient all round as possible. @Shiva Kumar gives an excellent comprehensive set of possibilities (although there are still more ways.) You should do a google search to see how the problem has been solved for the language and environment you're programming for.

Jodes
  • 14,118
  • 26
  • 97
  • 156