LeetCode Q 836 - Rectangle Overlap
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner.
Given two (axis-aligned) rectangles, return whether they overlap.
Solution
Code:
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
if (Math.max(rec1[0], rec2[0]) < Math.min(rec1[2], rec2[2])
&& Math.max(rec1[1], rec2[1]) < Math.min(rec1[3], rec2[3]))
return true;
return false;
}
LeetCode Q 223 - Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Solution
Code:
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int res = (C - A) * (D - B) + (G - E) * (H - F);
int length = 0, width = 0;
int left = Math.max(A, E);
int right = Math.min(C, G);
int top = Math.min(D, H);
int bottom = Math.max(B, F);
int overlap = 0;
if (left < right && bottom < top)
overlap = (right - left) * (top - bottom);
return res - overlap;
}