Possible Duplicate:
Finding the overlapping area of two rectangles (in C#)
I have two areas identified by top left and bottom right corners (fig.1).
In c#, how can I test if they are in contact (fig.2)?
On your Android phone or tablet, open the Contacts app . At the top right, select the Google Account that has the duplicate contacts you want to merge. At the bottom, tap Fix & manage Merge & fix. Tap Merge duplicates.
Duplicate contacts on your iPhone can occur as the result of using iCloud, or due to an issue with your address book or email client on your computer. When using your iPhone for both your business and personal purposes, you may create duplicate contacts by syncing with more than one service.
Let's say you have two Rectangle
s which are r1
and r2
, you can check whether they intersects with each other by this:
if(r1.IntersectsWith(r2))
{
// Intersect
}
If you need the exact area which they intersects with each other, you can do this:
Rectangle intersectArea = Rectangle.Intersect(r1, r2);
You can check the documentation: Rectangle.IntersectsWith
, Rectangle.Intersect
Additional important note:
I've just checked that if the two rectangles just touch each other on an edge, Rectangle.Intersect
returns a rectangle with one dimension is zero , however Rectangle.IntersectsWith
will return false
. So you need to note that.
For example, Rectangle.Intersect
on {X=0,Y=0,Width=10,Height=10}
and {X=10,Y=0,Width=10,Height=10}
will return {X=10,Y=0,Width=0,Height=10}
.
If you hope to get true
also if they just touch each other, change the condition to:
if(Rectangle.Intersect(r1, r2) != Rectangle.Empty)
{
// Intersect or contact (just touch each other)
}
If you don't want to depend on System.Drawing
:
Let's note:
X1
, Y1
, X2
, Y2
: the coordinates of the points of the first rectangle (with X1 < X2
and Y1 < Y2
)X1'
, Y1'
, X2'
, Y2'
: the coordinates of the points of the second rectangle (with X1' < X2'
and Y1' < Y2'
)There is intersection if and only if:
(X2' >= X1 && X1' <= X2) && (Y2' >= Y1 && Y1' <= Y2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With