Monday, February 6, 2023

Easily scale rect to fit dimensions

 My programs used to very awkwardly try to scale an image... I would try to fit it on the x-axis, then the y-axis, or else the y-axis then the x-axis, depending on the original dimensions, and also it would probably need rescaling after that scaling phase...

No more! Now I get how to scale it easily in one phase. (It seems so obvious now!)

Basically, we make sure it fits on x-axis. Then, with the updated scaling dimension, we check whether it needs further scaling on the y-axis.
The check on the x-axis is with the original width, while the check on the y-axis is with the scaled height (from scaling the x-axis to match the desired width).
When the scale is set, it is always calculated with the original width/height.

JsFiddle:

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...