Rectangle.IntersectsWith Method
.NET Framework 4
Determines if this rectangle intersects with rect.
Assembly: System.Drawing (in System.Drawing.dll)
Parameters
- rect
- Type: System.Drawing.Rectangle
The rectangle to test.
Return Value
Type: System.BooleanThis method returns true if there is any intersection, otherwise false.
The following code example demonstrates the Intersect, IsEmpty and the IntersectsWith members. This example should be used with a Windows Form. Paste this code into a form and call this method when handling the form's Paint event, passing e as PaintEventArgs.
private void InstanceRectangleIntersection(PaintEventArgs e) { Rectangle rectangle1 = new Rectangle(50, 50, 200, 100); Rectangle rectangle2 = new Rectangle(70, 20, 100, 200); e.Graphics.DrawRectangle(Pens.Black, rectangle1); e.Graphics.DrawRectangle(Pens.Red, rectangle2); if (rectangle1.IntersectsWith(rectangle2)) { rectangle1.Intersect(rectangle2); if (!rectangle1.IsEmpty) { e.Graphics.FillRectangle(Brushes.Green, rectangle1); } } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Something's amiss with this method...
... or with my understanding of it.
I create two Rectangles from two PictureBoxes. I use each PictureBox's Top, Left, Width, Height properties to create the Rectangles.
Rect1 has the following values: Top=12, Left=12, Bottom=179, Right=112.
Rect2 has the following values: Top=151, Left=80, Bottom=318, Right=180.
Therefore...
Rect1's Bottom=179 > Rect2's Top=151, i.e., the bottom of Rect1 is below the top of Rect2.
AND
Rect1's Right=112 > Rect2's Left = 80, i.e., the left of Rect2 is placed to the left of the right of Rect1.
So, we have an intersection, defined by the Reactangle with the values: Top=151, Left=80, Bottom=179, Right=112.
And yet, Rect1.IntersectsWith(Rect2) returns false.
Am I making some obvious mistake here? If not, a clarification would be most welcome.
Edit: Obvious mistake on my part, naturally. The Top and Left were being swapped in the Rectangle creation. Once corrected, it's all working as expected.
Right, move along, people, nothing to see here.
I create two Rectangles from two PictureBoxes. I use each PictureBox's Top, Left, Width, Height properties to create the Rectangles.
Rect1 has the following values: Top=12, Left=12, Bottom=179, Right=112.
Rect2 has the following values: Top=151, Left=80, Bottom=318, Right=180.
Therefore...
Rect1's Bottom=179 > Rect2's Top=151, i.e., the bottom of Rect1 is below the top of Rect2.
AND
Rect1's Right=112 > Rect2's Left = 80, i.e., the left of Rect2 is placed to the left of the right of Rect1.
So, we have an intersection, defined by the Reactangle with the values: Top=151, Left=80, Bottom=179, Right=112.
And yet, Rect1.IntersectsWith(Rect2) returns false.
Am I making some obvious mistake here? If not, a clarification would be most welcome.
Edit: Obvious mistake on my part, naturally. The Top and Left were being swapped in the Rectangle creation. Once corrected, it's all working as expected.
Right, move along, people, nothing to see here.
- 8/5/2011
- Another Nail
- 8/6/2011
- Another Nail
beware of an implementation bug
The way MS implemented this method does not detect intersections if they occur on the edge of the rectangle. for example the following intersection test returns false: rectangle (0,0)-(10,10) does not intersect with rectangle (0,0)-(0,0)
- 10/20/2010
- Dani Avni
- 2/13/2011
- Thomas Lee