Given coordinates of 4 points, bottom-left and top-right corners of two rectangles. The task is to find the coordinates of the intersecting rectangle formed by the given two rectangles.
rec1: bottom-left(0, 0), top-right(10, 8),
rec2: bottom-left(2, 3), top-right(7, 9)
Output: (2, 3) (7, 8) (2, 8) (7, 3)
rec1: bottom-left(0, 0), top-right(3, 3),
rec2: bottom-left(1, 1), top-right(2, 2)
Output: (1, 1) (2, 2) (1, 2) (2, 1)
As two given points are diagonals of a rectangle. so, x1 < x2, y1 < y2. similarly x3 < x4, y3 < y4.
so, bottom-left and top-right points of intersection rectangle can be found by using formula.
x5 = max(x1, x3); y5 = max(y1, y3); x6 = min(x2, x4); y6 = min(y2, y4);
In case of no intersection, x5 and y5 will always exceed x6 and y5 respectively. The other two points of the rectangle can be found by using simple geometry.
Below is the implementation of the above approach:
(2, 3) (7, 8) (2, 8) (7, 3)
Time Complexity: O(1)
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Find Corners of Rectangle using mid points
- Find if there is a rectangle in binary matrix with corners as 1
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Find the centroid of a non-self-intersecting closed Polygon
- Count number of pairs of lines intersecting at a Point
- Length of the transverse common tangent between the two non intersecting circles
- Count ways to divide circle using N non-intersecting chords
- Length of direct common tangent between the two non-intersecting Circles
- Length of direct common tangent between two intersecting Circles
- Distance between centers of two intersecting circles if the radii and common chord length is given
- Find if two rectangles overlap
- Sum of Areas of Rectangles possible for an array
- Number of rectangles in N*M grid
- Find all rectangles filled with 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.