Find out if a rectangle is inside another rectangle [C]

后端 未结 2 1036
天涯浪人
天涯浪人 2020-12-20 21:39

So I have two rectangles, the user has to input the bottom left point (x1,y1, but y1 is always 0) and the top right point (x2,y2), and I have to find out if one of them is

相关标签:
2条回答
  • 2020-12-20 21:56

    Below is comparing the sides of the inner rectangle to the sides of the outer rectangle

    if Right2 < Right1 && Left2 > Left1 && Top2 > Top1 && Bottom2 < Bottom1
    

    Implementation:

    struct RECT
    {
        double x,y, w,h;
    
        RECT(double a,double b,double c,double d)
        {
        x=a; y=b; w=c; h=d;
        }
    };
    
    
    bool contains(RECT R1, RECT R2)
    {
        if (   (R2.x+R2.w) < (R1.x+R1.w)
            && (R2.x) > (R1.x)
            && (R2.y) > (R1.y)
            && (R2.y+R2.h) < (R1.y+R1.h)
            )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 22:05

    Well, by definition one rectangle is inside of another if all the points of the inner rectangle are within the outer rectangle. Using a bit of geometry you can boil it down to checking whether the two opposite corners of the inner rectangle are in the outer rectangle.

    0 讨论(0)
提交回复
热议问题