Skip to content

Instantly share code, notes, and snippets.

@lmccart
Created July 8, 2013 21:51
Show Gist options
  • Save lmccart/5952806 to your computer and use it in GitHub Desktop.
Save lmccart/5952806 to your computer and use it in GitHub Desktop.
Quad Quad::getCenteredFit(const Quad &other, bool expand) const
{
Quad result = *this;
result.offset( other.getCenter() - result.getCenter() );
bool isInside = ( ( result.getWidth() < other.getWidth() ) && ( result.getHeight() < other.getHeight() ) );
if( expand || !isInside ) { // need to do some scaling
float aspectAspect = result.getAspectRatio() / other.getAspectRatio();
if( aspectAspect >= 1.0f ) { // result is proportionally wider so we need to fit its x-axis
float scaleBy = other.getWidth() / result.getWidth();
result.scaleCentered( scaleBy );
}
else { // result is proportionally wider so we need to fit its y-axis
float scaleBy = other.getHeight() / result.getHeight();
result.scaleCentered( scaleBy );
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment