here is an implementation in macro language for k-neasrest neighbor
```lang=javascript
// clear environment
run("Close All");
roiManager("Reset");
// open an image
run("Blobs (25K)");
run("Invert LUT");
// Blur (denoise) and find Local Maximas
run("Gaussian Blur...", "sigma=1");
run("Find Maxima...", "noise=50 output=[Point Selection]");
// from the existing point
getSelectionCoordinates(xCoord,yCoord);
totalPoints = lengthOf(xCoord);
// specify the min and max distance
minDistance = 15 ;
maxDistance = 100;
// and the Nbr of neighbor
k = 3;
// go throuhgh
for (i = 0 ; i < lengthOf(xCoord) ; i++){
// define current point of Interest
xCurrent = xCoord[i];
yCurrent = yCoord[i];
// defime an array with the size of the totalPoints
allNeighBor = newArray(totalPoints);
// and fill it with the maximum distance allowed
Array.fill(allNeighBor, maxDistance);
//
for (j = 0 ; j < totalPoints ; j++ ){
if ( i != j){
// euclidian distance calculation
distance = sqrt ( (pow ((xCurrent-xCoord[j]),2)) + (pow ((yCurrent-yCoord[j]),2)) );
// check if above the minimal distance
if ((distance > minDistance )&&(distance < maxDistance )){
allNeighBor[j] = distance;
}
}
}
// get the rank positions of the all the neighbor
rankPosArr = Array.rankPositions(allNeighBor);
// make the correspondings lines and add to the manager
for (jj = 0 ; jj < k ; jj++) {
makeLine(xCurrent,yCurrent,xCoord[rankPosArr[jj]],yCoord[rankPosArr[jj]]);
Roi.setName("P"+IJ.pad(i,5)+"-N"+IJ.pad(jj,5));
roiManager("Add");
}
}
roiManager("Show All without labels");
```