📜  两个矩形并集的周长

📅  最后修改于: 2021-09-02 06:29:05             🧑  作者: Mango

给定两个数组X[]Y[] ,每个数组的长度为4 ,其中(X[0], Y[0])(X[1], Y[1])表示一个数组的左下角和右上角矩形和(X[2], Y[2])(X[3], Y[3]) 分别代表另一个矩形的左下角和右上角,任务是找到矩形的外边界的周长两个矩形的并集如下所示。

例子:

处理方法:按照以下步骤解决问题:

  • 检查给定点形成的矩形是否相交。
  • 如果发现相交,那么周长可以通过公式2*((X[1] – X[0]) + (X[3] – X[2]) + (Y[1] – Y[ 0]) + (Y[3] – Y[2]))
  • 否则,分别打印两次XY坐标之间最大差值的总和,即2 * (max(X[]) – min(X[]) + max(Y[]) – min(Y[]))

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if two
// rectangles are intersecting or not
bool doIntersect(vector X,
                 vector Y)
{
    // If one rectangle is to the
    // right of other's right edge
    if (X[0] > X[3] || X[2] > X[1])
        return false;
 
    // If one rectangle is on the
    // top of other's top edge
    if (Y[0] > Y[3] || Y[2] > Y[1])
        return false;
 
    return true;
}
 
// Function to return the perimeter of
// the Union of Two Rectangles
int getUnionPerimeter(vector X,
                      vector Y)
{
    // Stores the resultant perimeter
    int perimeter = 0;
 
    // If rectangles do not interesect
    if (!doIntersect(X, Y)) {
 
        // Perimeter of Rectangle 1
        perimeter
            += 2 * (abs(X[1] - X[0])
                    + abs(Y[1] - Y[0]));
 
        // Perimeter of Rectangle 2
        perimeter
            += 2 * (abs(X[3] - X[2])
                    + abs(Y[3] - Y[2]));
    }
 
    // If the rectangles intersect
    else {
 
        // Get width of combined figure
        int w = *max_element(X.begin(),
                             X.end())
                - *min_element(X.begin(),
                               X.end());
 
        // Get length of combined figure
        int l = *max_element(Y.begin(),
                             Y.end())
                - *min_element(Y.begin(),
                               Y.end());
 
        perimeter = 2 * (l + w);
    }
 
    // Return the perimeter
    return perimeter;
}
 
// Driver Code
int main()
{
    vector X{ -1, 2, 4, 6 };
    vector Y{ 2, 5, 3, 7 };
 
    cout << getUnionPerimeter(X, Y);
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to check if two
// rectangles are intersecting or not
static boolean doIntersect(int []X,
                 int []Y)
{
    // If one rectangle is to the
    // right of other's right edge
    if (X[0] > X[3] || X[2] > X[1])
        return false;
 
    // If one rectangle is on the
    // top of other's top edge
    if (Y[0] > Y[3] || Y[2] > Y[1])
        return false;
 
    return true;
}
 
// Function to return the perimeter of
// the Union of Two Rectangles
static int getUnionPerimeter(int []X,
                      int []Y)
{
    // Stores the resultant perimeter
    int perimeter = 0;
 
    // If rectangles do not interesect
    if (!doIntersect(X, Y)) {
 
        // Perimeter of Rectangle 1
        perimeter
            += 2 * (Math.abs(X[1] - X[0])
                    + Math.abs(Y[1] - Y[0]));
 
        // Perimeter of Rectangle 2
        perimeter
            += 2 * (Math.abs(X[3] - X[2])
                    + Math.abs(Y[3] - Y[2]));
    }
 
    // If the rectangles intersect
    else {
 
        // Get width of combined figure
        int w = Arrays.stream(X).max().getAsInt()
                - Arrays.stream(X).min().getAsInt();
 
        // Get length of combined figure
        int l = Arrays.stream(Y).max().getAsInt()
                - Arrays.stream(Y).min().getAsInt();
 
        perimeter = 2 * (l + w);
    }
 
    // Return the perimeter
    return perimeter;
}
 
// Driver Code
public static void main(String[] args)
{
    int []X = { -1, 2, 4, 6 };
    int []Y = { 2, 5, 3, 7 };
 
    System.out.print(getUnionPerimeter(X, Y));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check if two
# rectangles are intersecting or not
def doIntersect(X, Y):
     
    # If one rectangle is to the
    # right of other's right edge
    if (X[0] > X[3] or X[2] > X[1]):
        return False
 
    # If one rectangle is on the
    # top of other's top edge
    if (Y[0] > Y[3] or Y[2] > Y[1]):
        return False
    return True
 
# Function to return the perimeter of
# the Union of Two Rectangles
def getUnionPerimeter(X, Y):
   
    # Stores the resultant perimeter
    perimeter = 0
 
    # If rectangles do not interesect
    if (not doIntersect(X, Y)):
 
        # Perimeter of Rectangle 1
        perimeter += 2 * (abs(X[1] - X[0]) + abs(Y[1] - Y[0]))
 
        # Perimeter of Rectangle 2
        perimeter += 2 * (abs(X[3] - X[2]) + abs(Y[3] - Y[2]))
     
    # If the rectangles intersect
    else:
 
        # Get width of combined figure
        w = max(X) -  min(X)
         
        # Get length of combined figure
        l = max(Y) - min(Y)
        perimeter = 2 * (l + w)
 
    # Return the perimeter
    return perimeter
 
# Driver Code
if __name__ == '__main__':
    X = [ -1, 2, 4, 6]
    Y = [ 2, 5, 3, 7 ]
 
    print (getUnionPerimeter(X, Y))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Linq;
public class GFG
{
 
// Function to check if two
// rectangles are intersecting or not
static bool doIntersect(int []X,
                 int []Y)
{
   
    // If one rectangle is to the
    // right of other's right edge
    if (X[0] > X[3] || X[2] > X[1])
        return false;
 
    // If one rectangle is on the
    // top of other's top edge
    if (Y[0] > Y[3] || Y[2] > Y[1])
        return false;
 
    return true;
}
 
// Function to return the perimeter of
// the Union of Two Rectangles
static int getUnionPerimeter(int []X,
                      int []Y)
{
   
    // Stores the resultant perimeter
    int perimeter = 0;
 
    // If rectangles do not interesect
    if (!doIntersect(X, Y))
    {
 
        // Perimeter of Rectangle 1
        perimeter
            += 2 * (Math.Abs(X[1] - X[0])
                    + Math.Abs(Y[1] - Y[0]));
 
        // Perimeter of Rectangle 2
        perimeter
            += 2 * (Math.Abs(X[3] - X[2])
                    + Math.Abs(Y[3] - Y[2]));
    }
 
    // If the rectangles intersect
    else
    {
 
        // Get width of combined figure
        int w = X.Max()
                - X.Min();
 
        // Get length of combined figure
        int l = X.Max()
                - Y.Min();
 
        perimeter = 2 * (l + w);
    }
 
    // Return the perimeter
    return perimeter;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []X = { -1, 2, 4, 6 };
    int []Y = { 2, 5, 3, 7 };
 
    Console.Write(getUnionPerimeter(X, Y));
}
}
 
// This code contributed by shikhasingrajput


输出:
24

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live