Last active
December 25, 2015 17:59
-
-
Save sheac/7016937 to your computer and use it in GitHub Desktop.
This file contains 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 scale_down(iw, ih, dw, dh) | |
# 1. determining which dimension is the one that needs to be | |
# scaled the most | |
limiting_dim = (iw / ih) > (dw / dh) ? "width" : "height" | |
# 2. then figure out how much that scaling actually is | |
if limiting dim == "width" | |
scale_by = dw / iw | |
else | |
scale_by = dh / ih | |
end | |
# 3. apply the scaling factor to both dimensions | |
return [iw*scale_by, ih*scale_by] | |
end | |
def find_scale_pair(iw, ih, dw, dh) | |
# 1. determining which dimension is the one that needs to be | |
# scaled the most | |
limiting_dim = (iw / ih) > (dw / dh) ? "width" : "height" | |
# 2. return the non-limiting dimension's desired value | |
# and -1 for the limiting dimension | |
if limiting_dim == "width" | |
return [-1, dh] | |
else | |
return [dw, -1] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment