我的代码在c ++中计算两个矩形的重叠面积有什么问题?

我的目标是在坐标平面中计算完整图形的面积,不包括相交面积。有人可以帮我看看我需要解决什么吗?

#include <iostream>
#include <cstdlib>

using namespace std;

int minusOverlap(int lx, int ly, int rx, int ry, int llx, int lly, int rrx, int rry);

int main() {

cout << minusOverlap(1, 1, 3, 3, 2, 2, 5, 5);
}

int minusOverlap(int lx, int ly, int rx, int ry, int llx, int lly, int rrx, int rry){

 int a1, a2, overlap;

 //Calculate the area of rect 1 and 2
 //lower left is (lx, ly)
 //upper right is (rx, ry)

a1 = abs(rx-lx) * abs(ry - ly);
a2 = abs(rrx - llx) * abs(rry - lly);

//Calculate intersections using max and minus

overlap = (min(rx, rrx) - max(lx, llx)) 
*
 (min(rry, ry) * max(ly, lly));

   return a1 + a2 - 2*overlap; }
评论