Find vertex coordinates of all possible rectangles with a given vertex and dimensions
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
Ouput:
(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
Ouput:
(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:
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:
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; void printHorizontal( int X, int Y, int L, int B) { cout << '(' << X << ", " << Y << "), " ; cout << '(' << X + L << ", " << Y << "), " ; cout << '(' << X << ", " << Y + B << "), " ; cout << '(' << X + L << ", " << Y + B << ")" << endl; } void printVertical( int X, int Y, int L, int B) { cout << '(' << X << ", " << Y << "), " ; cout << '(' << X + B << ", " << Y << "), " ; cout << '(' << X << ", " << Y + L << "), " ; cout << '(' << X + B << ", " << Y + L << ")" << endl; } // Function to find all possible rectangles void findAllRectangles( int L, int B, int X, int Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return ; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code int main() { int L = 5, B = 3; int X = 9, Y = 9; findAllRectangles(L, B, X, Y); } |
Java
// Java code for the above approach class GFG{ static void printHorizontal( int X, int Y, int L, int B) { System.out.print( "(" + X+ ", " + Y+ "), " ); System.out.print( "(" + (X + L)+ ", " + Y+ "), " ); System.out.print( "(" + X+ ", " + (Y + B)+ "), " ); System.out.print( "(" + (X + L)+ ", " + (Y + B)+ ")" + "\n" ); } static void printVertical( int X, int Y, int L, int B) { System.out.print( "(" + X+ ", " + Y+ "), " ); System.out.print( "(" + (X + B)+ ", " + Y+ "), " ); System.out.print( "(" + X+ ", " + (Y + L)+ "), " ); System.out.print( "(" + (X + B)+ ", " + (Y + L)+ ")" + "\n" ); } // Function to find all possible rectangles static void findAllRectangles( int L, int B, int X, int Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return ; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code public static void main(String[] args) { int L = 5 , B = 3 ; int X = 9 , Y = 9 ; findAllRectangles(L, B, X, Y); } } // This code is contributed by shikhasingrajput |
Python3
# python code for the above approach def printHorizontal(X, Y, L, B): print (f "({X}, {Y}), " , end = "") print (f "({X + L}, {Y}), " , end = "") print (f "('{X}, {Y + B}), " , end = "") print (f "({X + L}, {Y + B})" ) def printVertical(X, Y, L, B): print (f "({X}, {Y}), " , end = "") print (f "({X + B}, {Y}), " , end = "") print (f "({X}, {Y + L}), " , end = "") print (f "({X + B}, {Y + L})" ) # Function to find all possible rectangles def findAllRectangles(L, B, X, Y): # First four Rectangles printHorizontal(X, Y, L, B) printHorizontal(X - L, Y, L, B) printHorizontal(X, Y - B, L, B) printHorizontal(X - L, Y - B, L, B) # If length and breadth are same # i.e, it is a square if (L = = B): return # Next four Rectangles printVertical(X, Y, L, B) printVertical(X - B, Y, L, B) printVertical(X, Y - L, L, B) printVertical(X - B, Y - L, L, B) # Driver Code if __name__ = = "__main__" : L = 5 B = 3 X = 9 Y = 9 findAllRectangles(L, B, X, Y) # This code is contributed by rakeshsahni |
C#
// C# code for the above approach using System; class GFG{ static void printHorizontal( int X, int Y, int L, int B) { Console.Write( "(" + X + ", " + Y + "), " ); Console.Write( "(" + (X + L) + ", " + Y + "), " ); Console.Write( "(" + X + ", " + (Y + B) + "), " ); Console.Write( "(" + (X + L) + ", " + (Y + B) + ")" + "\n" ); } static void printVertical( int X, int Y, int L, int B) { Console.Write( "(" + X + ", " + Y + "), " ); Console.Write( "(" + (X + B) + ", " + Y + "), " ); Console.Write( "(" + X + ", " + (Y + L) + "), " ); Console.Write( "(" + (X + B) + ", " + (Y + L) + ")" + "\n" ); } // Function to find all possible rectangles static void findAllRectangles( int L, int B, int X, int Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return ; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code public static void Main(String[] args) { int L = 5, B = 3; int X = 9, Y = 9; findAllRectangles(L, B, X, Y); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript code for the above approach function printHorizontal(X, Y, L, B) { document.write( '(' + X + ", " + Y + "), " ); document.write( '(' + (X + L) + ", " + Y + "), " ); document.write( '(' + X + ", " + (Y + B) + "), " ); document.write( '(' + (X + L) + ", " + (Y + B) + ")" + '<br>' ); } function printVertical(X, Y, L, B) { document.write( '(' + X + ", " + Y + "), " ); document.write( '(' + (X + B) + ", " + Y + "), " ); document.write( '(' + X + ", " + (Y + L) + "), " ); document.write( '(' + (X + B) + ", " + (Y + L) + ")" + '<br>' ); } // Function to find all possible rectangles function findAllRectangles(L, B, X, Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return ; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code let L = 5, B = 3; let X = 9, Y = 9; findAllRectangles(L, B, X, Y); // This code is contributed by Potta Lokesh </script> |
(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)
Please Login to comment...