Last active
December 20, 2015 20:49
-
-
Save stuaxo/6193014 to your computer and use it in GitHub Desktop.
Find the largest rectangle of {aspect ratio} that can fit into the given dimensions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_aspect(width, height): | |
| """ :return aspect ratio """ | |
| return float(width) / float(height) | |
| def get_ideal_size(width, height, ideal_aspect): | |
| """ | |
| :return the size of the largest rectangle with an aspect | |
| ratio of 'ideal_aspect' that will fit into width x height. | |
| """ | |
| aspect = get_aspect(width, height) | |
| if ideal_aspect < aspect: | |
| resized = height * ideal_aspect, height | |
| else: | |
| resized = width, width / ideal_aspect | |
| return resized |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment