C# 将矩形夹在较大的矩形内

C# 将矩形夹在较大的矩形内,c#,xna,xna-4.0,C#,Xna,Xna 4.0,假设我有一个带有 X = 0, Y = 0, Width = 25, Height = 25. 我想要的是一种方法,将另一个矩形夹在里面,这样较小的矩形就不会“离开”较大矩形的边界 例如,如果我使用以下内容创建一个新矩形: X = 23, Y = 20, Width = 10, Height = 10, X = 0, Y = 8, Width = 2, Height = 10. 我希望该方法返回一个矩形,其中包含: X = 23, Y = 20, Width = 2, Height = 5

假设我有一个带有

X = 0, Y = 0, Width = 25, Height = 25.
我想要的是一种方法,将另一个矩形夹在里面,这样较小的矩形就不会“离开”较大矩形的边界

例如,如果我使用以下内容创建一个新矩形:

X = 23, Y = 20, Width = 10, Height = 10,
X = 0, Y = 8, Width = 2, Height = 10.
我希望该方法返回一个矩形,其中包含:

X = 23, Y = 20, Width = 2, Height = 5.
X = -8, Y = 8, Width = 10, = 10,
另外,如果我用以下内容创建一个新矩形:

X = 23, Y = 20, Width = 2, Height = 5.
X = -8, Y = 8, Width = 10, = 10,
我希望该方法返回一个矩形,其中包含:

X = 23, Y = 20, Width = 10, Height = 10,
X = 0, Y = 8, Width = 2, Height = 10.

与此同时,我让夹具开始工作。尽管如此,它还是有大量难看的代码,所以新的问题是,如何简化它

if (SelectionRectangle.X < 0)
        {
            SelectionRectangle.Width = SelectionRectangle.Width + SelectionRectangle.X;
            SelectionRectangle.X = 0;
        }
        if (SelectionRectangle.X + SelectionRectangle.Width > tileset.Width)
        {
            if (SelectionRectangle.X > tileset.Width)
            {
                SelectionRectangle.X = tileset.Width;
                SelectionRectangle.Width = 0;
            }
            else
            {
                SelectionRectangle.Width = tileset.Width - SelectionRectangle.X;
            }
        }
        if (SelectionRectangle.Y < 0)
        {
            SelectionRectangle.Height = SelectionRectangle.Height + SelectionRectangle.Y;
            SelectionRectangle.Y = 0;
        }
        if (SelectionRectangle.Y + SelectionRectangle.Height > tileset.Height)
        {
            if (SelectionRectangle.Y > tileset.Height)
            {
                SelectionRectangle.Y = tileset.Height;
                SelectionRectangle.Height = 0;
            }
            else
            {
                SelectionRectangle.Height = tileset.Height - SelectionRectangle.Y;
            }
        }
if(SelectionRectangle.X<0)
{
SelectionRectangle.Width=SelectionRectangle.Width+SelectionRectangle.X;
SelectionRectangle.X=0;
}
如果(SelectionRectangle.X+SelectionRectangle.Width>tileset.Width)
{
如果(选择矩形.X>tileset.Width)
{
SelectionRectangle.X=tileset.Width;
SelectionRectangle.Width=0;
}
其他的
{
SelectionRectangle.Width=tileset.Width-SelectionRectangle.X;
}
}
如果(SelectionRectangle.Y<0)
{
SelectionRectangle.Height=SelectionRectangle.Height+SelectionRectangle.Y;
SelectionRectangle.Y=0;
}
如果(SelectionRectangle.Y+SelectionRectangle.Height>tileset.Height)
{
如果(选择矩形.Y>平铺设置高度)
{
SelectionRectangle.Y=tileset.Height;
SelectionRectangle.Height=0;
}
其他的
{
SelectionRectangle.Height=tileset.Height-SelectionRectangle.Y;
}
}

像这样的东西怎么样:

    Rectangle clamp(Rectangle smaller, Rectangle larger)
    {
        Rectangle ret;
        ret.X = Math.Max(smaller.X, larger.X);
        ret.Y = Math.Max(smaller.Y, larger.Y);
        ret.Width = Math.Min(smaller.X + smaller.Width, larger.X + larger.Width) - ret.X;
        ret.Height = Math.Min(smaller.Y + smaller.Height, larger.Y + larger.Height) - ret.Y;
        return ret;
    }
private void clamble(外部为矩形,内部为矩形){
如果(左内<左外){
inside.width-=outside.left-inside.left;
in.left=out.left
}
如果(内.右>外.右){
in.width-=in.right-out.right;
}
如果(内侧顶部<外侧顶部){
内.height-=外.top-内.top;
inside.top=outside.top;
}
如果(内部.bottom>外部.bottom){
in.height-=in.height-out.height;
}
}
我刚刚在这里输入了代码,请检查拼写错误。
我不确定这些属性是否带有大写字母(高度/高度…)

这不就是吗

创建一个矩形,定义一个矩形与另一个矩形重叠的区域


如果较小的矩形完全位于较大的矩形内,则交点将与较小的矩形相同。如果较小的矩形延伸到较大的矩形之外,则该部分将不会返回。

您能告诉我们您尝试了什么吗?你写的代码,为什么不起作用,你的问题是什么?你能告诉我们你在哪里使用夹钳吗?但是如果内部。右=23,外部。右=25,内部。宽度=比如说,15?右=左边框+宽度,那么如果内部。右<外部。右,这意味着内部矩形实际上在外部矩形内,在过去的两年里,我忽略了这种方法。非常感谢。