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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Distance between centers of two intersecting circles if the radii and common chord length is given
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Length of the transverse common tangent between the two non intersecting circles
- Length of direct common tangent between the two non-intersecting Circles
- Length of direct common tangent between two intersecting Circles
- Minimum adjacent swaps to move maximum and minimum to corners
- Count straight lines intersecting at a given point
- Count ways to divide circle using N non-intersecting chords
- Count number of pairs of lines intersecting at a Point
- Find the centroid of a non-self-intersecting closed Polygon
- Number of ways to choose K intersecting line segments on X-axis
- Find if two rectangles overlap
- Minimum area of square holding two identical rectangles
- Find side of Square which makes minimal area to fit two identical rectangles inside it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.