Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Find the pair of points with the closest distance
ResourceFunction["ClosestPairOfPoints"][{p1,p2,p3,…}] finds the pair of points from pi that are closest to each other. | |
ResourceFunction["ClosestPairOfPoints"][{p1,p2,p3,…},"Association"] returns an Association with the indices of the points, the points and the distance. |
Given four input points, find the closest in 2D:
| In[1]:= |
| Out[2]= |
Visualize the closest pair:
| In[3]:= |
| Out[3]= | ![]() |
The function can also handle many points:
| In[4]:= |
| Out[5]= |
Visualize the closest pair:
| In[6]:= |
| Out[6]= | ![]() |
Get the indices, the points, and the distance:
| In[7]:= |
| Out[8]= |
Find the closest points in 3D:
| In[9]:= | ![]() |
| Out[10]= | ![]() |
The function can also handle high-dimensional points:
| In[11]:= |
| Out[12]= | ![]() |
The function can be naively implemented by checking all pairs:
| In[13]:= | ![]() |
| Out[14]= |
The difference is in the speed:
| In[15]:= | ![]() |
| Out[15]= |
Visualize the difference in speed:
| In[16]:= | ![]() |
| Out[17]= | ![]() |
The dimension can exceed the number of points:
| In[18]:= |
| Out[19]= |
When no points are given a Failure object is returned:
| In[20]:= |
| Out[20]= |
Also a single point is not enough:
| In[21]:= |
| Out[21]= |
For 1D, the coordinate should be between braces:
| In[22]:= |
| Out[22]= |
Like this:
| In[23]:= |
| Out[23]= |
Check the method against a naive implementation:
| In[24]:= | ![]() |
| Out[24]= |
Wolfram Language 14.0 (January 2024) or above
This work is licensed under a Creative Commons Attribution 4.0 International License