Blender 3.6+
This answer is technically the same solution as shown here:
How could I get the distance of a point to its nearest point?
With Blender version 3.6 a new node is available that makes this task a lot easier: Index of Nearest.
The node Index of Nearest
simply returns the index of the nearest point, which makes it just as easy to query its position with Sample Index. Then you just need to instantiate lines, and move their endpoints to the position of the nearest point:
Blender 3.1+
The desired result can be achieved with the technique The shortest edge trick.
All you have to do is to convert your randomly generated points into a triangulated mesh and apply the following principle:
If you use the node Distribute Points on Faces
, you can achieve the triangulated mesh you need in this way, for example:
But if you use the node Distribute Points on Faces
to create only the points, there is also a convenient alternative: The node Voronoi Texture
.
If you only need randomly distributed points that you want to connect, it is sometimes easier to create a triangulated mesh directly instead of turning points into a triangulated mesh.
Here's an example of how that can work:
Once such a mesh is available, and this is actually the more complicated part in this case, it is easy to find out the shortest distance.
The procedure in this case would be as follows:
Create your points on a grid
Create the triangulated mesh
Shorten the edges by a certain factor
Find the nearest edge and instantiate your lines there.
Here is the blend file: