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

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

问题描述

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



而且它会更难一些,因为我实际上必须让程序成为用户可以决定他们想要创建多少个矩形,但起初我很乐意知道如何检查两个矩形的情况。

解决方案 div>

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

  if Right2< Right1&& Left2>左1&& Top2> Top1&&底部2<底部1 

执行:

  struct RECT 
{
double x,y,w,h;

RECT(double a,double b,double c,double d)
{
x = a; Y = B;瓦特= C; H = d;
}
};如果((R2.x + R2.w)<(R1.x + R1)),bool包含(RECT R1,RECT R2)
{
b

。 (R1.x)
&&(R2.y)>(R1.y)
&& (R2.y + R2.h)<(R1.y + R1.h)

{
return true;
}
else
{
return false;
}
}


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.

解决方案

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

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

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