Java Utililty Methods Rectangle

List of utility methods to do Rectangle

Description

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

Method

Rectangleinterpolate(Rectangle r1, Rectangle r2, float f)
interpolate
Rectangle out = new Rectangle();
interpolate(r1, r2, f, out);
return out;
SetmergeRects(Set prev, Set current)
merge Rects
Set<Rectangle> unmerged = new HashSet<Rectangle>(prev);
if (!prev.isEmpty() && !current.isEmpty()) {
    Rectangle[] pr = prev.toArray(new Rectangle[prev.size()]);
    Rectangle[] cr = current.toArray(new Rectangle[current.size()]);
    int ipr = 0;
    int icr = 0;
    while (ipr < pr.length && icr < cr.length) {
        while (cr[icr].x < pr[ipr].x) {
...
voidnormalizeRect(Rectangle rect)
normalize Rect
if (rect.width < 0) {
    rect.width = -rect.width;
    rect.x -= rect.width;
if (rect.height < 0) {
    rect.height = -rect.height;
    rect.y -= rect.height;
RectanglerandomRectangle(int minW, int maxW, int minH, int maxH)
Returns a rectangle with the given min/max width and height.
int w = random(minW, maxW);
int h = random(minH, maxH);
return new Rectangle(w, h);
RectanglerandomSquare(int minW, int maxW)
Returns a square with the given min/max side length.
int w = random(minW, maxW);
return new Rectangle(w, w);
RectanglerectangleResize(Rectangle rect, int width, int height)
rectangle Resize
return new Rectangle(rect.x + (rect.width - width) / 2, rect.y + (rect.height - height) / 2, width, height);
booleanrectCollide(final Rectangle a, final Rectangle b)
rect Collide
if (a.x > b.x + b.width || a.y > b.y + b.height || a.x + a.width < b.x || a.y + a.height < b.y) {
    return false;
return true;
intrectDistance(final Rectangle r, final Rectangle q)
rect Distance
if (rectOverlaps(r, q)) {
    return 0;
int r_x0 = r.x;
int r_x1 = r.x + r.width;
int r_y0 = r.y;
int r_y1 = r.y + r.height;
int q_x0 = q.x;
...
voidrectFromArray(Rectangle2D aRectangle, double[] pts)
rect From Array
double minX = pts[0];
double minY = pts[1];
double maxX = pts[0];
double maxY = pts[1];
double x;
double y;
for (int i = 1; i < 4; i++) {
    x = pts[2 * i];
...
RectanglerectFromRect2D(Rectangle2D rect)
Make a Rectangle from a Rectangle2D (Rectangle is subclass of Rectangle2D)
return ((rect == null) ? null
        : new Rectangle((int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(),
                (int) rect.getHeight()));