可以说我有一个XNA矩形与钳矩形内较大的矩形
X = 0, Y = 0, Width = 25, Height = 25.
我想是内部夹具另一个矩形的方法,使小矩形从不“叶子”中较大的边界。
例如,如果我提出一个新的Rectangle有:
X = 23, Y = 20, Width = 10, Height = 10,
我想返回具有矩形的方法:
X = 23, Y = 20, Width = 2, Height = 5.
而且,如果我做一个新的Rectangle有:
X = -8, Y = 8, Width = 10, = 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;
}
}
你能告诉我们你试过了什么吗?你写的代码,为什么没有工作,你的问题是什么? – TJHeuvel 2012-03-28 13:19:41
你能告诉我们你在哪里使用夹子吗? – 2012-03-28 13:20:38