[Lua help] Function to jigsaw rectangles as closely / optimally
This is a rather general maths and Lua question, but since it's for a Minetest mod I posted here. I've been struggling to figure out a formula for hours but with no success, so I need help. This is the problem:
Let's say I have a table consisting of other tables, each containing x and z coordinates of a rectangle's size. For example:
The 'rectangles' table would contain any count of such sub-tables, each specifying various sizes. Next I have a vector position which is a location in the world. I want to add all rectangles in such a way to fill an area from that position (left-to-right and top-to-bottom) and have as little empty spaces between the rectangles as possible, aiming for a rectangular pattern to place them in. Each rectangle is ok to place anywhere randomly as long as this purpose is met, but must never overlap another. My aim is to obtain the center origin (node) at which I'd have to place each rectangle as x and z coordinates from this calculation. Since Lua is slow so we'd want as few loops and checks as possible.
Imagine you have a rectangular table. You also have a number of paper cutouts which can be of any size and shape, but all of them are squares / rectangles. You need to jigsaw them in such a way to leave as little empty spaces between them, starting from the upper-left corner of the table. All of them must be used up and their placement should resemble a square / rectangular coverage. The end objective is to know the center of each rectangle.
Can anyone figure the Lua formula for doing this? It would allow me to have a much more correct pattern in my Structures mod and an optimal natural villages spawn system. Thanks.
Let's say I have a table consisting of other tables, each containing x and z coordinates of a rectangle's size. For example:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local rectangles = { }
table.insert(rectangles, { size_x = 10, size_y = 15 }
The 'rectangles' table would contain any count of such sub-tables, each specifying various sizes. Next I have a vector position which is a location in the world. I want to add all rectangles in such a way to fill an area from that position (left-to-right and top-to-bottom) and have as little empty spaces between the rectangles as possible, aiming for a rectangular pattern to place them in. Each rectangle is ok to place anywhere randomly as long as this purpose is met, but must never overlap another. My aim is to obtain the center origin (node) at which I'd have to place each rectangle as x and z coordinates from this calculation. Since Lua is slow so we'd want as few loops and checks as possible.
Imagine you have a rectangular table. You also have a number of paper cutouts which can be of any size and shape, but all of them are squares / rectangles. You need to jigsaw them in such a way to leave as little empty spaces between them, starting from the upper-left corner of the table. All of them must be used up and their placement should resemble a square / rectangular coverage. The end objective is to know the center of each rectangle.
Can anyone figure the Lua formula for doing this? It would allow me to have a much more correct pattern in my Structures mod and an optimal natural villages spawn system. Thanks.