Java Utililty Methods Rectangle Fit

List of utility methods to do Rectangle Fit

Description

The list of methods to do Rectangle Fit are organized into topic(s).

Method

voidfitRectangle(Rectangle rect, Dimension d)
fit Rectangle
fitRectangle(rect, d.width, d.height);
voidfitRectangle(Rectangle rect, Rectangle target)
fit Rectangle
if (rect.width > target.width) {
    rect.width = target.width;
if (rect.height > target.height) {
    rect.height = target.height;
if (rect.x < target.x) {
    rect.x = target.x;
...
RectanglefitRectangles(Dimension imageSize, Dimension size)
fit Rectangles
double aspectX = (double) size.width / imageSize.width;
double aspectY = (double) size.height / imageSize.height;
double aspect = Math.min(aspectX, aspectY);
int paintedWidth = (int) (imageSize.width * aspect);
int paintedHeight = (int) (imageSize.height * aspect);
int x = (size.width - paintedWidth) / 2;
int y = (size.height - paintedHeight) / 2;
return new Rectangle(x, y, paintedWidth, paintedHeight);
...
PointfitRectInRect(Rectangle rect, Rectangle bounds)
Returns the most reasonable position for the specified rectangle to be placed at so as to maximize its containment by the specified bounding rectangle while still placing it as near its original coordinates as possible.
return new Point(Math.min(bounds.x + bounds.width - rect.width, Math.max(rect.x, bounds.x)),
        Math.min(bounds.y + bounds.height - rect.height, Math.max(rect.y, bounds.y)));
booleanfits(Rectangle r)
fits
boolean ret = false;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (((r.x + r.width) <= screenSize.width) && ((r.y + r.height) <= screenSize.height)) {
    ret = true;
return ret;
booleanfits(Rectangle2D o1, Rectangle2D o2)
Takes two rectangles and check if the first fits into the second
return (o1.getHeight() <= o2.getHeight() && o1.getWidth() <= o2.getWidth());
booleanfitsInside(Dimension2D dim, Rectangle2D rect)
fits Inside
return rect.getWidth() >= dim.getWidth() && rect.getHeight() >= dim.getHeight();
booleanfitsRotated(Rectangle2D o1, Rectangle2D o2)
fits Rotated
return (o1.getHeight() <= o2.getWidth() && o1.getWidth() <= o2.getHeight());
RectanglereScale(Rectangle rect, int oldScale, int newScale)
re Scale
Rectangle res = new Rectangle();
res.x = reScale(rect.x, oldScale, newScale);
res.y = reScale(rect.y, oldScale, newScale);
res.width = reScale(rect.width, oldScale, newScale);
res.height = reScale(rect.height, oldScale, newScale);
return res;
RectangleshrinkToFit(Rectangle container, Rectangle item)
shrink To Fit
double scale = scaleProportional(container, item);
if (scale > 1) {
    return item;
} else {
    return new Rectangle((int) (item.width * scale), (int) (item.height * scale));