# How to fit rectangles in a page?

Here's my problem: I need to fit rectangles in a polygon but in "certain way".
I have few rectangular shapes (of different sizes) and I need to put them on a polygon.

My first idea was to always try to put them on the top right of the previously placed shape, and if it is not possible, on the bottom left angle of the previously placed shape. It works but it does not looks great, since it does not take into account the placement of the previously placed shape (sometimes the last placed shape is not the most logical).

From this, I imagine we could have a kind of "navmesh": when a shape is placed, it says that nothing more can be placed here and that it needs to find a better place.

Here is how it should look:

Imagine I have the above case, and I want to add one rectangular shape... it shoud go like this:

... with enough space to welcome a new rectangular shape that could possible fit on the blank space.

So... what I want is to place my shapes on the far top and then on the far left.

Do you guys have any ideas how to do this? Maybe this kind of algorithm already exists and I don't know the name?

Thanks !

• When you say ' I need to fit rectangles in a polygon but in "certain way"' do you mean that they should be inside the polygon in such a way that they are "sorted" ? Or can they just be randomly inside, as long as they don't collide with each other ? Also when you say "inside a polygon" do you mean the shape could be a triangle or another polygon, or is it always a rectangle? Jun 13, 2018 at 13:46
• By a certain way I mean sorted yes, like if you were placing photo inside a book. And yes, when I say polygon, in my case it will always be a rectangle. thanks!
– Izzo
Jun 13, 2018 at 13:56