calculate co-ordinates of lower left corner of minimum enclosing rectangle and length and width provided 2 rectangle

3.4k views Asked by At

Input and Output Format:

The 1st line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the first rectangle. The 2nd line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the second rectangle.

Output consists of 4 integers that correspond to x, y, l and w of the Union rectangle.

Sample Input :
3 8 1515
2 6 10 10
Sample Output:
2 6 16 17

here is my code it gets validated for some test cases and it is not accepted when I submit it. I'm trying this on an online coding website. here is my code.

#include<stdio.h>
#include<math.h>
int main()
{
    int x1,y1,x2,y2,l1,w1,l2,w2,x3,y3,l3,w3;
    scanf("%d %d %d %d",&x1,&y1,&l1,&w1);
    scanf("\n%d %d %d %d",&x2,&y2,&l2,&w2);
    if(x1<x2)
        x3=x1;
    else
        x3=x2;
    if(y1<y2)
        y3=y1;
    else
        y3=y2;
    if(x1==x2)
    {
        if(l1<l2)
            w3=l2;
        else
            w3=l1;
    }
    if(y1==y2)
    { 
        // printf("inp");
        if(w1<w2) 
        {
            w3=w2;
            //printf("%d",w3);
        }
        else
        {
            w3=w1;
        }
    }
    if(x1<x2)
        l3=l2+fabs(x1-x2);
    else if(x2<x1)
        l3=l1+fabs(x1-x2);
    if(y1<y2)
        w3=w2+fabs(y1-y2);
    else if(y2<y1)
        w3=w1+fabs(y1-y2);
    printf("%d ",x3);
    printf("%d ",y3);
    printf("%d ",l3);
    printf("%d",w3);
    return 0;
}

if anyone have alternative logic then tell me.

2

There are 2 answers

4
Jashaszun On
#include<stdio.h>
int main()
{
    int x1, y1, x2, y2, l1, w1, l2, w2;
    scanf("%d %d %d %d",&x1,&y1,&l1,&w1);
    scanf("\n%d %d %d %d",&x2,&y2,&l2,&w2);

    int min_x = x1 < x2 ? x1 : x2;
    int min_y = y1 < y2 ? y1 : y2;
    int max_x = (x1+w1) > (x2+w2) ? (x1+w1) : (x2+w2);
    int max_y = (y1+l1) > (y2+l2) ? (y1+l1) : (y2+l2);

    int max_w = max_x - min_x;
    int max_l = max_y - min_y;

    printf("%d %d %d %d", min_x, min_y, max_l, max_w);
    return 0;
}

This is assuming that your widths are in the x direction and your lengths are in the y direction. If it is the other way, it should not be too hard to change.

0
620d On

I have faced the same problem in the online course... here is my link to the question. union of two rectangles.write a program to find the smallest possible rectangle enclosing the 2 given rectangles and my program which is working and is accepted without any error or "wrong answer"..

#include<stdio.h>
int main() {
  int x1, x2, y1, y2, l1, l2, w1, w2, xmax, xmin, ymax, ymin;
  scanf(“%d %d %d %d\n”,&x1,&y1,&l1,&w1);
  scanf(“%d %d %d %d\n”,&x2,&y2,&l2,&w2);
  xmin = x1 < x2 ? x1 : x2;
  ymin = y1 < y2 ? y1 : y2;
  int b = x1 + l1;
  int c = x2 + l2;
  xmax = b > c ? b : c;
  int d = y1 + w1;
  int e = y2 + w2;
  ymax = d > e ? d : e;
  int l = xmax - xmin;
  int w = ymax - ymin;
  printf(“%d %d %d %d”,xmin,ymin,l,w);
  return 0;
}