📌  相关文章
📜  查找具有给定顶点和尺寸的所有可能矩形的顶点坐标

📅  最后修改于: 2022-05-13 01:56:07.691000             🧑  作者: Mango

查找具有给定顶点和尺寸的所有可能矩形的顶点坐标

给定两个整数LB代表一个矩形的长和宽,一个坐标(X, Y)代表笛卡尔平面上的一个点,任务是找到所有以(X, Y)为顶点的矩形的坐标给定尺寸。

例子:

方法:可以观察到,对于给定的长度和宽度以及顶点(X, Y) ,可能有八个矩形,如下图所示:

如果矩形的给定长度和宽度相等,则水平和垂直矩形都将表示相同的坐标。因此,图 1 或图 2 中仅可能显示 4 个独特的正方形。

下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
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


输出:
(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)

时间复杂度: O(1)
辅助空间: O(1)