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[11]= | ![]() |
The function can also handle high-dimensional points:
| In[12]:= |
| Out[13]= | ![]() |
The function can be naively implemented by checking all pairs:
| In[14]:= | ![]() |
| Out[17]= |
The difference is in the speed:
| In[18]:= | ![]() |
| Out[18]= |
Visualize the difference in speed:
| In[19]:= | ![]() |
| Out[20]= | ![]() |
The dimension can exceed the number of points:
| In[21]:= |
| Out[22]= |
When no points are given a Failure object is returned:
| In[23]:= |
| Out[23]= |
Also a single point is not enough:
| In[24]:= |
| Out[24]= |
For 1D, the coordinate should be between braces:
| In[25]:= |
| Out[25]= |
Like this:
| In[26]:= |
| Out[26]= |
Check the method against a naive implementation:
| In[27]:= | ![]() |
| Out[27]= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License