Wolfram Technology Guide: Integrated Geometric Computing  previous | next 
Geometric Searching for Nearest Neighbors
Mathematica provides state-of-the-art tools for nearest-neighbor finding in any number of dimensions, and with any distance function.
In[1]:=

Click for copyable input
data = Table[RandomReal[{-4, 4}, {2}] -> i, {i, 40}];
In[2]:=

Click for copyable input
nf = Nearest[data, DistanceFunction -> (Norm[#1 - #2, Infinity] &)];
In[3]:=

Click for copyable input
ContourPlot[First[nf[{x, y}]], {x, -5, 5}, {y, -5, 5}, 

 PlotPoints -> 50, Contours -> Range[1/2, 40], 

 ColorFunction -> "Pastel", 

 Epilog -> {Red, PointSize[Large], Point[First /@ data]}]
Out[3]=