C++ 两个重叠矩形的总面积
重叠区域是两个物体共有的区域。对于矩形而言,它是属于两个矩形的重叠部分的面积。为了找到两个重叠矩形的总面积,首先需要分别计算两个矩形的面积,但在这个总面积中,重叠区域被计算了两次。因此还需要减去重叠区域的面积。
问题陈述
给定两个矩形的左下角和右上角顶点,找出两个矩形覆盖的总面积。
示例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++实现
在下面的程序中,我们使用OOP的概念来创建Rectangle类及其方法。
#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)。