# Sorting Points That Are Not In Order

## Recommended Posts

Hi guys and gals,

I have been sitting with the problem for quite some time and thought it was time to ask some smarter people than myself.

I have a geo that I am wanting to run a curve through the middle of.

What I have managed to do so far is create points running through the center of the geo but the points are not in order. I need them to run in order to create a line out of the points as I am wanting to generate a gradient along the line from point 0 to the last point.

I have attached some screenshots. Any advice would be greatly appreciated!

Thanks

##### Share on other sites

I have attached the hip file.

Thanks!!

Sort Points.hiplc

##### Share on other sites

Gave it a quick try. Its just connecting the closest points. You have to pick a starting point though.

Sort Points_1.hiplc

##### Share on other sites

I hope to one day be the proud owner of a... "_Blender Library/Scans/Donut/Sprinkled_Donut_01_C.OBJ"!

Until then you could use this maybe? It acts wonky unless you flatten the points against one axis, but your thingie looks circular enough for it to work.

I tried to copy your geometry and it sorted it alright.

sortish.hiplc

##### Share on other sites

Here is one solution.

Here is a really nice blog on connecting points:

There is an example file with it too.

##### Share on other sites

Thank you so much for that hip file!

I have been through it using the F1 key on each expression to try and understand but it's going over my head a little.

I'm sure I'll understand it in the future but if you have some time and if you wouldn't mind, would it be possible to comment on each line of code to explain what it's doing?

If not I totally understand and this is already awesome! Will just take a while to sink in. Thanks for the help!

##### Share on other sites

```// Relative Neighbourhood Graph

// https://houdininote.tumblr.com/post/162427874300/connecting-points

// This wrangle runs thru all points.
// Current point = point being ran thru
// 'nearpoints' gives you a list of point numbers near the current point

foreach(int pt; nearpoints(0, @P, 1.0, 10)) // For every point 'nearpoints' finds in the given radius..
{
if(pt == @ptnum) continue; // Skip itself, go to the next iteration

vector p = point(0, 'P', pt); // Store position of the near point
float dist = distance(p, @P); // Store distance (mydist) from current point to near point

// Building 2 new arrays...
// We will search for any points within the radii (mydist) of both the current and near points
// to check if they are closer to each other.

int ptsA[] = nearpoints(0, @P, dist); // Look for any more points around the current point within mydist...
int ptsB[] = nearpoints(0, p, dist); // Then, look for any more points around its near point within mydist.

int chk = 0;  // Initialize a flag

foreach(int cpt; ptsB) // For every point (cpt) found in our 'ptsB' array..
{

if(cpt == @ptnum || cpt == pt) continue; // if cpt is the current point or near point, skip it.

chk = removevalue(ptsA, cpt); // Remove cpt from our 'ptsB' array. If successful, set our flag to TRUE.
if(chk) break; // If we successfuly removed cpt, break out of this nested loop.
}

// If our list 'ptsA' is empty, lets draw a line between the current point and the near point.
// If there were points left in the list, no line will be drawn.

if(!chk) // If we failed to remove a value previously...
{
int pr = addprim(0, 'polyline'); // create a new empty polyline
addvertex(0, pr, @ptnum);  // Add a new vertex to that polyline at the position of the current point
addvertex(0, pr, pt); // Add another vertex to that polyline at the position of the near point
}
}```

Im not the original author of this code. Its an example I pointed to in that link.

##### Share on other sites

Thank you so much Ryan!

I appreciate the commenting on the code!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×