This is an algorithm which takes as input, the number of small rectangles to divide a big rectangular area and outputs required number of rows and columns for each row.
void DivideRectangularArea (unsigned int num_rects, int &num_rows, int * &cols_per_row)
{
if (num_rects == 0)
return false;
if (num_rects < 4)
{
int sections = 1;
cols_per_row = new int[sections];
for (int i = 0; i < sections; i++)
{
cols_per_row[i] = num_rects;
}
num_rows = sections;
}
else
{
double root = num_rects;
double sqr_root = sqrt(root);
int rounded_root = (int) sqr_root;
int row_count = rounded_root;
int col_count = rounded_root;
cols_per_row = new int[row_count];
int leftover = ((int) num_rects) - row_count * col_count;
int distrib = leftover / row_count;
col_count += distrib;
int left = num_rects - (row_count * col_count);
int start_adding_index = row_count - left;
for (int i = 0; i < row_count; i++)
{
if (i == start_adding_index)
{
col_count++;
start_adding_index = -1;
}
cols_per_row[i] = col_count;
}
num_rows = row_count;
}
}