Skip to content

Instantly share code, notes, and snippets.

@sheac
Last active December 25, 2015 17:59
Show Gist options
  • Save sheac/7016937 to your computer and use it in GitHub Desktop.
Save sheac/7016937 to your computer and use it in GitHub Desktop.
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