Sort array of rectangles to cluster colours together

I have a need to reorganise coloured rectangles in an array so that each colour clusters around a specified home point for that colour:

(The Before rectangles could be placed more randomly, neat blocks were quicker to draw.)

Before I start writing Python, are there any existing grasshopper tools or code that could help with this?


Do you need the resulting clusters to be packed together tightly as your diagram suggests or do you need them to just cluster into looser islands near the home base?

Where is your GH code file?

Seem to me that initial colors are irrelevant? And this is a simple attractor problem:

P.S. I am reluctant to post code when you didn’t bother… But for the sake of others, here it is: (22.0 KB)

1 Like

I think the initial populations (number of squares per color) are fixed ?

Hi Michael,

Thanks for your interest. The clusters have to be packed - every cell in the array is occupied - I’m looking to shuffle things around.

Yes. The array will be fully populated at the start and remain so at the end. The number of colours, number of cells per colour and location of home points will vary from one run to another (There will always be one home location per colour).


Dear Joseph,

As my post said, I was looking to find out if there is any pre-existing code or plug-in that might help before starting to code. I didn’t particularly want to reinvent the wheel. Pasting a stub gh file that generates an array of rectangles is not going to help someone remember a useful plugin.

I wasn’t asking for anyone to write code, but on behalf of the others for whom you did so, thank you.

As far as I’m concerned, justifying your omission of code is worse than not posting any.

:rofl: :rofl: :rofl:

You think it’s funny? Bye-bye.

Let’s add fuel to the fire :rofl:

Now in theaters !

1 Like

I don’t think there is a way to do this without custom code.
Using a bool array to store which cells have been visited, starting from the home locations and adding squares from the sides to the center in an iterative process.

I fear you are right, but I think it will need to be more complicated, with an ability for a set colour to be displaced from a cell because the adjacent colour has run out of room. And perhaps an optimiser to prefer rounded regions to long thin ones. My thoughts are heading towards implementing a state machine to run this rather than simple iteration. First step though will be to come up with robust algorithms. I foresee a lot of work.

Hence the hope someone had already done this…

A rough try.

Code is awful, not optimized, does not always work. But maybe a step in the right direction ?
The idea is to find the square that is furthest away from its home, and try to swap it with the nearest wrong tile from this home - checking it’s not the same color and that the result indeed gives overall a better distance. (17.6 KB)

This seems related to voronoi.
Weighted voronoi maybe, where weights are a dynamic function calculated by counting the amount of cell centers inclusion (area). (maybe a custom kangaroo goal?)

(Let’s try to not step over others trying to help…)

Hi @magicteddy,

Thanks for the example. It is using a component called Rainbow Display which I don’t have. I couldn’t find this via the package manager or food4rhino: do you recall where it came from?


It’s a custom component of mine, I made a change to the file and forgot to remove it when re-uploading :man_facepalming:
It doesn’t affect the main C# method but anyway here is another version with Gradient. (13.5 KB)

1 Like