I've got a number of controls (charts in this case) that's determined at runtime. I'd like to put these in a grid with the proper number of rows and columns. For example,
Sorry, I don't really have any code to show my attempts. I started playing with determining if the square root is an integer, if the number is evenly divisible by 2 etc. and realized I'm not sure how to attack this problem. But this is what I'm thinking:
I'd appreciate it if someone could point me in the right direction - or suggest a different algorithm if I'm way off base.
Idea: If square root is not integer, floor it, then divide whole number by this, ceil it.
int columns = (int)sqrt(number);
int lines = (int)ceil(number / (float)columns);
Example: 21 => columns = 4, lines = 6.
UPDATE: bonus, it also works when sqrt(number) is integer. No rounding occurs anywhere, and values are correct.