Problem Statement
You are given a collection of rectangles whose sides are parallel to coordinate axes. Compute and return the total area covered by those rectangles. Note that the area covered by multiple rectangles just counts once.
The rectangles are given in four int[]s, X1, X2, Y1 and Y2. The coordinates of the lower-left corner of the i-th rectangle are (X1[i], Y1[i]), and the coordinates of the upper-right corner are (X2[i], Y2[i]).
Definition
- Class:
- SeveralRectangles
- Method:
- computeArea
- Parameters:
- int[], int[], int[], int[]
- Returns:
- int
- Method signature:
- int computeArea(int[] X1, int[] X2, int[] Y1, int[] Y2)
- (be sure your method is public)
Constraints
- X1, X2, Y1 and Y2 will contain the same number of elements.
- X1 will contain between 1 and 10 elements, inclusive.
- Each element of X1, X2, Y1 and Y2 will be between 0 and 50, inclusive.
- Each element of X2 will be strictly greater than the corresponding element of X1.
- Each element of Y2 will be strictly greater than the corresponding element of Y1.
- No two rectangles will be the same.
Examples
{33,35}{36,43}{15,36}{27,42}Returns: 84
Here we have two non-intersecting rectangles. The corners of the first rectangle are in (33, 15) and (36, 27). The corners of the second one are in (35, 36) and (43, 42). The areas covered by rectangles are (36 - 33) * (27 - 15) = 36 and (43 - 35) * (42 - 36) = 48. So the answer is 36 + 48 = 84.{0,5}{27,18}{10,13}{36,49}Returns: 871
Two intersecting rectangles with areas 702 and 468. Their common part's covered area is 299, so the answer is 702 + 468 - 299 = 871.{12,9,22}{21,13,26}{27,26,11}{40,40,36}Returns: 260
{9,40,34,26,6,12}{23,45,40,42,45,37}{13,16,34,7,18,22}{18,26,40,36,29,48}Returns: 1203