What is the best (or at least a good enough) algorithm for automatically positioning images within a CSS sprite?

StackOverflow https://stackoverflow.com/questions/283219

  •  08-07-2019
  •  | 
  •  

Question

I have written a CSS sprite auto-generator which takes selected images out of the HTML page and converts them to CSS sprites, but right now it does not attempt to lay them out optimally but rather just stacks them, which wastes a lot of space. What would be the best algorithm for determining the optimal layout?

To state the problem more generally, I need an algorithm that, given any number of rectangles of arbitrary size, will arrange them into a rectangle with the smallest possible area.

Was it helpful?

Solution

This has been here already: Stacking rectangles to take as little space as possible.

Maybe that's what you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top