Python Closest Point in Pointgrid, How to use resulting point again for Closest Point Loop

Hi, you can see in my script that I defined the closest point from one starting point to all other points that are in the grid.
Now I want to use the Closest Point again and search for the closest point … and again the result…

How to define this searching in a for loop? I am tried it for hours :frowning:

Thanks a lot for your help!

EDIT:

Maybe this one is better:

poolunused = sum_attractors[:]
closestattractor = []
dist = 9999999

for n in poolunused:
    ee = rs.Distance(start_point, n)
    if ee <= dist:
        dist = rs.Distance(start_point, n)
        closestattractor = n

in the end of your loop you should redefine your start point to be the found closest point, and then you should also remove the closest point from the point grid array.

1 Like

Yes that is the way, but unfortunatly I do not know how to write this in python. Can you give me an example please?

I dunno Python syntax so great but this is what I would do in C#


SortPath.gh (5.7 KB)

It is very difficult for me to read C# beacuse I am new to Python and it is my first language. Let me try…

Sorry, I do not get it :sweat_smile:

but the redefinition and romoving process should be inside the loop?

Yes, that is generally how recursion works.

Hopefully there is somone who can show me your part in python

But the file is there already.

I am sorry :smiley:

Hi, do you want to exclude all previous closest points from the new search, ie visit all points by closest neighbour to closest unvisited neighbour?

Hi ,
that is totally correct. The following searches should not paying attention to the points that are already used.

Which is what the C# script does, using Point3dList is a good way to find closest point by Closest Index without spending heavy calculations on distance searches.

1 Like

I also noticed the adventage of this code. Regarding to scripting efficiency I would say that python also has adventages that C# does not have. But I think this belongs to the never ending prefered language discussion. Thank you again for your time, hopefully @Dancergraham can help me now?

This advantage is not specific to C#, Point3dList and Closest Index are part of Rhinocommon, they can be used in Python also.

sorry, yes that is a way better idea

These notes should help you, Just change the syntax of each of those steps into the Python syntax. The Rhinocommon does not need to change.


SortPath-with Notes.gh (7.9 KB)

1 Like

something like this might work, (first draft, might not be right). One advantage that C# has over Python, is the incrementer i++, that can be put right in the initialization of the loop. Python, (I don’t think…), has an equivalent ability, so you have to do it “manually”.

while len(pts) > 0:
    counter = 0
    for pt in pts:
        if counter == 0: StartPt = StartPt
        else: StartPt = a[len(a)-1]
        ClosestPt = Rhino.Collections.Point3dList.ClosestIndexInList(pts, StartPt)
        a.append(pts[ClosestPt])
        pts.pop(ClosestPt)
        counter +=1
        print StartPt

pts input type hint, point3d, list access
StartPt input type hint, point3e, item access

1 Like

This almost works for me. Awesome! But there is still the problem that my start_point0 is not the start of the string. It is just one of them inside . See attatched picture

Code:

poolunused = sum_attractors[:]
pairs = [] 
while len(poolunused) > 0:
       counter = 0
for pt in poolunused:
    if counter == 0: start_point0 = start_point0
        else: start_point0 = pairs[len(pairs)-1]
        ClosestPt = rc.Collections.Point3dList.ClosestIndexInList(poolunused, start_point0)
        pairs.append(poolunused[ClosestPt])
        poolunused.pop(ClosestPt)
        counter +=1
print start_point0