Problem Statement
You are given two rectangles in the plane. The sides of each rectangle are parallel to the coordinate axes. Each rectangle is given by the coordinates of its lower left corner and its upper right corner. You are given two int[]s: A and B. A describes the first rectangle: its lower left corner has coordinates (A[0],A[1]) and its upper right corner has coordinates (A[2],A[3]). B describes the second rectangle in the same way.
Your task is to determine how their intersection looks like. There are four options:
- If the rectangles have a non-zero area in common, return "rectangle".
- Otherwise, if they have a common line segment with non-zero length, return "segment".
- Otherwise, if they have a point in common, return "point".
- Otherwise, return "none" (in all four cases, the quotes are just for clarity).
Definition
- Class:
- TwoRectangles
- Method:
- describeIntersection
- Parameters:
- int[], int[]
- Returns:
- String
- Method signature:
- String describeIntersection(int[] A, int[] B)
- (be sure your method is public)
Notes
- The Constraints guarantee that both rectangles lie in the first quadrant of the coordinate plane, and each rectangle has a positive area.
Constraints
- A will contain exactly 4 elements.
- The elements of A will satisfy 0 <= A[0] < A[2] <= 1000 and 0 <= A[1] < A[3] <= 1000.
- B will contain exactly 4 elements.
- The elements of B will satisfy 0 <= B[0] < B[2] <= 1000 and 0 <= B[1] < B[3] <= 1000.
Examples
{0,0,3,2}{1,1,5,3}Returns: "rectangle"
These two rectangles overlap partially:{0,0,5,3}{1,2,2,3}Returns: "rectangle"
The second rectangle is completely inside the first one.{1,1,6,2}{3,2,5,4}Returns: "segment"
The second rectangle sits on top of the first one.{0,1,2,3}{2,0,5,2}Returns: "segment"
{0,0,1,1}{1,1,5,2}Returns: "point"
{1,1,2,2}{3,1,4,2}Returns: "none"