I've read variations of this algorithm many times, and still get momentarily confused whenever I see it. Can you add some comments (e.g., "if wider than the size to fit within, scale down height")?