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

python
(STUDENT_F) #1

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
0 Likes

(Michael Pryor) #2

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

(STUDENT_F) #3

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

0 Likes

(Michael Pryor) #4

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


SortPath.gh (5.7 KB)

0 Likes

(STUDENT_F) #5

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

0 Likes

(STUDENT_F) #6

Sorry, I do not get it :sweat_smile:

0 Likes

(STUDENT_F) #7

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

0 Likes

(Michael Pryor) #8

Yes, that is generally how recursion works.

0 Likes

(STUDENT_F) #9

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

0 Likes

(Michael Pryor) #11

But the file is there already.

0 Likes

(STUDENT_F) #12

I am sorry :smiley:

0 Likes

(Graham) #13

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?

0 Likes

(STUDENT_F) #15

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

0 Likes

(Michael Pryor) #16

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

(STUDENT_F) #17

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?

0 Likes

(Michael Pryor) #18

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

0 Likes

(STUDENT_F) #19

sorry, yes that is a way better idea

0 Likes

(Michael Pryor) #20

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

(Chris Hanley) #21

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

(STUDENT_F) #23

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
0 Likes