If you create a rectangle with a negative width, then it draws a rectangle that starts at the original (x,y) point and draws to the left instead of to the right.
If you create a rectangle with a negative height, then it draws a rectangle that starts at the original (x,y) point and draws to up instead of down.
Thus if you make a rectangle of with say new Rectangle(5, 4, -1, -2);
then you get a rectangle with the following points: (5,4), (4,4), (5,2), (4,2).
Run the following code for an example:
Rectangle negWidthHeightRect = new Rectangle(5, 4, -1, -2);
Console.WriteLine(negWidthHeightRect.Left);
Console.WriteLine(negWidthHeightRect.Right);
Console.WriteLine(negWidthHeightRect.Bottom);
Console.WriteLine(negWidthHeightRect.Top);
Console.WriteLine(negWidthHeightRect.Width);
Console.WriteLine(negWidthHeightRect.Height);
Console.ReadLine();
The output is as follows:
5
4
2
4
-1
-2
The behavior is somewhat expected given Microsoft's comments on other rectangle-like controls (besides System.Drawing.Rectangle). See MSDM here.
Now, why does the rectangle's IntersectsWith
method produce such weird results. It is because it is using the following code for implementing the IntersectsWith
method:
public bool IntersectsWith(Rectangle rect)
{
return ((((rect.X < (this.X + this.Width)) && (this.X < (rect.X + rect.Width))) && (rect.Y < (this.Y + this.Height))) && (this.Y < (rect.Y + rect.Height)));
}
If you follow through the logic yourself you will see why you get the answers you get. What Microsoft should probably do is either:
1) when it sees a negative width/height make the width/height positive and readjust the Left/Top
2) Or in the IntersectsWith logic, they should do some Min(x, x+width) and Min (y, y+width) when doing the logic.
Some others smarter than me may have an even more clever way to do this.