Skip to content

Instantly share code, notes, and snippets.

@wilkerlucio
Created March 5, 2014 19:54
Show Gist options
  • Select an option

  • Save wilkerlucio/9375224 to your computer and use it in GitHub Desktop.

Select an option

Save wilkerlucio/9375224 to your computer and use it in GitHub Desktop.
class @Rectangle
constructor: (@left, @top, @width, @height) ->
expand: (amount) ->
yamount = amount / @width * @height
new Rectangle(
@left - amount
@top - yamount
@width + amount * 2
@height + yamount * 2
)
fitIn: (rectangle) ->
newRectangle = new Rectangle(@left, @top, @width, @height)
factor = rectangle.width / rectangle.height
boundsFactor = @width / @height
if factor > boundsFactor
newWidth = @height * factor
newRectangle.left -= (newWidth - @width) / 2
newRectangle.width = newWidth
else
factor = rectangle.height / rectangle.width
newHeight = @width * factor
newRectangle.top -= (newHeight - @height) / 2
newRectangle.height = @width * factor
newRectangle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment