public static Rectangle2D intersection(Rectangle2D a, Rectangle2D b) {
Objects.requireNonNull(a, "The argument 'a' must not be null.");
Objects.requireNonNull(b, "The argument 'b' must not be null.");
if (a.intersects(b)) {
double intersectionMinX = Math.max(a.getMinX(), b.getMinX());
double intersectionMaxX = Math.min(a.getMaxX(), b.getMaxX());
double intersectionWidth = intersectionMaxX - intersectionMinX;
double intersectionMinY = Math.max(a.getMinY(), b.getMinY());
double intersectionMaxY = Math.min(a.getMaxY(), b.getMaxY());
double intersectionHeight = intersectionMaxY - intersectionMinY;
return new Rectangle2D(intersectionMinX, intersectionMinY, intersectionWidth, intersectionHeight);
} else {
return Rectangle2D.EMPTY;
}
}