0

Problem Description:

For a small graphic program I want to split an arbitrary sized canvas in an arbitrary number of tiles, whereas the tiles should be quadratic and as big as possible. Naturally there will be borders either on top and bottom - or - at the sides, they should be left blank.

**given:**

width
height
tiles_nr

**wanted:**

tile_size
tiles_nr_x
tiles_nr_y

Example:

I have a Canvas of 800x600 px. I want to place 30 tiles in that area. How big will be one tile? And: How are the tiles distributed in rows and columns?

My appoach so far:

tile_nr_x * tile_nr_y = tiles_nr
tile_nr_x * tile_size <= width
tile_nr_y * tile_size <= height

Are these equations enough to determine the variables? How can this system of equations be solved and implemented in program code?

Anton Harald
  • 5,772
  • 4
  • 27
  • 61
  • do all tiles have to have same dimensions or tile dimensions can vary from tile to tile? in the first case it is very simple, in the second case it is actually a variation of knapshack problem and requires other solutions – Nikos M. May 16 '15 at 00:45
  • http://stackoverflow.com/questions/22814546/find-an-optimal-n-square-size-same-for-each-to-fit-most-part-of-rectangle-cont/22815562#22815562 – MBo May 16 '15 at 04:02
  • Closing as duplicate, also see http://en.wikipedia.org/wiki/Square_packing_in_a_square –  May 16 '15 at 04:02

1 Answers1

0

You can try a treemap. Sort the tiles and pick the first and create a node in a tree. Split the tree on both axis and pick the next tile and find the best fit of the nodes.

Micromega
  • 12,486
  • 7
  • 35
  • 72