Traveling salesman / conditional / python functions / gcode generationgra

I am trying to add a function to an existing traveling salesman drawing function that will draw a continous line for 3d printing a randomly generated point cloud. According a given layer height the traveler should not choose the closest one that is higher than that value. It works to some level but I would like to get some suggestions on writing a simple algorithm on this. Here is GIF made to demonstrate what I want and the GH file .

ASYA (7.4 KB)