Beispiel #1
0
        private static void Main(string[] args)
        {
            LoveRectangle rectangle1       = new LoveRectangle(1, 1, 5, 4);
            LoveRectangle rectangle2       = new LoveRectangle(5, 3, 3, 4);
            RangeOverlap  rangeOverlap     = new RangeOverlap(3, 1);
            LoveRectangle overlapRectangle = rangeOverlap.FindRectangularOverlap(rectangle1, rectangle2);

            Console.WriteLine(overlapRectangle);
        }
Beispiel #2
0
        public void TestLoveRectanglesOverLap()
        {
            // Arrange
            LoveRectangle rectangle1   = new LoveRectangle(1, 1, 5, 4);
            LoveRectangle rectangle2   = new LoveRectangle(5, 3, 3, 4);
            RangeOverlap  rangeOverlap = new RangeOverlap();

            // Act
            LoveRectangle overlapRectangle = rangeOverlap.FindRectangularOverlap(rectangle1, rectangle2);

            // Assert
            Assert.AreEqual(5, overlapRectangle.LeftX, "X Coordinate of Bottom Left Points for over lapped rectangle is wrong.");
            Assert.AreEqual(3, overlapRectangle.BottomY, "Y Coordinate of Bottom Left Points is wrong.");
            Assert.AreEqual(1, overlapRectangle.Width, "Width for over lapped rectangle is wrong");
            Assert.AreEqual(2, overlapRectangle.Height, "Height for over lapped rectangle is wrong.");
            Assert.AreEqual("(5, 3, 1, 2)", overlapRectangle.ToString(), "Display format for over lapped rectangle is wrong.");
        }
Beispiel #3
0
        public static Rectangle FindRectangularOverlap(Rectangle rect1, Rectangle rect2)
        {
            // Calculate the overlap between the two rectangles
            RangeOverlap xOverlap = FindRangeOverlap(rect1.LeftX, rect1.Width, rect2.LeftX, rect2.Width);
            RangeOverlap yOverlap = FindRangeOverlap(rect1.BottomY, rect1.Height, rect2.BottomY, rect2.Height);

            if (xOverlap.Length == 0 || yOverlap.Length == 0)
            {
                return(new Rectangle());
            }

            return(new Rectangle(
                       xOverlap.StartPoint,
                       yOverlap.StartPoint,
                       xOverlap.Length,
                       yOverlap.Length
                       ));
        }
        public static Rectangle FindRectangularOverlap(Rectangle rect1, Rectangle rect2)
        {
            // Get the x and y overlap points and lengths
            RangeOverlap xOverlap = FindRangeOverlap(rect1.LeftX, rect1.Width, rect2.LeftX, rect2.Width);
            RangeOverlap yOverlap = FindRangeOverlap(rect1.BottomY, rect1.Height, rect2.BottomY, rect2.Height);

            // Return null rectangle if there is no overlap
            if (xOverlap.Length == 0 || yOverlap.Length == 0)
            {
                return(new Rectangle());
            }

            return(new Rectangle(
                       xOverlap.StartPoint,
                       yOverlap.StartPoint,
                       xOverlap.Length,
                       yOverlap.Length
                       ));
        }