> For the complete documentation index, see [llms.txt](https://gitbook.testingwaters.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.testingwaters.in/geometry-1/pair-nearest.md).

# Pair Nearest

![](/files/-MCeSOWcJS7Z8OJ2ATqF)

```d
s = PolyCurve.ByJoinedCurves(List.Shuffle(
(Rectangle.ByWidthLength(10..20..#5,5).Translate
(0..40..#5,0..40..#5,0))).Explode());

def cns(cr:var[]..[], oc:var[]..[])
{
	a = List.SetDifference(List.RestOfItems(cr),oc);
	b = cr[0].DistanceTo(a);
	c = List.SortByKey(a,b)["sorted list"];
	return List.FirstItem(c);
};

s;
nearestPairs = [Imperative]
{
	t = [];
	i = 0;
	while (List.Count(t)<List.Count(s))
	{
		d = cns(List.ShiftIndices(s,-i),t);
		t = List.AddItemToEnd(d,t);
		i = i + 1;
	}
	return List.Transpose([s,t]);
};
```

{% file src="/files/-MCeSZFsTgwtzaFJ4z2a" %}
Dynamo version 2.8
{% endfile %}
