Created
July 19, 2017 19:39
-
-
Save sanmadjack/104354cac1de23b008f0751b0426216c to your computer and use it in GitHub Desktop.
Dart function to fit a rectangle represented by a point
This file contains hidden or 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
/// 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