# Creating random rectangles in a grid

Hello:)

It’s been 4 days I am struggling to figure out an idea and pretty sure that you can help me with.
I have been trying to find an algorithm or script to start from the corners of the irregular grid and randomly cull the rectangles and keep some, based on these rules: 1. they should start growing from corners and at least have 2 rectangles attached to the boundary 2. they should be rectangle but the orientation doesn’t matter(0 or 90) 3. 4 side should start at the same time but they should keep one block distance with others(the group of rectangles from others corners.) and it will stop when they reach defined number of the rectangles. I attached the picture to make it clearer.
thank you sooo much in advance.

Hello
you need to learn to code (Python, VB, C#). You have the rough algorithm described by you. If you can work with some size defined by integer and not double I’ll use a big 2D array containing bools. You’ll find example in some of my script.

Great, thank you so much, yes I also tried something with python(I’m not really familier with c# though) which didn’t work but the first link that you shared is super close to what I want, except that I in my scenario rectangles generation should start from the boundary, can you please tell me which part of the code I should look at to add or change it in order to achieve my desired result?

All the best,

and sorry more question, Is it also possible to get the all these random results?

Not super clear if you want to place rectangle or make them grow, the logic will non be the same.
If you are Ok to place rectangle you could reuse this function

``````public bool[,] GetPixelArrayFromMesh(Mesh mesh, double size)
``````

Then you’ll need to fin the index of the corner and try to fit rectangle touching these points. But you’ll have to find a different way than what I use. because I think for me the index (in X and Y) is always the lower left part of rectangle. But for you it could be lower, higher, left or right part of rectangle. So welcome in the algorithmic world. You’ll have to find a strategy, then make class/function that helps to make the problem more easy to solve.

Draw what you want, step by step, find the logic the algorithm will appears to you
for example
Let say I begin with 1x1 rectangle placed at each corner
For all corners
__ place a 1x1 rectangle that fits

While there is still a rectangle is growing
__For all rectangles
____Grow rectangle randomly

Function random grow
___Grow randomly a rectangle of 1 unit on left or right or up or down

oh yea you’re right, but I think in my case placing them would make more sense.
what I want is something like this:

so that’s why I think yours is quite similar to what I want except the part that defines the target point for their movement, for this I should add “public bool[,] GetPixelArrayFromMesh(Mesh mesh, double size)” to the code?

I think you first rule is gonna conflict with the second because, the 1st rectangle in the corner is a special case, also you need a way keeping track of every group of rectangles, I would personally use the union polygon for this(So that you dont need to keep track of internal edges). And you need some placement rule, you said that every rectangle needs to have at least two neighbors. Normally when you do nesting algorithms you need a placement criteria, so that you check that criteria every time you place a new geometry. In Polygon Nesting that Criteria is a measurement of the convexity, and wat you do is you calculate that criteria all around group polygon, to see which new polygon gives more a convex result. In this case because you want random rectangles you need a way measuring randomness or something like that, so that you check which of the neighbors is more random and select that one. You can also just select randomly out all the neighbors, but I have the suspicion that this method is not gonna give you the expected result. Because after the first rectangle the grow is gonna be bias towards one side and well that is not really gonna look random.

Also using only the 2 neighbors criteria is not gonna work(grow) on corners. because when there is 4 rectangles together in grid like in this image, all the rectangles satisfy the criteria so you cant really place any other around them, look at the image:

Im wondering wat the expected result should look like in a bigger grid with more loops, because the 2 example drawing you upload, they maybe have like 2 or 3 loops, and you need a bigger case so that we can understand wat the expected result is. Maybe just run your algorithm by hand on a drawing, maybe for 5 to 8 loops to see how it should look like. The reason why Im telling you this is because based on your rules you could have an algorithm that endup looking like is growing radially(this result would look just like voronoi on a grid) instead of linearly like nesting

Hello @laurent_delrieu, first of all great job, I was wondering if it’s possible to change the size of the plot rectangle and also size of the divisions? I was playing around with it and although I changed them still the sample rectangles that you set will appear as the result. I would really appreciate it if you can help me with this.

All the best,