ax1,ay1为第一个矩形的左下角,ax2,ay2为第一个矩形的左上角;
bx1,by1为第二个矩形的左下角,bx2,by2为第二个矩形的右上角。
#include <iostream>
using namespace std;
class Solution {
public:
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int Max_X = max(ax2, bx2); //右边界
int Min_X = min(ax1, bx1); //下边界
int Max_Y = max(by1, by2); //上边界
int Min_Y = min(ay1, ay2); //下边界
int width = (ax2 - ax1) + (bx2 - bx1) - (Max_X - Min_X);//重叠部分的宽
int height = (ay2 - ay1) + (by2 - by1) - (Max_Y - Min_Y);//重叠部分的宽
if (width > 0 && height > 0)
{
int area = width * height; //重叠部分面积
}
else
{
return 0;
}
}
};
int main(int argc, char** argv)
{
Solution s;
cout << s.computeArea(0, 0, 4, 4, 2, 2, 6, 6) << endl;
return 0;
}