I have some object. It has some original width and height. I am given with new width and height. How to adopt new given width and height to object original ones? ( Say we had a square 1000x1000 and were given with some desired maximum possible coordinates say 1900x1200 what algorithm would produce 1200x1200 out of it? or in other case we had 1000x1000 original and were given with 400x600 it shall return 400x400…)
So I started triing to create some code that would od it but failed with:
void resize_coefs(const int & original_w, const int & original_h, int & new_w, int & new_h)
{
double VW = new_w;
double VH = new_h;
double rw = original_w/VW;
double rh = original_h/VH;
if (rw>=rh){
new_h = VH;
new_w = rh*new_w + 2;
}
else
{
new_w = VW;
new_h= rw*new_h;
}
}
Do it this way:
Sample code:
Due to possible rounding errors, it is advisable to check that
new_wandnew_hare still within bounds after scaling.EDIT: Here’s a version that does the same thing with integer arithmetic only, using the hint that Kerrek provided in his answer. It’s a little hard to see that it’s following exactly the same logic.