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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to specify a number and have that number halve itself equally so that the bottom of my canvas fills with rectangles. For instance, if I specified the number 4, the bottom of my canvas would have 4 rectangles, each rectangle smaller than the last, until the bottom half of the canvas was filled. The total size of the rectangles would be approximate to half the size of my canvas.

|               |
|               |
|               |
|               |
|_______________| <- middle = 240
|        a      |
|               |

Where a, b, c, and d, represet sections each rectangle. A would be the largest rectangle, B would be around half the size of A, and C would be around half the size of B, so that the entirety fills up half of my canvas, etc.

What formula could I use to achieve this?

I'm developing an application and would obviously do each calculation inside of a loop, hence, I would only need to know how to calculate each individual rectangle while plugging in the iteration of my loop. e.g.

totalRects = 4;
middle = 240;
for(int i = 0; i<totalRects; i++)
   // Calculate size of rectangle of current iteration and draw.

Update Using the formula provided in the answer below, the total sum of the pieces don't add up to the mid point:

enter image description here

The black line represents the midpoint of 240.

share|cite|improve this question
up vote 2 down vote accepted

For $n$ rectangles, if you measure in terms of the length of the first rectangle, you have a total length of $$1+\frac{1}{2}+\frac{1}{4}+\ldots +\frac{1}{2^{n-1}}=\sum_{1=0}^{n-1}2^{-i}=2-2^{1-n}$$ So divide the total length by $2-2^{1-n}$ and that is the size of the first rectangle. This is an application of a geometric series

share|cite|improve this answer
Thanks for the quick response. I've tried your application above as, size = 240 / (2 - Math.Pow(2, 4 - 1)) Where my my center and n is the number of rectangles I want to produce. I'm getting -40. What am I doing wrong? – George Dec 13 '11 at 15:10
@George Ross meant to type $2-2^{1-n}$. – David Mitra Dec 13 '11 at 15:15
@DavidMitra: yes. Fixed – Ross Millikan Dec 13 '11 at 15:42
@RossMillikan I updated my problem with your formula, and the division of the rectangles exceeds that of the midpoint. – George Dec 13 '11 at 15:57
@George: I don't understand what you mean by midpoint. If you want $4$ rectangles to fit in 240 pixels, the formula says they add to $1\frac{7}{8}$. Multiplying $240 \cdot \frac{8}{15}=128$ so the rectangles would be $128, 64, 32, 16$ which add to $240$. I think you have an off-by-one error, as I got 265 as well using $1\frac34$ – Ross Millikan Dec 13 '11 at 16:54

Let $a$ denote the area of the first and largest rectangle. Assuming we want to draw $n>0$ rectangles, We can say that the total area of the page $T$ is:

$T= a + (\frac{1}{2}) a +(\frac{1}{4}) a +...+ (\frac{1}{2})^{n-1} a$



Accordingly, the area of the (k-th) rectangle can be found using the value of $a$ above and this formula:

$a_k =\frac{a}{2^{k-1}} $

share|cite|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.