patterncModerate
Check if 2 rectangles fit in another one, given their length and height
Viewed 0 times
rectanglesheightlengthoneanotherandcheckfitgiventheir
Problem
I was very intrigued with the manner to solve this problem. The program needs to know if 2 rectangles fit in another one, considering the lines of the 2 rectangles are always parallel to the other one's.
I know I could just put everything in a single if, but is that more readable? I want to know a good approach for this situation, using only conditions.
NOTE:
```
#include
int main(void)
{
int x, y, l1, h1, l2, h2;
scanf("%d %d %d %d %d %d", &x, &y, &l1, &h1, &l2, &h2);
if ((l1 + l2 <= x && h1 <= y && h2 <= y) ||
(h1 + h2 <= x && l1 <= y && l2 <= y))
{
puts("They fit.");
}
else if ((l1 <= x && h2 <= x && h1 + l2 <= y) ||
(h1 <= x && l2 <= x && l1 + h2 <= y))
{
puts("They fit.");
}
I know I could just put everything in a single if, but is that more readable? I want to know a good approach for this situation, using only conditions.
NOTE:
x and y represent the first rectangle's width and height (the one that will fit the other 2) and l1, h1, l2, and h2 represent the other 2 rectangles' width and height.```
#include
int main(void)
{
int x, y, l1, h1, l2, h2;
scanf("%d %d %d %d %d %d", &x, &y, &l1, &h1, &l2, &h2);
if ((l1 + l2 <= x && h1 <= y && h2 <= y) ||
(h1 + h2 <= x && l1 <= y && l2 <= y))
{
puts("They fit.");
}
else if ((l1 <= x && h2 <= x && h1 + l2 <= y) ||
(h1 <= x && l2 <= x && l1 + h2 <= y))
{
puts("They fit.");
}
Solution
Any two rectangles can fit into a third rectangle (without overlapping with each other) only under 2 cases:
As mentioned in the question, consider:
Note: You could use structures or 2-dimensional arrays for better readability.
Case 1:
-
When
-
Here the height
Note:
Case 2:
-
When
-
Here the length
Note: similarly, even here
Now, keeping the above 2 cases in mind, a simple function can be constructed this way:
Note: here
Now, the 2 rectangles can be sent in 4 different ways to check if they can fit in the rectangle of dimensions
-
You could use the
-
The above
Altogether your code would be:
As mentioned in the question, consider:
x,y-> length and height of first rectangle into which below two rectangles must fit.
l1,h1-> length/width and height of second rectangle.
l2,h2-> length and height of third rectangle.
Note: You could use structures or 2-dimensional arrays for better readability.
Case 1:
-
When
x is greater than or equal to l1+l2 -
Here the height
y must be greater than or equal to maximum of h1 and h2 for the rectangles to fit in, else it doesn't fit in.Note:
x >= l1+12 can be written as ((x >= l1) && (x-l1 >= l2)). This is to avoid the overflow while adding integers. Case 2:
-
When
y is greater than or equal to h1+h2-
Here the length
x must be greater than or equal to maximum of l1 and l2 for the rectangles to fit in, else it doesn't fit in.Note: similarly, even here
y >= h1+h2 can be written as (y >= h1) && (y-h1 >= h2). This is to avoid the overflow while adding integers. Now, keeping the above 2 cases in mind, a simple function can be constructed this way:
int fit( int x, int y, int l1, int h1, int l2, int h2)
{
//return 1 for fit and 0 if does not fit
//case 1 or case 2
if( ( ((x >= l1) && (x-l1 >= l2)) && y >= max(h1,h2)) ||
( ((y >= h1) && (y-h1 >= h2)) && x >= max(l1,l2)) )
{
return 1;
}
return 0;
}Note: here
max() is a function which returns the larger value among the two sent arguments.int max(int a, int b)
{
if(a>b)
{
return a;
}
return b;
}Now, the 2 rectangles can be sent in 4 different ways to check if they can fit in the rectangle of dimensions
x and y://sending as entered by the user
fit( x, y, l1, h1, l2, h2)
//interchanging length and height of anyone rectangle
fit( x, y, h1, l1, l2, h2)
//interchanging length and height of other rectangle
fit( x, y, l1, h1, h2, l2)
//interchanging lengths and heights of both rectangles
fit( x, y, h1, l1, h2, l2)- Now, you can easily know whether the rectangles fit or does not fit by checking for the return value of
fit()function.
- If return value of anyone of the above four function calls is
1then the rectangles fit in!
-
You could use the
main() function as:int main(void)
{
int x, y, l1, h1, l2, h2;
scanf("%d %d %d %d %d %d", &x, &y, &l1, &h1, &l2, &h2);
//tests for fit
if(fit( x, y, l1, h1, l2, h2) ||
fit( x, y, h1, l1, l2, h2) ||
fit( x, y, l1, h1, h2, l2) ||
fit( x, y, h1, l1, h2, l2))
{
printf("fits");
}
else
{
printf("doesn't fit");
}
}-
The above
if block functions similarly as an else-if ladder because, || stops evaluation on the first true (i.e, function returns 1).- So, if any of the function returns a
1, then then"fits"gets printed out and program terminates
- In case, if none of the function calls return
1the"doesn't fit"gets printed out and program terminates
Altogether your code would be:
#include
int max(int a, int b)
{
if(a>b)
{
return a;
}
return b;
}
int fit( int x, int y, int l1, int h1, int l2, int h2)
{
//return 1 for fit and 0 if does not fit
//case 1 or case 2
if( ( ((x >= l1) && (x-l1 >= l2)) && y >= max(h1,h2)) ||
( ((y >= h1) && (y-h1 >= h2)) && x >= max(l1,l2)) )
{
return 1;
}
return 0;
}
int main(void)
{
int x, y, l1, h1, l2, h2;
scanf("%d %d %d %d %d %d", &x, &y, &l1, &h1, &l2, &h2);
//tests for fit
if( fit( x, y, l1, h1, l2, h2) ||
fit( x, y, h1, l1, l2, h2) ||
fit( x, y, l1, h1, h2, l2) ||
fit( x, y, h1, l1, h2, l2))
{
printf("fits");
}
else
{
printf("doesn't fit");
}
}Code Snippets
int fit( int x, int y, int l1, int h1, int l2, int h2)
{
//return 1 for fit and 0 if does not fit
//case 1 or case 2
if( ( ((x >= l1) && (x-l1 >= l2)) && y >= max(h1,h2)) ||
( ((y >= h1) && (y-h1 >= h2)) && x >= max(l1,l2)) )
{
return 1;
}
return 0;
}int max(int a, int b)
{
if(a>b)
{
return a;
}
return b;
}//sending as entered by the user
fit( x, y, l1, h1, l2, h2)
//interchanging length and height of anyone rectangle
fit( x, y, h1, l1, l2, h2)
//interchanging length and height of other rectangle
fit( x, y, l1, h1, h2, l2)
//interchanging lengths and heights of both rectangles
fit( x, y, h1, l1, h2, l2)int main(void)
{
int x, y, l1, h1, l2, h2;
scanf("%d %d %d %d %d %d", &x, &y, &l1, &h1, &l2, &h2);
//tests for fit
if(fit( x, y, l1, h1, l2, h2) ||
fit( x, y, h1, l1, l2, h2) ||
fit( x, y, l1, h1, h2, l2) ||
fit( x, y, h1, l1, h2, l2))
{
printf("fits");
}
else
{
printf("doesn't fit");
}
}#include <stdio.h>
int max(int a, int b)
{
if(a>b)
{
return a;
}
return b;
}
int fit( int x, int y, int l1, int h1, int l2, int h2)
{
//return 1 for fit and 0 if does not fit
//case 1 or case 2
if( ( ((x >= l1) && (x-l1 >= l2)) && y >= max(h1,h2)) ||
( ((y >= h1) && (y-h1 >= h2)) && x >= max(l1,l2)) )
{
return 1;
}
return 0;
}
int main(void)
{
int x, y, l1, h1, l2, h2;
scanf("%d %d %d %d %d %d", &x, &y, &l1, &h1, &l2, &h2);
//tests for fit
if( fit( x, y, l1, h1, l2, h2) ||
fit( x, y, h1, l1, l2, h2) ||
fit( x, y, l1, h1, h2, l2) ||
fit( x, y, h1, l1, h2, l2))
{
printf("fits");
}
else
{
printf("doesn't fit");
}
}Context
StackExchange Code Review Q#133118, answer score: 11
Revisions (0)
No revisions yet.