VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-vertex-coordinates-of-all-possible-rectangles-with-a-given-vertex-and-dimensions/

⇱ Find vertex coordinates of all possible rectangles with a given vertex and dimensions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find vertex coordinates of all possible rectangles with a given vertex and dimensions

Last Updated : 23 Jul, 2025

Given two integers L and B representing the length and breadth of a rectangle and a coordinate (X, Y) representing a point on the cartesian plane, the task is to find coordinates of all rectangles having a vertex as (X, Y) of the given dimensions.

Example:

Input: X=9, Y=9, L=5, B=3
Output:
(9, 9), (14, 9), (9, 12), (14, 12)
(4, 9), (9, 9), (4, 12), (9, 12)
(9, 6), (14, 6), (9, 9), (14, 9)
(4, 6), (9, 6), (4, 9), (9, 9)
(9, 9), (12, 9), (9, 14), (12, 14)
(6, 9), (9, 9), (6, 14), (9, 14)
(9, 4), (12, 4), (9, 9), (12, 9)
(6, 4), (9, 4), (6, 9), (9, 9)
Explanation: There are 8 possible rectangles such that one of their vertex is (9, 9) and the length and breadth is 5 and 3 respectively as mentioned above.

Input: X=2, Y=3, L=4, B=1
Output:
(2, 3), (6, 3), (2, 4), (6, 4)
(-2, 3), (2, 3), (-2, 4), (2, 4)
(2, 2), (6, 2), (2, 3), (6, 3)
(-2, 2), (2, 2), (-2, 3), (2, 3)
(2, 3), (3, 3), (2, 7), (3, 7)
(1, 3), (2, 3), (1, 7), (2, 7)
(2, -1), (3, -1), (2, 3), (3, 3)
(1, -1), (2, -1), (1, 3), (2, 3)

Approach: It can be observed that for a given length and breadth and a vertex (X, Y), eight rectangles are possible as shown in the images below:

👁 Image
👁 Image

If the given length and breadth of the rectangles are equal, both the horizontal and vertical rectangles will represent the same coordinates. Hence, only 4 unique squares are possible either shown in image 1 or in image 2.

Below is the implementation of the above approach:


Output: 
(9, 9), (14, 9), (9, 12), (14, 12)
(4, 9), (9, 9), (4, 12), (9, 12)
(9, 6), (14, 6), (9, 9), (14, 9)
(4, 6), (9, 6), (4, 9), (9, 9)
(9, 9), (12, 9), (9, 14), (12, 14)
(6, 9), (9, 9), (6, 14), (9, 14)
(9, 4), (12, 4), (9, 9), (12, 9)
(6, 4), (9, 4), (6, 9), (9, 9)

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: