Click here to Skip to main content
11,928,352 members (52,018 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

Rectangle Tiling Algorithm

, 23 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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;
        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)
                start_adding_index = -1;
            cols_per_row[i] = col_count;
        num_rows = row_count;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.

You may also be interested in...

Comments and Discussions

GeneralEdited title (too long), moved to description Pin
Indivara23-Jan-11 14:53
subeditorIndivara23-Jan-11 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 23 Jan 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid