Finding overlapping Area of rectangles c++


#1

int position(int a,int b,int c,int d)
{
if(a<=c && d<=b){return abs(d-c); }
if(a>=c && d>=b){return abs(b-a); }
if(a>c && d<b && d>a){return abs(a-d);}
if(a<c && c<b && d>b){return abs(b-c); }
if(d<=a){return 0;}
if(c>=b){return 0;}

}

int Solution::solve(int A, int B, int C, int D, int E, int F, int G, int H) {
if(A==C || E==G || B==D || F==H)
return 0;

int fx[2]={0,0} ;
int sx[2]={0,0} ;

int fy[2]={0,0} ;
int sy[2]={0,0} ;

if(A<C){
fx[0]=A;fx[1]=C;}
else {fx[1]=A;fx[0]=C;}

if(E<G){
sx[0]=E;sx[1]=G;}
else {sx[1]=E;sx[0]=G;}

if(B<D){
fy[0]=B;fy[1]=D;}
else {fy[1]=B;fy[0]=B;}
if(F<H){
sy[0]=F;sy[1]=H;}
else {sy[1]=F;sy[0]=H;}

return position(fx[0],fx[1],sx[0],sx[1])*position(fy[0],fy[1],sy[0],sy[1]);
}