Order points around a rectangle

Hi, I have a list of points which I know sit on the perimeter of a rectangle. They’re currently in a random order but I’d like to reorder them in as if you cycle round the perimeter of the rectangle in a clockwise fashion.

The best way I could think of is to divide the list into 4 groups based on which of the 4 edges they sit on, sort them, then recombine them. My script is failing at this stage - I can’t get it to happily find the points that sit on the Y=0 edge. What am I missing? I’ve deconstructed the points, and asked it to list those that = 0, but it’s not picking them up.

Is this even the best way to solve the problem?

You are looking for the Sort Along Curve component that does exactly this.

Hi @Derek4 ,

What @Volker_Rakow said but sometimes I also take my unsorted points and use a Fit Circle or Fit Line component on the points and then use that curve as the input for the Sort Along Curve component.

Depending on the shape of the points being sorted this will sometimes yield much more consistent results.

1 Like

Yes. But, here that won’t be necessary.

Perhaps I should mention that to get the points to order themselves in the “clockwise” direction along the rectangle, you might have to change the direction of the rectangle curve with Flip Curve

1 Like

Thanks, no idea why that didn’t come up in my searches!

Just for my own learning, any idea why my initial plan wasn’t working and it wasn’t picking up the points on the edge?

Edit: Haven’t drilled into it, but I suspect it may be a floating point rounding issue.

shouldn’t the Equality component work for points…? - Grasshopper (grasshopper3d.com)

One suggested fix is to push the data through a text string node which makes them more literal - that seems to have fixed it.

that’s a clever one, thanks I can use that

That and 0 as a float vs 0 as an integer vs 0 as text don’t return matches in all components.

I usually cast both to text or int. If floating (number) I usually do text comparisons for more “literal” accuracy