# Sorting Points by Minimum Distance Along Polycurve

Good morning all-

*I have searched this forum extensively, for days, so I apologize if this question sounds familiar. I am struggling.

I am trying to plot a polycurve along a set of points, using:

I can sort by X, Y, Z- but that doesn’t keep the points in order of minimum distance, and produces a fairly sporadic looking set of lines.

Maybe I’m overlooking something simple. How could I make that happen?

you can use sortByKey … distance to

SortByLength.dyn (18.7 KB)
KR
Andreas

1 Like

Thank you so much for your help.

That made me realize I’m approaching the problem incorrectly.

This is an example of what I am working with:

This is what I would like to happen:

So maybe it’s not the shortest distance.

Any suggestions?

1 Like

like this

KR
Andreas

I appreciate your help. I might have to go back to the drawing board with this one.

This is what I’ve got:

And this is the line it’s giving me:

2 Likes

make you own line f.e. best fit.

KR

Andreas

2 Likes

It seems like there would be a simpler way to sequence a set of points. It’s borderline ridiculous.

This is certainly a problem that is more complicated than it initially appears, but it’s still fairly straightforward if you can lay out the logic of what’s happening. As long as you have a known start point, you can work from the current point to the next closest point. You just have to be sure to remove “used” points from the pool of available options after each step. That’s the part that can get a little tricky.

I’ve shown one option below that gets all the distances up front and sorts them by closest adjacent point for each of the possible locations. The python script just steps through each group of potential points and grabs the first (closest) point that isn’t already in my list of “used” points. I did this with indices instead of actual points to hopefully explain the situation a little better and make the next part easier. As you step through and grab an index (point), you use that index as the group to check next. You’re left with the index of the closest point for each of the original points as they’ve been stepped through.

Python
``````orderedDistances = IN[0]
sortedOrder = [0]
i = 0
n = 0

while n < len(orderedDistances):
distances = orderedDistances[i]
for d in distances:
if d not in sortedOrder:
sortedOrder.append(d)
n+=1

OUT = sortedOrder
``````
3 Likes

Nick- thank you so much. This is like being able to see the horizon.

This is my current setup:

This is my implementation of the script:

My values look perfect. However, every time it hits the script- Revit crashes.

Did I miss anything?

Again- thank you.

Sorry, I forgot to format my python code initially. Make sure the increment to `n` is under the `while` loop and not the `if` loop. I also noticed that you may have redundant points (your first group has two points that are 0 units away). It would probably help to clean those up first. You may also have issues if you have points that are equidistant from your current location as you may get the wrong branch.