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.

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.

2 Answers
2

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

@RossMillikan I updated my problem with your formula, and the division of the rectangles exceeds that of the midpoint.
–
GeorgeDec 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 MillikanDec 13 '11 at 16:54