0

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;
}
haccks
  • 104,019
  • 25
  • 176
  • 264
user2805620
  • 125
  • 1
  • 1
  • 13

3 Answers3

1

step 1 - The culprit is "\n" in scanf. If you remove that it will work Let me know if you need any specific help in Step 2 or Step 3.

0

First thing:

return scanf("%d%d%d%d\n",w,x,y,z);

should be

return scanf("%d %d %d %d",w,x,y,z);

Then you can enter your list of numbers as a space separated list, and it will scan them correctly.

The other parts of your question, you would have to attempt to solve yourself, make your problem more specific, and raise as new questions.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
0

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

printf("intersection rectangle: (%d,%d)(%d,%d)\n", maxInt(a,e), maxInt(b,f), minInt(c,g), minInt(d,h) );
haccks
  • 104,019
  • 25
  • 176
  • 264
  • And for printf printing the intersection? What should the variables be? I'm really grateful for your help.I'm just a beginner programmer, I never started programming when I was child like many of you have. But people just keep down voting me, sure, my questions are assignment related, but I'm not using the people of the internet to do my assignments, they are who can help me learn. – user2805620 Oct 14 '13 at 04:20
  • Actually I am confused with your question and the output you are showing. In your program you are inputting sides of rectangle but your question is telling about vertices: `As shown below where a rectangle is shown as its lower left coordinates (xmin, ymin) and top right corner coordinates (xmax, ymax).` – haccks Oct 14 '13 at 07:12
  • 1
    The program is actually for inputting the lower left vertex and upper right vertex of a rectangle. That's what the coordinates are. I'm really sorry for the misunderstanding. – user2805620 Oct 14 '13 at 07:49
  • I have edited my answer. Now it is working like charm! – haccks Oct 14 '13 at 08:30
  • Yes it will. Look at your `minInt` function that should be `int minInt(int x, int y){ if(x < y){ return x; }else{ return y; } }`. But you are returning `y` for `if` and `else` both! – haccks Oct 14 '13 at 08:51
  • Yea I made a mistake, sorry for that. Is putting it in a do while loop the best way to make it go into an infinite loop? – user2805620 Oct 14 '13 at 08:53
  • Yes. You can put it in a `do while` loop or better to put in a `while` loop. – haccks Oct 14 '13 at 08:57
  • Thanks for being patient with me, as well for all your help, I really appreciate it. Its really hard to get help as an inexperienced programmer on here, some people just don't that get others have different starting points, like I was only exposed to programming only a month ago, and people here expect me to know a lot by now. Sorry for ranting. – user2805620 Oct 14 '13 at 09:08
  • It doesn't matter you have started programming late but how much you are devoted to it matters a lot. I started programming only 4 months ago and now I am answering some basic questions on SO. Never underestimate your self. Do not bother about harsh comments by people around you. Keep trying to improve yourself. And yes be confident. – haccks Oct 14 '13 at 09:33