To be honest, I'm now a little stunned myself at how complicated the answer seemed at first, and how simple the solution is in the end.
The basic idea here is to shorten all edges between the points proportionally, and thus find the closest point. This method saves a lot of calculations and complexity and is basically applicable to any shape (grid, sphere, cube, etc.):
Here we go:
First create your points with
Distribute Points on Faces
.In the case of a sphere, the
Convex Hull
node makes it easy to obtain the required triangulated mesh without missing a point. For other shapes, it is best to create the mesh using theTriangulate
(Shortest Diagonal) node.Using the nodes
Extrude Mesh
,Split Edges
andSeparate Geometry
you get the isolated edges of this mesh.Then reduce the scale of each edge by half.
Now that the edges are reduced in proportion to their length, you can reliably find the nearest point with the node
Geometry Proximity
. If you then calculate the direction vector between your originally created points and the position results ofGeometry Proximity
, you will know in which direction the shortest vector points.In the last step you only have to correct the length. Since you have shortened the edges by 50% before, you simply scale the direction vector by $4$, which is exactly the point you were looking for (Apart from a few minor rounding errors).
The final result is this (Each previously created point is here connected to the nearest point):
...and with animated Seed/Density it looks like this:
Here is an overview of the node group:
Here is the blend file (I added an additional view for debugging):
...and as a bonus I added the animation to the blend file too, because it's so nice to see the thing in motion (even though I won't win any beauty contests with the node tree, but it's meant as a little animation example).
Useful Hints
- This solution works best by converting a mesh into triangles with the shortest edge length! Quads are less suitable here, because they may produce false positives.
- If you do not use a sphere, it is best to create the mesh using the
Triangulate
(Shortest Diagonal) node.- If you use a sphere, this works best with a sphere of the type
Ico Sphere
in a higher resolution.- Remember: If you use a sphere like in this example, the calculated distance is of course also the shortest straight path between the points. The real distance on a sphere would actually be the angle between the points multiplied by the radius of the sphere. The angle is obtained with the formula: $\alpha = 2 \ast \arcsin (\frac {s}{r \ast 2})$