Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

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

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 completely inside the other (or they are exactly the same).

And it'll be a bit harder since I'll actually have to make the program so the user can decide how many rectangles they want to create, but at first I'd be happy to know how to check in the case of 2 rectangles.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...