samedi 23 mai 2020

What is wrong with my code to calculate the overlapping area of two rectangles in c++?

My goal is to calculate the area of the complete figure in the coordinate plane, excluding the intersecting area. Can someone help me see what I need to fix?

#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; }

Aucun commentaire:

Enregistrer un commentaire