Skip to main content
added 17 characters in body
Source Link
Tunaki
  • 9.3k
  • 1
  • 30
  • 46

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: xx and yy represent the first rectangle's width and height (the one that will fit the other 2) and l1l1, h1h1, l2l2, and h2h2 represent the other 2 rectangles' width and height.

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: 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.

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: 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.

Tweeted twitter.com/StackCodeReview/status/747330534892703745
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
Lúcio Cardoso
  • 837
  • 1
  • 6
  • 16

Check if 2 rectangles fit in another one, given their length and height

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: 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 <stdio.h>                                                                                  
                                                                                                    
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.");                                                                                                                                                             
    }                                                                                                                        
    else if ((l1 <= y && h2 <= y && h1 + l2 <= x) || 
    (h1 <= y && l2 <= y && l1 + h2 <= x))          
    {                                                                                               
        puts("They fit.");                                                                                                                                                             
    }                                                                                                                       
    else if ((l1 + l2 <= y && h1 <= x && h2 <= x) || 
    (h1 + h2 <= y && l1 <= x && l2 <= x))          
    {                                                                                               
        puts("They fit.");                                                                                                                                                          
    }                                                                                                                                                               
    else                                                                                            
    {                                                                                               
        puts("They don't fit.");                                                                                                                                                           
    }   
    return 0;
}