Are multiple conditions, as in multiple if else statements needed for the intersection rectangles to be printed correctly?
Step 3: Two rectangles intersect if they have an area in common Two rectangles do not overlap if they just touch (common edge, or common corner)
Two rectangles intersect(as specified above)if, and only if,
i) max(xmin1, xmin2) < min(xmax1, xmax2) and
ii) max(ymin1, ymin2) < min(ymax1, ymax2)
Your output is to be formatted. As shown below where a rectangle is shown as its lower left coordinates (xmin, ymin) and top right corner coordinates (xmax, ymax). Where the coordinates are coordinates in a Cartesian plane.
Sample output:
enter two rectangles:
1 1 4 4
2 2 5 5
rectangle 1: (1,1)(4,4)
rectangle 2: (2,2)(5,5)
intersection rectangle: (2,2)(4,4)
and
enter two rectangles:
1 1 4 4
5 5 10 10
rectangle 1: (1,1)(4,4)
rectangle 2: (5,5)(10,10)
these two rectangles do not intersect
Code:
#include <stdio.h>
#include <stdlib.h>
int readRect (int *w, int *x, int *y, int *z){
return scanf("%d%d%d%d",w,x,y,z);
}
int minInt(int x1, int x2){
return x1, x2;
}
int maxInt(int y1, int y2){
return y1, y2;
}
int main (void){
int a,b,c,d,e,f,g,h;
printf(">>enter two rectangles:\n");
readRect(&a,&b,&c,&d);
readRect(&e,&f,&g,&h);
printf("rectangle 1:(%d,%d)(%d,%d)\n",a,b,c,d);
printf("rectangle 2:(%d,%d)(%d,%d)\n",e,f,g,h);
if(maxInt(a,e) < minInt(c,g) && maxInt(b,f) < minInt(d,g)){
printf("intersection rectangle: (%d,%d)(%d,%d)\n",?,?,?,?);
}
else {
printf("these rectangles do not intersect\n");
}
return EXIT_SUCCESS;
}
Solution 2
Your function for max and min is wrong.
1. you are not comparing the parameter passed inside these functions for maximum/minimum of two.
2. You can't return two values from a function.
You should do like this;
int minInt(int x1, int x2){
if(x1 < x2)
return x1;
else
return x2;
}
int maxInt(int x1, int x2){
if(x1 > x2)
return x1;
else
return x2;
}
And change your printf printing the intersection rectangle to