C # List <T> .Find (x => x.Rectangle.Contains (Point)) FAIL

In life, I cannot understand why this code produces the following output ...

I think there is an error or something when using List and lambda if the type provides the Rectangle property and you use the Contains method of the rectangle object ... and an explicit iteration proves the truth until the List Find method works ....

Oneway

the code

public GridSquare WorldToKeyPadSub(Point location)
    {
        location = _Map.WorldToClient(location);
        GridSquare gs = this.Find(x => x.Rectangle.Contains(location));
        GridSquare kp = gs.Find(x => x.Rectangle.Contains(location));
        List<GridSquare> list = kp.FindAll(x=>x.Rectangle.Contains(location));
        u.dp(list.Count);
        GridSquare sub = kp.Find(x => x.Rectangle.Contains(location));

        if (sub == null)
        {
            u.dp("Location to look for " + location);
            u.dp("Found Location in grid square " + gs.ToString());
            u.dp("grid square bounds " + gs.Rectangle.ToString());
            u.dp("Found Location in Keypad " + kp.ToString());
            u.dp("key pad bounds " + kp.Rectangle.ToString());
            u.dp("Sub Key Pads Print All sub keys in this grid.keypad");
            foreach (GridSquare t in kp)
            {
                u.dp(t.ToString() + "  " + t.Rectangle.ToString());                   

            }
            u.dp("Sub Key Pads Print Explicit Finds");
            foreach (GridSquare t in kp)
            {
                if (location.X >= t.Location.X
                    && location.Y >= t.Location.Y
                    && location.X <= t.Location.X + t.Rectangle.Width
                    && location.Y <= t.Location.Y + t.Rectangle.Height)
                {
                    u.dp(true);
                    u.dp(t.ToString() + "  " + t.Rectangle.ToString());
                }

            }
        }
        return sub;
    }

This creates the following result ...

Notice how the explicit Rectangle (the so-called Manual method) finds grid squares that contain the location .... the internal version of GDI does not work ....

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'
+3
source share
3 answers

Rectangle.Contains(Point) is exceptional (strictly smaller) along the upper boundaries of the rectangle.

, , 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()); 
    } 

} 

, , , Rectangle.Contains(Point).

, , - {X = 1476, Y = 1716}, :

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}  

false, true.

kp.Find(x => x.Rectangle.Contains(location)); null, .

+6

...

, 0,0,100,100...

, 100 100 , ...

Rectangle.Contains ... , true 0,0 99,99 , 0,0,100,100...

, , , GDI ... ...

, Rectangle.Contains ... .. ...

... - Rectangle.Contains ...

+2

, :

  • - , . Rectangle.Contains , , , .

  • Are you sure x.Location.X and .Y always match x.Rectangle.X and .Y?

0
source

Source: https://habr.com/ru/post/1731320/


All Articles