# Consecutive Points

![](https://1430428134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ld8QK19sjP1I9rhLouo%2F-MGig2qw4WChC1Aspqzx%2F-MGigRmhD_eQS0SYWv28%2FclosestPointsSequence_2020-09-08_10-14-58.png?alt=media\&token=57daf44a-2361-4099-8e68-bead221a56df)

{% file src="<https://1430428134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ld8QK19sjP1I9rhLouo%2F-MGig2qw4WChC1Aspqzx%2F-MGigbyfvFPMwaqzXVju%2FclosestPointsSequence.zip?alt=media&token=6f571fd5-9a6b-4687-91be-5ae0a31bc117>" %}
Dynamo 2.9
{% endfile %}

```d
def ClosestPointsSequence (stPt, pnts:var[]..[])
{
	return = [Imperative]
	{
		cnt1 = List.Count (pnts);
		pnt1 = pnts;
		pnt2 = [stPt];

		while (List.Count(pnt2)<cnt1)
		{
			pnt3 = List.SetDifference(pnt1,pnt2);
			pnt4 = List.LastItem(pnt2);
			dis1 = pnt4.DistanceTo(pnt3);
			pnt5 = List.SortByKey(pnt3,dis1)["sorted list"];
			pnt1 = List.RestOfItems(pnt5);
			pnt2 = List.AddItemToEnd(List.FirstItem(pnt5),pnt2);
		}

		return pnt2;
	}
};
```

This can be extended to obtain the shortest path connecting a list of points by obtaining sequences with all possible start points and then calculating the length of the connecting poly line

![](https://1430428134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ld8QK19sjP1I9rhLouo%2F-MHCmX6xzJ3z0YOwBzIg%2F-MHCn_OR3YdZN4YvuWrY%2FshortestPathConnectingPoints_2020-09-14_07-48-45.png?alt=media\&token=e989d453-127a-4657-8b40-faf4d87e80ba)

{% file src="<https://1430428134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ld8QK19sjP1I9rhLouo%2F-MHCmX6xzJ3z0YOwBzIg%2F-MHCnm7qOMKPs3VWP4ZJ%2FshortestPathConnectingPoints.zip?alt=media&token=4872d4b2-6610-4f95-a2c0-378c1aa3c058>" %}
Dynamo 2.9
{% endfile %}
