Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

enter image description here Each rectangle has a fixed proportion e.g. 1:2 Each rectangle has to be the same size Grid's height has to be filled Last row can have space if the number of elements is odd. Width might have space if proportions don't allow to fill it. What size each rectangle should be (the size will be the same among all rectangles)?

EDIT: What I ended up doing is this:

introducing some size e.g. 400x500 for rectangles

now if e.g. area to fit in is 3000x4000 I can calculate it's proportions (height/width)

    so area proportions = height / width
    add rect to upper left corner
    while(there are still rectangles to position)
    calculate current occupied area of all rects
    current occupied area proportions = occupied area height / width
    if area proportions < current occupied area proportions
    fill column with rectangles
    fill row with rectangles

     areaHeightToOccupiedHeightProportion = Area.Height / currentOccupiedSpace.Height;
            areaWidthToOccupiedWidthProportion = Area.Width / currentOccupiedSpace.Width;
    if(areaHeightToOccupiedHeightProportion < areaWidthToOccupiedWidthProportion){ 
            //scaling to maximum width would make the occupied area bigger than the   area we want to position rectangles in 
            var scale = areaHeightToOccupiedHeightProportion;
else scale = areaWidthToOccupiedWidthProportion;
foreach rectangle added
rectangle.x * scale; y * scale; width * scale; height * scale;

I'm pretty sure it's like the worst way to do it. But I had to do something. I would love to see how it would be possible to solve using math.

share|improve this question
You must specify more constraints to get a unique way of filling. For example, the first case could have been filled by a vertical stack of 15 rectangles. Are you trying to maximize the total area of the rectangles? –  ronno Sep 15 '13 at 18:52
@ronno Yes. I try to make rectangles as big as possible while maintaining their scale. What I ended up doing is something like this: –  CoolCodeBro Sep 21 '13 at 14:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.