3
$\begingroup$

i need to figure out how can i add given number of squares in a rectangle i want to create this. http://i.stack.imgur.com/MHoda.jpg

The whole scenario is to create a function that take a number and then create a layout of it, for a quick reference i did this with predefined values for rows, columns, x&y scale etc. The size of the rectangle is fixed and it will always be something predefined, everything else need to be calculated with these values. Can anybody help me please??

1 Answers 1

2

The first step is to figure out how many rows $R$ and columns $C$ you want. If you want N squares, you need $R*C\ge N$. If you are given the dimensions of the rectangle, say w is the ratio of the width $W$ to the height $H$(presumed $\ge 1$). Then you probably want $C/R$ to be about this, so $R^2w \ge N$ Now you can round up to get $R$ and divide and round up to get $C$.

To get the coordinates of each square of side $s$, there is some space $t$ between the squares. So $Cs+(C-1)t\le W$ and $Rs+(R-1)t\le H$ and you lay them out starting at (0,0) if that is top left in increments of $s+t$

Added: for 34 tiles in 600 wide by 200 high, you can either have a 4x10 or 3x12 layout (with some blanks like you showed in the original question). If we take the 4x10 option, then you can have, for example, the tiles 45 high, which uses up 180 of the 200. This leaves 20 units to divide by 3, so the gaps would be 6. But going the other way, 10 tiles would use 450, leaving 50 to divide by 9, so the gaps could only be 5. Then the top left tile is at (0,0), the next one down is at (50,0), the next at (100,0) and the last at (150,0) with its bottom at 195. In width they go 0, 50, 100, 150,...450 with the right edge at 495. Is that what you were looking for?

  • 0
    @Rao: as you may see from the example, there are subtleties in the breakage. When I made vertical gaps you could use 6, but they didn't fit horizontally2011-02-12