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

Hi @PeterFotiadis
Thank you very much for your tips!
Already the first one about clustering has helped!

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

Thank you @PeterFotiadis
It is much closer to what I need :+1:

Thank you @11165 for the Python solution!
It should be also helpful :rocket:

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!
Thanks for the answer Peter!

Given the opportunity here’s a challenge for you.

See this? Is a naive implementation of attractors in rnd pts. Why naive? Because pts [pull] overpass the attractor (kinda the Event Horizon in a black hole) and we’ve got bananas (but OK for artistic purposes): Plus the push mode is stupid as well.

But … well … we are not artists (in fact I hate contemporary art: just a product). So do the right thing: mastermind a way where the point never passes any pulling attractor (and solve the push option properly):

So … start from that and do the required mods (is the weighted sum pts problem):
RndPts_ProbDensity_V1.gh (126.5 KB)

1 Like