private void AssertRectangleHasTheFollowingValues(Rectangle rect, int x, int y, int width, int height) { Assert.AreEqual(x, rect.X); Assert.AreEqual(y, rect.Y); Assert.AreEqual(height, rect.Height); Assert.AreEqual(width, rect.Width); }
public Rectangle GetIntersection(Rectangle rect) { if (IsPointWithinRectangle(rect.X, rect.Y) || IsPointWithinRectangle(rect.X + rect.Width, rect.Y) || IsPointWithinRectangle(rect.X, rect.Y + rect.Height) || IsPointWithinRectangle(rect.X + rect.Width, rect.Y + rect.Height)) { int x = Math.Max(this.X, rect.X); int y = Math.Max(this.Y, rect.Y); int width = Math.Min( (this.X + this.Width), (rect.X + rect.Width)) - x; int height = Math.Min( (this.Y + this.Height), (rect.Y + rect.Height)) - y; return new Rectangle(x, y, width, height); } else { return null; } }
public void Rectangle_IntersectsTwoPoints_ReturnsCorrectIntersection() { Rectangle compareRect = new Rectangle(8, 11, 10, 3); Rectangle intersection = rect.GetIntersection(compareRect); AssertRectangleHasTheFollowingValues(intersection, 10, 11, 8, 3); }
public void Rectangle_NoIntersection_ReturnsNull() { Rectangle compareRect = new Rectangle(1, 1, 1, 1); Rectangle intersection = rect.GetIntersection(compareRect); Assert.IsNull(intersection); }
public void Rectangle_IntersectsOnePoint_ReturnsCorrectIntersection() { Rectangle compareRect = new Rectangle(17, 17, 20, 20); Rectangle intersection = rect.GetIntersection(compareRect); AssertRectangleHasTheFollowingValues(intersection, 17, 17, 3, 3); }
public void Rectangle_IntersectOnVerticalLine_Should_ReturnWidthZero_SameHeight() { Rectangle compareRect = new Rectangle(0, 10, 10, 10); Rectangle intersection = rect.GetIntersection(compareRect); AssertRectangleHasTheFollowingValues(intersection, 10, 10, 0, 10); }
public void Rectangle_IntersectOnHorizontalLine_ShouldReturnHeightZero_SameWidth() { Rectangle compareRect = new Rectangle(10, 8, 10, 2); Rectangle intersection = rect.GetIntersection(compareRect); AssertRectangleHasTheFollowingValues(intersection, 10, 10, 10, 0); }
public void Rectangle_IntersectionIsCompletelyWithinRectangle_ShouldReturnSmallerRectangle() { Rectangle compareRect = new Rectangle(11, 11, 6, 6); Rectangle intersection = rect.GetIntersection(compareRect); AssertRectangleHasTheFollowingValues(intersection, 11, 11, 6, 6); }
public void Rectangle_IntersectAtSinglePoint_ShouldReturnHeightWidthAsZero() { Rectangle compareRect = new Rectangle(0, 0, 10, 10); Rectangle intersection = rect.GetIntersection(compareRect); AssertRectangleHasTheFollowingValues(intersection, 10, 10, 0, 0); }