teaching machines

Fitting a Rectangle Inside a Rectangle

January 18, 2016 by . Filed under algorithms, graphics, public.

A common problem in computer graphics and user interfaces is scaling one rectangle to fit inside another—preserving the rectangle’s aspect ratio. For example, I’ve encountered this problem in the following situations:

When I first encountered the problem, I just hacked away until I found some scale factors and logic that did the trick. But now that I’ve solved it N times, I need a little more certainty that I am doing the right thing. So, I sat down to prove to myself that my solution works.

fit_aspect