0
$\begingroup$

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
|________d______|
|________c______|
|________b______|
|        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.

$\endgroup$
0
2
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$
    – George
    Dec 13 '11 at 15:10
  • 1
    $\begingroup$ @George Ross meant to type $2-2^{1-n}$. $\endgroup$ Dec 13 '11 at 15:15
  • $\begingroup$ @DavidMitra: yes. Fixed $\endgroup$ Dec 13 '11 at 15:42
  • $\begingroup$ @RossMillikan I updated my problem with your formula, and the division of the rectangles exceeds that of the midpoint. $\endgroup$
    – George
    Dec 13 '11 at 15:57
  • $\begingroup$ @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$ $\endgroup$ Dec 13 '11 at 16:54
0
$\begingroup$

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$

$T=a(2-2^{1-n})$

$a=\frac{T}{2-2^{1-n}}$

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}} $

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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