Write a function to check if two rectangles defined
as below, have common area or not. The functions take the top left and
bottom right coordinate as input and return 1 if they have common area,
otherwise return 0.
Input Output
10 1
20
20
10
10
20
20
10
Input Output
10 1
20
20
10
10
20
20
10
#include
ReplyDeletetypedef struct Rectangle{
int x1;
int y1;;
int x2;
int y2;
}Rect;
int areOverlapping( Rect A, Rect B ) {
if( B.x1>=A.x2) //second rectangle is outside first rectangle at right hand side
return 0;
else
{
if(B.x2<=A.x1) //second rectangle is outside first rectangle at left hand side
return 0;
else
{
if(B.y2>=A.y1) // second rectangle is above first rectangle
return 0;
else
{
if(B.y1<=A.y2) //second rectangle is below first rectangle
return 0;
else
return 1;
}
}
}
}
int main(void) {
Rect A={10,20,20,10};
Rect B={10,20,20,10};
printf("Are rectangles overlapping? %d",areOverlapping(A,B));
return 0;
}