Define the given 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
Hello, daveywavey!
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 with height and width ,
I need to find rectangle with height and width
such that sits inside , and is just big enough to enclose
. . but no bigger than it needs to be.
Code:*-------------------* - | | : - *-------------------* : : | |1.848 1 | R | : : | | : - *-------------------* - : - - - - 3 - - - - :
Code:: - - 3 - - : *-----------*---* | | | 2 | R | | | | | *-----------*---* : - - 3.247 - - :
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?
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.
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