Added centerRectInRect(), minor optimization for fitRectInRect().

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@450 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Ray Greenwell
2008-03-17 22:57:43 +00:00
parent 2d3ab6fc94
commit 3afd587398
+14 -5
View File
@@ -70,6 +70,17 @@ public class DisplayUtil
}
}
/**
* Center the specified rectangle within the specified bounds. If the bounds are too
* small then the rectangle will be pinned to the upper-left.
*/
public static function centerRectInRect (rect :Rectangle, bounds :Rectangle) :Point
{
return new Point(
bounds.x + Math.max(0, (bounds.width - rect.width) / 2),
bounds.y + Math.max(0, (bounds.height - rect.height) / 2));
}
/**
* Returns the most reasonable position for the specified rectangle to
* be placed at so as to maximize its containment by the specified
@@ -79,15 +90,13 @@ public class DisplayUtil
* @param rect the rectangle to be positioned.
* @param bounds the containing rectangle.
*/
public static function fitRectInRect (
rect :Rectangle , bounds :Rectangle) :Point
public static function fitRectInRect (rect :Rectangle, bounds :Rectangle) :Point
{
// Guarantee that the right and bottom edges will be contained
// and do our best for the top and left edges.
var br :Point = bounds.bottomRight;
return new Point(
Math.min(br.x - rect.width, Math.max(rect.x, bounds.x)),
Math.min(br.y - rect.height, Math.max(rect.y, bounds.y)));
Math.min(bounds.right - rect.width, Math.max(rect.x, bounds.x)),
Math.min(bounds.bottom - rect.height, Math.max(rect.y, bounds.y)));
}
/**