Problem of the day
You are given two arbitrary rectangles on a 2-D coordinate plane, which may have an intersecting area. You have to find the net area covered by both the rectangles on the cartesian plane.
The orange area depicted in the above figure is the net area covered by both rectangles on the cartesian plane.
Note:
1. For a rectangle, its top left and bottom right coordinates are given.
2. Coordinates of the rectangles are integer values.
3. Edges of the given rectangles will always be parallel to the X and Y coordinate axes of the cartesian plane.
4. It is guaranteed that both the rectangles will have at least a unit area.
The first line of the input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains 4 space-separated integer values 'x1', 'y1', 'x2', 'y2' denoting the top left ('x1', 'y1') and bottom-right ('x2', 'y2') coordinates of the first rectangle.
The second line of each test case contains 4 space-separated integer values 'x3', 'y3', 'x4', 'y4' denoting the top left ('x3', 'y3') and bottom-right ('x4', 'y4') coordinates of the second rectangle.
Output Format:
For each test case, return an integer denoting the net area of two rectangles.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10^5
-10^9 <= x1, y1, x2, y2 <= 10^9
x1 < x2, x3 < x4
y1 > y2, y3 > y4
Time Limit: 1sec
1
-1 0 3 -1
2 0 4 -3
9
In the above figure, the area of the green rectangle is 4 units, the area of the violet rectangle is 6 units and the intersecting area is 1 unit marked by the red rectangle. So, the total area of the overlapping rectangles is 9 units.
1
1 2 2 1
-1 2 2 1
3
O(1)
Since all operations take constant time. Thus the time complexity will be O(1).
O(1)
Since we are using constant extra memory. Thus the space complexity will be O(1).