12,507,845 members (57,216 online)
alternative version

15.9K views
2 bookmarked
Posted

# Rectangle Tiling Algorithm

, 23 Jan 2011 CPOL
 Rate this:
A useful algorithm to divide a rectangular area into rectangular subregions. Good for tiling windows in a given area
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.

```// num_rects : Specifies the number of small rectangle you want to divide a big rectangle into
// num_rows  : Output parameter to receive number of rows needed to divide the big rectangle
// cols_per_row : Output parameter to receive the number of varying columns per rows (just pass a int* data type, the function will create an array of integers where the index of the array is the row, and the value in that index is the columns per that row)

void DivideRectangularArea (unsigned int num_rects, /*OUT*/ int &num_rows, /*OUT*/ 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++)
{
{
col_count++;
}
cols_per_row[i] = col_count;
}
num_rows = row_count;
}
}```

## Share

 Chief Technology Officer Rational Technologies Bangladesh
If you are not in - you are out !
- Chapter 1

## You may also be interested in...

 Pro

 View All Threads First Prev Next
 thnx.. Mukit, Ataul23-Jan-11 18:38 Mukit, Ataul 23-Jan-11 18:38
 thnx..
 Last Visit: 31-Dec-99 18:00     Last Update: 28-Sep-16 14:34 Refresh 1