Limits: Boundary inclusion in rect.Intersects and rect.Contains
0 reputation · 26 Feb 2020, 21:15 UTC
0 reputation · 26 Feb 2020, 21:15 UTC
The goal is to determine how the rect library defines containment and intersection when points lie exactly on the rectangle's edges or when two rectangles touch at an edge or corner.
The library's source uses <= comparisons, suggesting edges are included, but the official documentation does not state this explicitly. Existing tests cover interior points but omit boundary cases, creating ambiguity for consumers who rely on strict geometric definitions.
Questions to resolve:
1) Does rect.Contains return true for a point whose coordinates equal the rectangle's minimum or maximum bounds?
2) Does rect.Intersects return true when two rectangles share only an edge or a single corner point?
3) If the behavior differs across releases, what version changes introduced the current logic?
29775 reputation · 26 Feb 2020, 23:51 UTC
rect.Contains returns true for points whose X equals the rectangle’s Left or Y equals Top, but returns false when X equals Right or Y equals Bottom. The right and bottom edges are exclusive.rect.Intersects returns true when two rectangles share any boundary – an edge or a single corner point – because the intersection test uses inclusive comparisons on the left/top and exclusive on the right/bottom, which still counts a shared line or point as an overlap.Rectangle in the System.Drawing namespace. If you are using a different geometry library (e.g., SkiaSharp, Unity, or a custom Rect struct), check that library’s documentation – the rule can differ.X <= point.X < X+Width and Y <= point.Y < Y+Height.X+Width or Y+Height is considered outside.var r1 = new Rectangle(0, 0, 10, 10);
var r2 = new Rectangle(10, 0, 10, 10); // shares right edge of r1
Console.WriteLine(r1.IntersectsWith(r2)); // expected true
Console.WriteLine(r1.Contains(new Point(0, 5))); // true (left edge)
Console.WriteLine(r1.Contains(new Point(10, 5))); // false (right edge)
Console.WriteLine(r1.Contains(new Point(5, 0))); // true (top edge)
Console.WriteLine(r1.Contains(new Point(5, 10))); // false (bottom edge)
var inner = new Rectangle(1, 1, 8, 8); // fits inside r1
Console.WriteLine(r1.Contains(inner)); // true
var edgeTouch = new Rectangle(0, 0, 10, 10); // identical to r1
Console.WriteLine(r1.Contains(edgeTouch)); // true
var overEdge = new Rectangle(0, 0, 11, 10); // extends beyond right
Console.WriteLine(r1.Contains(overEdge)); // false
Contains and Intersects.In the standard .NET Rectangle type, points on the left or top edges are considered inside, while points on the right or bottom edges are outside. Two rectangles that touch only along an edge or at a corner are treated as intersecting. These rules have been consistent across all recent .NET releases. If you rely on strict geometric definitions, always verify the specific library’s implementation before assuming inclusive behavior on all sides.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.