.net C#列表<;T>;。查找(x=>;x.Rectangle.Contains(点))失败
就我个人而言,我搞不懂为什么这段代码会产生以下输出 我认为使用List和lambda时,如果类型公开一个Rectangle属性,并且您使用Rectangle对象的Contains方法,那么会有一个bug或其他问题……当List Find方法失败时,显式迭代被证明是正确的.net C#列表<;T>;。查找(x=>;x.Rectangle.Contains(点))失败,.net,contains,point,.net,Contains,Point,就我个人而言,我搞不懂为什么这段代码会产生以下输出 我认为使用List和lambda时,如果类型公开一个Rectangle属性,并且您使用Rectangle对象的Contains方法,那么会有一个bug或其他问题……当List Find方法失败时,显式迭代被证明是正确的 Location to look for {X=1476,Y=1716} Found Location in grid square GS: 14.3.0.0 grid square bounds {X=1398,Y=1650,
Location to look for {X=1476,Y=1716}
Found Location in grid square GS: 14.3.0.0
grid square bounds {X=1398,Y=1650,Width=100,Height=100}
Found Location in Keypad GS: 14.3.6.0
key pad bounds {X=1465,Y=1683,Width=33,Height=34}
Sub Key Pads Print All sub keys in this grid.keypad
GS: 14.3.6.7 {X=1465,Y=1683,Width=11,Height=11}
GS: 14.3.6.8 {X=1476,Y=1683,Width=11,Height=11}
GS: 14.3.6.9 {X=1487,Y=1683,Width=11,Height=11}
GS: 14.3.6.4 {X=1465,Y=1694,Width=11,Height=11}
GS: 14.3.6.5 {X=1476,Y=1694,Width=11,Height=11}
GS: 14.3.6.6 {X=1487,Y=1694,Width=11,Height=11}
GS: 14.3.6.1 {X=1465,Y=1705,Width=11,Height=11}
GS: 14.3.6.2 {X=1476,Y=1705,Width=11,Height=11}
GS: 14.3.6.3 {X=1487,Y=1705,Width=11,Height=11}
Sub Key Pads Print Explicit Finds
True
GS: 14.3.6.1 {X=1465,Y=1705,Width=11,Height=11}
True
GS: 14.3.6.2 {X=1476,Y=1705,Width=11,Height=11}
A first chance exception of type 'System.NullReferenceException'
单向
代码
Rectangle.Contains(Point)
在矩形的上限上是独占的(严格小于)
例如,Rectangle.Contains(Point)
在您的上下文中执行的等效检查是:
foreach (GridSquare t in kp)
{
if (location.X >= t.Location.X
&& location.Y >= t.Location.Y
&& location.X < t.Location.X + t.Rectangle.Width // < instead of <=
&& location.Y < t.Location.Y + t.Rectangle.Height) // < instead of <=
{
u.dp(true);
u.dp(t.ToString() + " " + t.Rectangle.ToString());
}
}
将返回false,而您的将返回true
这就是为什么
kp.Find(x=>x.Rectangle.Contains(location))代码>返回null,但您的手动检查返回true。我发现您可以检查两件事:
您的显式检查是包含的-小于和等于,大于和等于。如果Rectangle.Contains是独占的,那么显式检查发现的两个点都将被忽略
您确定x.Location.x和.Y始终与x.Rectangle.x和.Y相同吗
这就是我发现的
考虑一下定义为0,0100的矩形
有人会假设点100100在矩形内,但事实并非如此
Contains不包括边界…换句话说,对于定义为0,0100的矩形中的所有点0,0到99,99,它只会返回true
我遇到的麻烦是,当你用GDI画那个矩形的时候…像素是向下和向右倾斜的
最终的效果是矩形。包含包括矩形的顶部和左腿,并排除矩形的底部和右腿…从图形的角度来看..您可以在鼠标点击测试期间放大到像素级别
光标可能正好出现在矩形的右下边界内…但由于矩形的独占性,命中测试返回false。包含右支腿和下支腿
SW我想指出这是非法的。。。我建议使用Mono源代码作为参考。我想你的观点是可能的,但是我怀疑链接到Mono的Cairo对不使用它的人会有任何好处。
foreach (GridSquare t in kp)
{
if (location.X >= t.Location.X
&& location.Y >= t.Location.Y
&& location.X < t.Location.X + t.Rectangle.Width // < instead of <=
&& location.Y < t.Location.Y + t.Rectangle.Height) // < instead of <=
{
u.dp(true);
u.dp(t.ToString() + " " + t.Rectangle.ToString());
}
}
GS: 14.3.6.1 {X=1465,Y=1705,Width=11,Height=11}
GS: 14.3.6.2 {X=1476,Y=1705,Width=11,Height=11}