This topic has not yet been rated - Rate this topic

Rectangle.IntersectsWith Method

Determines if this rectangle intersects with rect.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
public bool IntersectsWith(
	Rectangle rect
)

Parameters

rect
Type: System.Drawing.Rectangle
The rectangle to test.

Return Value

Type: System.Boolean
This 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);
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
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.
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)