Question

In: Computer Science

Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.

 C LANGUAGE

Objectives:
Learn about conditional statements and Boolean operators.
Problem:
Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.
Example:
1) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (1,1) with a height equal to 2 and width equal to 3. Then L and S overlap.

2) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (100,100) with a height equal to 2 and width equal to 3. Then L and S do not overlap.

Bonus:
If two given rectangles as described above overlap, their intersection forms a new rectangle. Further determine its center, height, and width

Solutions

Expert Solution

#include
#include//for bool datatype
struct point{ //struct point to store point coordinates
int m,n; //m represents x and n represents y coordinate
};
//overlap functioin takes 4 points and return true if overlaps and false if doesn't
bool overlap(struct point p1, struct point p2, struct point p3, struct point p4){
if(p1.m>p4.m||p3.m>p2.m)
return false;
if(p1.n return false;
return true;
}
//main function
void main(){
int a,b,c,d,w1,h1,w2,h2;
printf("Enter rectange1 center: ");
scanf("%d%d",&a,&b);//a,b stores center of 1st rectangle.
printf("Enter rectangle1 width and height: ");
scanf("%d%d",&w1,&h1);//w1 and h1 stores width and height of rectangle1 respectively.
printf("Enter rectange2 center: ");
scanf("%d%d",&c,&d);//c,d stores center of 2nd rectangle.
printf("Enter rectangle2 width and height: ");
scanf("%d%d",&w2,&h2);//w2,h2 stores width and height of rectange2 respectively.
struct point p1,p2;//p1 stores the top-left point and p2 stores bottom-right of rectangle1
p1.m = a-w1/2;
p1.n = b+h1/2;
p2.m = a+w1/2;
p2.n = b-h1/2;
struct point p3,p4;//p2 stores top-left and p4 stores bottom-right points of rectangle2
p3.m = c-w2/2;
p3.n = d+h2/2;
p4.m = c+w2/2;
p4.n = d-h2/2;
bool result = overlap(p1,p2,p3,p4);
if(result)//if result is true then
printf("the two rectangles are overlapping");//this message is printed.
else printf("the rectangles are not overlapping");//else this message is printed
}

//output screenshots

//any query, post in the comment section.


Related Solutions

Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes.
 c programming Objectives:Learn about conditional statements and Boolean operators.Problem:Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.Example:1) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (1,1) with a height equal to 2 and width equal to 3. Then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT