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.
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.
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.