/** * Unions the supplied two rectangles, writing the result into {@code dst}. */ public static void union (IRectangle src1, IRectangle src2, Rectangle dst) { float x1 = Math.min(src1.minX(), src2.minX()); float y1 = Math.min(src1.minY(), src2.minY()); float x2 = Math.max(src1.maxX(), src2.maxX()); float y2 = Math.max(src1.maxY(), src2.maxY()); dst.setBounds(x1, y1, x2 - x1, y2 - y1); }
/** * Unions the supplied two rectangles, writing the result into {@code dst}. */ public static void union (IRectangle src1, IRectangle src2, Rectangle dst) { float x1 = Math.min(src1.minX(), src2.minX()); float y1 = Math.min(src1.minY(), src2.minY()); float x2 = Math.max(src1.maxX(), src2.maxX()); float y2 = Math.max(src1.maxY(), src2.maxY()); dst.setBounds(x1, y1, x2 - x1, y2 - y1); }
/** * Intersects the supplied two rectangles, writing the result into {@code dst}. */ public static void intersect (IRectangle src1, IRectangle src2, Rectangle dst) { float x1 = Math.max(src1.minX(), src2.minX()); float y1 = Math.max(src1.minY(), src2.minY()); float x2 = Math.min(src1.maxX(), src2.maxX()); float y2 = Math.min(src1.maxY(), src2.maxY()); dst.setBounds(x1, y1, x2 - x1, y2 - y1); }
/** * Intersects the supplied two rectangles, writing the result into {@code dst}. */ public static void intersect (IRectangle src1, IRectangle src2, Rectangle dst) { float x1 = Math.max(src1.minX(), src2.minX()); float y1 = Math.max(src1.minY(), src2.minY()); float x2 = Math.min(src1.maxX(), src2.maxX()); float y2 = Math.min(src1.maxY(), src2.maxY()); dst.setBounds(x1, y1, x2 - x1, y2 - y1); }
/** Moves ths given inner rectangle such that it lies within the given outer rectangle. * The results are undefined if either the inner width or height is greater that the outer's * width or height, respectively. */ protected static Rectangle confine (IRectangle outer, Rectangle inner) { float dx = outer.x() - inner.x(), dy = outer.y() - inner.y(); if (dx <= 0) dx = Math.min(0, outer.maxX() - inner.maxX()); if (dy <= 0) dy = Math.min(0, outer.maxY() - inner.maxY()); inner.translate(dx, dy); return inner; }
/** * Computes the point inside the bounds of the rectangle that's closest to the given point, * writing the result into {@code out}. * @return {@code out} for call chaining convenience. */ public static Point closestInteriorPoint (IRectangle r, IPoint p, Point out) { out.set(MathUtil.clamp(p.x(), r.minX(), r.maxX()), MathUtil.clamp(p.y(), r.minY(), r.maxY())); return out; }
/** * Computes the point inside the bounds of the rectangle that's closest to the given point, * writing the result into {@code out}. * @return {@code out} for call chaining convenience. */ public static Point closestInteriorPoint (IRectangle r, IPoint p, Point out) { out.set(MathUtil.clamp(p.x(), r.minX(), r.maxX()), MathUtil.clamp(p.y(), r.minY(), r.maxY())); return out; }
/** Tries to place the inner bounds within the outer bounds, such that the inner bounds does * not contain the position. */ protected static boolean avoidPoint (IRectangle outer, Rectangle inner, IRectangle fingerprint) { Rectangle checkBounds = new Rectangle(); Rectangle best = new Rectangle(inner); float bestDist = Float.MAX_VALUE, edge; // confine to the left edge = fingerprint.x(); checkBounds.setBounds(outer.x(), outer.y(), edge - outer.x(), outer.height()); bestDist = compareAndConfine(checkBounds, inner, best, bestDist); // right edge = fingerprint.maxX(); checkBounds.setBounds(edge, outer.y(), outer.width() - edge, outer.height()); bestDist = compareAndConfine(checkBounds, inner, best, bestDist); // top edge = fingerprint.y(); checkBounds.setBounds(outer.x(), outer.y(), outer.width(), edge - outer.y()); bestDist = compareAndConfine(checkBounds, inner, best, bestDist); // bottom edge = fingerprint.maxY(); checkBounds.setBounds(outer.x(), edge, outer.width(), outer.height() - edge); bestDist = compareAndConfine(checkBounds, inner, best, bestDist); inner.setBounds(best); return bestDist < Float.MAX_VALUE; }