Skip to content

Instantly share code, notes, and snippets.

@sanmadjack
Created July 19, 2017 19:39
Show Gist options
  • Save sanmadjack/104354cac1de23b008f0751b0426216c to your computer and use it in GitHub Desktop.
Save sanmadjack/104354cac1de23b008f0751b0426216c to your computer and use it in GitHub Desktop.
Dart function to fit a rectangle represented by a point
/// Calculates the size of a rectangle that fits within the confines of another
/// rectangle the size of the dimensions described by [outer] with the same
/// aspect ratio as the rectangle described by [inner].
Point fitWithin(Point inner, Point outer) {
final double outerRatio = outer.x/outer.y;
final double innerRatio = inner.x/ inner.y;
if(outerRatio<innerRatio) {
final num x = outer.x;
final num y = inner.y * (outer.x/inner.x);
return new Point(x,y);
} else if(outerRatio>innerRatio) {
final num y = outer.y;
final num x = inner.x * (outer.y/inner.y);
return new Point(x,y);
} else {
return new Point(outer.x, outer.y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment