Select closest points from a given list

Hello,

I am wondering if it is possible to divide a list of points into sublists, where points are sorted into the sublists by distance between them.

For example I have this list of random points:

15.0, 18.0;
16.0, 9.0;
11.0, 12.0;
12.0, 12.0;
16.0, 3.0;
17.0, 3.0;
10.0, 6.0;
5.0, 15.0;
6.0, 15.0;
16.0, 2.0;
17.0, 2.0;
1.0, 3.0;
2.0, 3.0;
17.0, 9.0;
16.0, 10.0;
1.0, 2.0;
2.0, 2.0;
6.0, 7.0;
10.0, 7.0;
11.0, 7.0;
5.0, 8.0;
6.0, 8.0;
5.0, 7.0;
5.0, 16.0;
6.0, 16.0;
14.0, 18.0;
17.0, 10.0;
11.0, 11.0;
12.0, 11.0;
11.0, 6.0;
14.0, 19.0;
15.0, 19.0;

And here is a needed output, where distance between points in a sublist is 1.0 (in both directions X and Y):

{1.0, 2.0; 2.0, 2.0; 1.0, 3.0; 2.0, 3.0}
{16.0, 2.0; 17.0, 2.0; 16.0, 3.0; 17.0, 3.0}
{10.0, 6.0; 11.0, 6.0; 10.0, 7.0; 11.0, 7.0}
{5.0, 7.0; 6.0, 7.0; 5.0, 8.0; 6.0, 8.0}
{16.0, 9.0; 17.0, 9.0; 16.0, 10.0; 17.0, 10.0}
{11.0, 11.0; 12.0, 11.0; 11.0, 12.0; 12.0, 12.0}
{5.0, 15.0; 6.0, 15.0; 5.0, 16.0; 6.0, 16.0}
{14.0, 18.0; 15.0, 18.0; 14.0, 19.0; 15.0, 19.0}

Iâ€™ve attached an image to visualize my question.

The language doesnâ€™t matter - I just canâ€™t start with the logic. If itâ€™s important, I am working in C#.

Thank you in advance for any ideas!

Good news.

You have 3 options (in fact 4):

1. The classic one is to Cluster your collection according to a distance filter (and maybe some more).Thatâ€™s the recommended way to cut the mustard for similar matters - because Clustering is a paramount thing in engineering.
2. An other is some sort of LINQ query (may be 100% cryptic if you are not familiar with queries).
3. An other is to create a suitable Class that assigns a prox distance Property to each object - meaning creating a List named, say, classList - and then do a classic LINQ thingy:
var query = classList. CroupBy (x => x.Dist).ToList();
4. BUT â€¦ If your goal is to create random islands of points â€¦ well â€¦ all the above are just useless reverse engineering - thereâ€™s numerous other ways to do that.

Notify which C# you want

1 Like

Thank you very much for your tips!

Here is the needed tutorial, that solves a problem similar to mine:

K Means Clustering is NOT the way for that kind of stuff (for obvious reasons: you donâ€™t know the N of clusters). That said Clustering - in general - is far away and above from the scope of KM.

See attached (is an Interview C# for my people in the practice - so has various obvious [and hidden] challenges for the brave).

Clusters_PtsByMinDistance_V1.gh (140.2 KB)

BTW: What are you after is called rnd pts collections with density spots. When Iâ€™ll be in the practice Iâ€™ll find a simple entry level thingy on that matter.

I think â€śPoint Groupâ€ť component is what you need.

1 Like

point group python code.gh (7.2 KB)
If you like to try it with your own code, here is one simple example by python.

1 Like

It is much closer to what I need

Thank you @11165 for the Python solution!

Found a couple of minutes to add (Help C# to the left) another option (4). Meaning that you have 2 options in total (3, 4) where you donâ€™t need at all any clustering - since the pts output is in a DataTree.

Clusters_PtsByMinDistance_V1A.gh (137.1 KB)

Option 4 demos:

2 Likes

One question, is there a reason why you used the Matrix class instead of a jagged array?

Not really other than I have lotâ€™s of Matrix related Methods (for far more elaborated results - the internal ones, that is)

ah,ok!