找出一个矩形是否在另一个矩形内 [C] [英] Find out if a rectangle is inside another rectangle [C]

查看:50
本文介绍了找出一个矩形是否在另一个矩形内 [C]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个矩形,用户必须输入左下角(x1,y1,但 y1 始终为 0)和右上角(x2,y2),我必须找出其中一个完全在另一个内部(或者它们完全相同).

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

这会有点困难,因为我实际上必须制作程序,以便用户可以决定他们想要创建多少个矩形,但起初我很高兴知道如何检查案例2 个矩形.

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.

推荐答案

下面是比较内部矩形的边和外部矩形的边

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

实施:

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

这篇关于找出一个矩形是否在另一个矩形内 [C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆