兩個重疊矩形的總面積


重疊區域是兩個物體共享的區域。對於矩形來說,它是屬於兩個矩形的區域。為了找到兩個重疊矩形的總面積,首先我們需要分別新增兩個矩形的面積,但在這個總數中,重疊區域被計算了兩次。因此,我們也需要減去重疊區域。

問題陳述

給定兩個矩形的左下和右上頂點。找到這兩個矩形覆蓋的總面積。

示例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 次檢視

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告