两个重叠矩形的总面积


重叠区域是两个物体共享的区域。对于矩形来说,它是属于两个矩形的区域。为了找到两个重叠矩形的总面积,首先我们需要分别添加两个矩形的面积,但在这个总数中,重叠区域被计算了两次。因此,我们也需要减去重叠区域。

问题陈述

给定两个矩形的左下和右上顶点。找到这两个矩形覆盖的总面积。

示例1

输入

bl_x1 = 0
bl_y1 = 0
tr_x1 = 5
tr_y1 = 5
bl_x2 = 3
bl_y2 = 3
tr_x2 = 8
tr_y2 = 8

输出

46

解释

Area of rectangle1 = (5 - 0) * (5 - 0) = 25
Area of rectangle2 = (8 - 3) * (8 - 3) = 25
Overlapping area = 4
Total area = 25 + 25 - 4 = 46

示例2

输入

bl_x1 = -5
bl_y1 = -5
tr_x1 = 2
tr_y1 = 2
bl_x2 = 4
bl_y2 = 4
tr_x2 = 2
tr_y2 = 2

输出

53

解释

Area of rectangle1 = (-5 - 2) * (-5 - 2) = 49
Area of rectangle2 = (4 - 2) * (4 - 2) = 4
Overlapping area = 0
Total area = 49 + 4 - 0 =53

方法1:暴力法

在这种方法中,我们分别找到两个矩形的面积和重叠面积。两个矩形的面积可以通过矩形给出的坐标找到。为了找到重叠面积,我们找到重叠区域的交点,然后计算面积。

伪代码

procedure overlapArea (rect1[], rect2[])
   x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0]))
y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1]))
ans = x * y
end procedure

procedure totalArea (rect1[], rect2[])
   area1 = rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1]
   area2 = rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1]
   overlap = overlapArea(rect1, rect2)
   ans = area1 + area2 - overlap
end procedure

示例:C++ 实现

在下面的程序中,找到矩形的面积和重叠面积以获得总面积。

#include <bits/stdc++.h>
using namespace std;

// Function to calculate the overlapping area
int overlapArea(int rect1[], int rect2[]){

   //Finding the length and width of the overlap area
   int x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0]));
   int y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1]));
   int area = x * y;
   return area;
}

// Function to calculate the total area of two rectangles
int totalArea(int rect1[], int rect2[]){

   // Area of rectangle 1
   int area1 = abs(rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1]);
   
   // Area of rectangle 2
   int area2 = abs(rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1]);
   int overlap = overlapArea(rect1, rect2);
   
   // Total area is the area of two rectangles minus the common overlap area
   int total = area1 + area2 - overlap;
   return total;
}
int main(){
   int rect1[4] = {0, 0, 5, 5}, rect2[4] = {3, 3, 8, 8};
   cout << "Total area = " << totalArea(rect1, rect2);
   return 0;
}

输出

Total area = 46

方法2:确认重叠

为了减少矩形不重叠的情况下的计算工作,我们可以首先确认矩形是否重叠,然后计算重叠面积。

伪代码

procedure overlapArea (rect1[], rect2[])
   x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0]))
y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1]))
ans = x * y
end procedure

procedure totalArea (rect1[], rect2[])
   area1 = rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1]
   area2 = rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1]
   if no overlap
      ans = area1 + area2
else
            overlap = overlapArea(rect1, rect2)
                  ans = area1 + area2 - overlap
end procedure

示例:C++ 实现

在下面的程序中,我们首先检查矩形是否重叠,然后相应地计算重叠面积。

#include <bits/stdc++.h>
using namespace std;

// Function to calculate the overlapping area
int overlapArea(int rect1[], int rect2[]){

   //Finding the length and width of overlap area
   int x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0]));
   int y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1]));
   int area = x * y;
   return area;
}
int totalArea(int rect1[], int rect2[]){
   int area1 = abs(rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1]);
   int area2 = abs(rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1]);
   
   // Checking for overlap
   if (rect1[0] > rect2[2] || rect2[0] > rect1[2] || rect1[1] > rect2[3] || rect2[1] > rect1[3])    {
   
      // No overlap
      return area1 + area2;
   } else {
   
      // Overlap
      int overlap = overlapArea(rect1, rect2);
      return area1 + area2 - overlap;
   }
}
int main(){
   int rect1[4] = {0, 0, 5, 5}, rect2[4] = {6, 6, 8, 8};
   cout << "Total area = " << totalArea(rect1, rect2);
   return 0;
}

输出

Total area = 29

方法3:面向对象编程

在这种方法中,我们将矩形表示为对象,并将它们的面积和重叠面积计算作为这些对象的方法。这种方法使代码更易读、可重用和高效。

伪代码

Define Rectangle Class
   Define Rectangle
   Define area()
   Define overlapArea()
   Define totalArea()

示例:C++ 实现

在下面的程序中,我们使用 OOPs 的概念来创建矩形类及其方法。

#include <bits/stdc++.h>
using namespace std;

class Rectangle{
public:
   int x1, y1, x2, y2;
   Rectangle(int x1, int y1, int x2, int y2){
      this->x1 = x1;
      this->y1 = y1;
      this->x2 = x2;
      this->y2 = y2;
   }
   int area(){
      return abs(x2 - x1) * abs(y2 - y1);
   }
   int overlapArea(Rectangle second){
      int x = max(0, min(x2, second.x2) - max(x1, second.x1));
      int y = max(0, min(y2, second.y2) - max(y1, second.y1));
      int area = x * y;
      return area;
   }
   int totalArea(Rectangle second){
      int overlap = overlapArea(second);
      int total = area() + second.area() - overlap;
      return total;
   }
};
int main(){
   Rectangle rect1(1, 1, 5, 5);
   Rectangle rect2(3, 3, 6, 6);
   std::cout << "Total area = " << rect1.totalArea(rect2);
   return 0;
}

输出

Total area = 21

结论

总之,为了找到重叠矩形的总面积,我们讨论了暴力法和其他一些优化方法来增强用户友好性。每种方法的时间和空间复杂度都是 O(1)。

更新于:2023年7月25日

956 次查看

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.