Skip to content

Commit 5d57f82

Browse files
committed
fix: Rectangle.Intersect
1 parent a68c4a4 commit 5d57f82

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

src/Rectangle.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,21 @@ public static void Intersect(
397397
ref Rectangle value2,
398398
out Rectangle result
399399
) {
400-
int right1 = value1.X + value1.Width; // inline value1.Right
401-
int right2 = value2.X + value2.Width; // inline value2.Right
402-
int bottom1 = value1.Y + value1.Height; // inline value1.Bottom
403-
int bottom2 = value2.Y + value2.Height; // inline value2.Bottom
404-
if (value1.X < right2 && value2.X < right1 && value1.Y < bottom2 && value2.Y < bottom1)
400+
int right1 = value1.X + value1.Width; // inline Rectangle.Right
401+
int right2 = value2.X + value2.Width; // inline Rectangle.Right
402+
int bottom1 = value1.Y + value1.Height; // inline Rectangle.Bottom
403+
int bottom2 = value2.Y + value2.Height; // inline Rectangle.Bottom
404+
405+
int left = value1.X > value2.X ? value1.X : value2.X;
406+
int top = value1.Y > value2.Y ? value1.Y : value2.Y;
407+
int right = right1 < right2 ? right1 : right2;
408+
int bottom = bottom1 < bottom2 ? bottom1 : bottom2;
409+
if (left < right && top < bottom)
405410
{
406-
result.X = Math.Max(value1.X, value2.X);
407-
result.Y = Math.Max(value1.Y, value2.Y);
408-
result.Width = Math.Min(right1, right2) - result.X;
409-
result.Height = Math.Min(bottom1, bottom2) - result.Y;
411+
result.X = left;
412+
result.Y = top;
413+
result.Width = right - left;
414+
result.Height = bottom - top;
410415
}
411416
else
412417
{

0 commit comments

Comments
 (0)