# Find Rectangle

• Jul 13th 2012, 06:16 AM
daveywavey
Find Rectangle
I need to enclose any given rectangle with another rectangle.

The enclosing rectangle must be of a specific ratio - eg width is twice its height for example. In actual fact the ratio I need for the enclosing rectagle is that the width is 1.62344 times the height.

So given any rectange r with heigh h and width w I need to find rectangle r2 with height h2 and width w2 such that r sits inside r2 and r2 is just big enough to enclose r but no bigger than it needs to be.

Many thanks
• Jul 13th 2012, 06:49 AM
bjhopper
Re: Find Rectangle
Define the given rectangle
• Jul 13th 2012, 07:02 AM
daveywavey
Re: Find Rectangle
Hi bjhopper

The rectangle to be enclosed is not defined. It can be any rectangle - well lets say any rectangle with a width >= 1 and < = 100000 and height >= 1 and <= 100000

Thanks

Dave
• Jul 16th 2012, 04:18 AM
Wilmer
Re: Find Rectangle
Code:

  A          1.62344c          B   E        b          F                               c   a   D                    G      C
Rectangle EFGD (height=a, width=b) is inside rectangle ABCD (height=c, width=1.62344c).
a and b are givens, with b>a.
Is this what you mean?
• Jul 16th 2012, 08:24 AM
Soroban
Re: Find Rectangle
Hello, daveywavey!

Quote:

I need to enclose any given rectangle with another rectangle.

The enclosing rectangle must be of a specific ratio - e.g. width is twice its height, for example.
In actual fact, the ratio I need for the enclosing rectagle is that the width is 1.62344 times the height.

So given any rectange $\displaystyle R$ with height $\displaystyle h$ and width $\displaystyle w$,
I need to find rectangle $\displaystyle R_2$ with height $\displaystyle h_2$ and width $\displaystyle w_2$
such that $\displaystyle R$ sits inside $\displaystyle R_2$, and $\displaystyle R_2$ is just big enough to enclose $\displaystyle R$
. . but no bigger than it needs to be.

$\displaystyle \text{Let }r\text{ = the specified ratio (1.62344).}$

$\displaystyle \text{The "inner" rectangle is }R\text{, with width }w\text{ and height }h.$
$\displaystyle \text{There are two cases to consider.}$

$\displaystyle \text{(1) If }\frac{w}{h} > r,\,\text{ then: }\,w_2 = w,\;h_2 = \frac{h}{r}$

$\displaystyle \text{Example: }\,R\text{ is }3\times 1.$

$\displaystyle \text{Then: }\,w_2 \,=\, 3,\; h_2 \,=\,\frac{3}{r} \,\approx\,1.848$

Code:

      *-------------------*  -       |                  |  :     - *-------------------*  :     : |                  |1.848     1 |        R        |  :     : |                  |  :     - *-------------------*  -       : - - - - 3 - - - - :

$\displaystyle \text{(2) If }\frac{w}{h} < r,\,\text{ then: }\,w_2 \,=\,rh,\;h_2 \,=\,h$

$\displaystyle \text{Example: }\:R\text{ is }3\times2.$

$\displaystyle \text{Then: }\,w_2 \,=\,(r)(2) \:\approx\: 3.247$

Code:

      : - - 3 - - :       *-----------*---*       |          |  |     2 |    R    |  |       |          |  |       *-----------*---*       : - - 3.247 - - :
• Jul 16th 2012, 09:56 AM
daveywavey
Re: Find Rectangle
Hi both and thanks for your help.

I needed this for some computer code I was writing and have already got it working with an algorithm which I guess is pretty simillar to Sorobans as follows:

private void MaintainAspectRatio()
{
double canvasWidth;
double canvasHeight;

canvasWidth = _croppedWidth + 4;
canvasHeight = canvasWidth * _aspectRatio;

if (canvasWidth < _croppedWidth || canvasHeight < _croppedHeight)
{
canvasHeight = _croppedHeight + 4;
canvasWidth = canvasHeight / _aspectRatio;
}
etc

Thanks for your solution though, I can make my test more elegant with this. I was also kind of wondering if this could be expressed in one formulae but I don't know if it can?
• Jul 16th 2012, 06:10 PM
Wilmer
Re: Find Rectangle
Quote:

Originally Posted by daveywavey
I was also kind of wondering if this could be expressed in one formulae but I don't know if it can?

Only in this style: IF [whatever] ELSE [whatever]

Like, impossible to have ONE straightforward formula giving you the cost of apples AND of bananas, right?
• Jul 16th 2012, 08:55 PM
daveywavey
Re: Find Rectangle
Ok thanks Wilmer. I only did O level maths and some A level maths at college so I didn't know what you can do these days - I just remember Russell Crowe in a beautiful mind writing lots of algorithms and was thinking of that sort of thing I think!

Many thanks for youre help :)

Also this is my first question on here - are there protocols for expressing thanks, giving points etc? I'm sure there must be as I see it says 40 Thanks next to your name.
• Jul 17th 2012, 04:12 AM
Wilmer
Re: Find Rectangle
If we let r = ratio (1.62344 in your example),
a = height inner rectangle, b = width inner rectangle,
u = height outer rectangle,v = width outer rectangle,

then you can use (assuming a*r <> b):

IF ar > b then u = a and v = a*r ELSE v = b and u = b/r
• Jul 17th 2012, 09:49 AM
daveywavey
Re: Find Rectangle
Thanks Wilmer thats very succint, thanks!