Algorithm to fit blocks of random width/height into a grid

Viewed 15

I have a container which is of size 1980x1080. Inside of this container can be N boxes that have a random width/height.

Is there an algorithm or library which automatically figures out the best fitting grid given the elements or arbitrary sizes?

Example code (desired would be the function getGrid())


const positions = getGrid([
  {width: 300, height: 300},
  {width: 500, height: 300},
  {width: 1000, height: 800}
], { gutter: 5, maxWidth: 1980, maxHeight: 1080 })

// It should return the best x/y coordinates for each block
// positions[0] = { x: 0, y: 805 }
// positions[1] = { x: 305, y: 805 }
// positions[2] = { x: 0, y: 0 }
0 Answers
Related