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
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:
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++
#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;
}
void findAllRectangles( int L, int B, int X, int Y)
{
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 (L == B)
return ;
printVertical(X, Y, L, B);
printVertical(X - B, Y, L, B);
printVertical(X, Y - L, L, B);
printVertical(X - B, Y - L, L, B);
}
int main()
{
int L = 5, B = 3;
int X = 9, Y = 9;
findAllRectangles(L, B, X, Y);
}
|
Java
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" );
}
static void findAllRectangles( int L, int B, int X, int Y)
{
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 (L == B)
return ;
printVertical(X, Y, L, B);
printVertical(X - B, Y, L, B);
printVertical(X, Y - L, L, B);
printVertical(X - B, Y - L, L, B);
}
public static void main(String[] args)
{
int L = 5 , B = 3 ;
int X = 9 , Y = 9 ;
findAllRectangles(L, B, X, Y);
}
}
|
Python3
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})" )
def findAllRectangles(L, B, X, Y):
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 (L = = B):
return
printVertical(X, Y, L, B)
printVertical(X - B, Y, L, B)
printVertical(X, Y - L, L, B)
printVertical(X - B, Y - L, L, B)
if __name__ = = "__main__" :
L = 5
B = 3
X = 9
Y = 9
findAllRectangles(L, B, X, Y)
|
C#
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" );
}
static void findAllRectangles( int L, int B, int X, int Y)
{
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 (L == B)
return ;
printVertical(X, Y, L, B);
printVertical(X - B, Y, L, B);
printVertical(X, Y - L, L, B);
printVertical(X - B, Y - L, L, B);
}
public static void Main(String[] args)
{
int L = 5, B = 3;
int X = 9, Y = 9;
findAllRectangles(L, B, X, Y);
}
}
|
Javascript
<script>
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 findAllRectangles(L, B, X, Y)
{
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 (L == B)
return ;
printVertical(X, Y, L, B);
printVertical(X - B, Y, L, B);
printVertical(X, Y - L, L, B);
printVertical(X - B, Y - L, L, B);
}
let L = 5, B = 3;
let X = 9, Y = 9;
findAllRectangles(L, B, X, Y);
</script>
|
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)
Last Updated :
11 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...