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

Here’s the script from above with @Dancergraham’s snippet added and it works:

``````import Rhino.Geometry as rg

def find_closest_points(start_point, point_cloud, max_recs=float("inf"), _depth=0, _path=[]):
"""Recursively searches a point cloud for consecutive closest points.

Args:
test_point: A point to search from.
point_cloud: A point cloud to search.
max_recs: An optional maximum number of recursions.
_depth: A current recursion depth.
_path: A current nested list of point pairs.

Returns:
The nested list of consecutive point pairs.
"""
if point_cloud.Count > 0:
pt_cloud = point_cloud.Duplicate()
closest_idx = pt_cloud.ClosestPoint(start_point)
closest_pt = rg.Point3d(pt_cloud[closest_idx].X,
pt_cloud[closest_idx].Y,
pt_cloud[closest_idx].Z)
_path.append([start_point, closest_pt])
pt_cloud.RemoveAt(closest_idx)
if point_cloud.Count > 0 and _depth < max_recs:
_depth +=1
return find_closest_points(closest_pt, pt_cloud, max_recs, _depth, _path)
return _path

# Get the start point
start_pt = Points[iStartPoint]
# Create the point cloud
attractor_pts = rg.PointCloud(Points)

# Get the consecutive point pairs
pairs = find_closest_points(start_pt, attractor_pts)
print pairs

# NEW!!!
# Create linear connections
connections = [rg.LineCurve(a, b) for a, b in pairs if a != b]

# Outputs
a = connections
# NEW!!!
``````

This line of code …

`[rg.LineCurve(a, b) for a, b in pairs if a != b]`

… is the same as this longer snippet:

``````connections = []
for a, b in pairs:
if a != b:
line = rg.LineCurve(a, b)
connections.append(line)
``````

`if a != b:` prevents the start point trying to form an invalid line with itself for the first pair.

Cant solve this error

At the end of line 279, your are missing a “)”!

That was a bad one Thank you

It does not correct correctly. The green lines should be equal to the red one(red one i did with gh )

What are you exactly trying to do?

I have a list of points in a special order. It is called poolunused. The list contains a just 3D points. poolunused = [a,b,c,d] (Letters are representing 3D points). From these points I want to have tuples in a list that represents start and endpoint of the order in poolunused.
So I get: = [(a,b), (b,c), (c,d)]
now i want to draw curves from a to b and b to c and c to d

Are your poolunused points in the right order? How do you evaluate these?

yes they are in the right order. the order is represented by the interpolated red curve you can see at the picture

Can you upload a file with just the internalised poolunused points?

``````from itertools import tee
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
``````
1 Like

I did a bad mistake when difining the pairs. I could solve that. Thanks again!!!