Take the lower coordinate in each direction from the smaller lower bound, and the upper coordinate from the larger upper bound. This gives an axis-aligned bounding box, rather than the generally nonrectangular set union of the two regions. Use defensive copying because the paper's
Point has mutable fields.final class Rect {
private final Point lowerLeft, upperRight;
public Rect(Point ll, Point ur) {
if (ll == null || ur == null)
throw new IllegalArgumentException("null corner");
double xl = ll.getX(), yl = ll.getY();
double xh = ur.getX(), yh = ur.getY();
if (!Double.isFinite(xl) || !Double.isFinite(yl)
|| !Double.isFinite(xh) || !Double.isFinite(yh)
|| xl > xh || yl > yh)
throw new IllegalArgumentException("invalid rectangle");
lowerLeft = new Point(xl, yl);
upperRight = new Point(xh, yh);
}
public Rect Union(Rect r) {
return new Rect(
new Point(Math.min(lowerLeft.getX(), r.lowerLeft.getX()),
Math.min(lowerLeft.getY(), r.lowerLeft.getY())),
new Point(Math.max(upperRight.getX(), r.upperRight.getX()),
Math.max(upperRight.getY(), r.upperRight.getY())));
}
public Point lowerLeft() { return lowerLeft.clone(); }
public Point upperRight() { return upperRight.clone(); }
}Any containing rectangle must reach at least as far as each input rectangle in all four coordinate directions. The constructed rectangle reaches exactly those four limits, which proves both containment and minimality:The corner accessors return copies, so changing a constructor argument or returned
Point cannot alter an existing Rect. Articles by others on the same topic
There are currently no matching articles.