private static void Test( IReadOnlyList <Rectangle> initialRectangles, Rectangle boundingRectangle, Rectangle targetRectangle) { var expectedRectangles = new List <Rectangle>(); var otherRectangles = new List <Rectangle>(); var tree = new RectangleRangeTree(); foreach (var initialRectangle in initialRectangles) { Assert.True(tree.Insert(initialRectangle)); if (targetRectangle.Contains(initialRectangle.TopLeft) || targetRectangle.Contains(initialRectangle.BottomLeft) || targetRectangle.Contains(initialRectangle.TopRight) || targetRectangle.Contains(initialRectangle.BottomRight)) { expectedRectangles.Add(initialRectangle); } else { otherRectangles.Add(initialRectangle); } } TestAssert.Equal(expectedRectangles, tree.GetOverlappingCorners(targetRectangle), $"Target: {targetRectangle}"); foreach (var rectangle in otherRectangles) { Assert.True(tree.Remove(rectangle)); } TestAssert.Equal(expectedRectangles, tree.GetOverlappingCorners(targetRectangle), $"Target: {targetRectangle}"); foreach (var rectangle in expectedRectangles) { Assert.True(tree.Remove(rectangle)); } Assert.Empty(tree.GetOverlappingCorners(targetRectangle)); tree.InsertRange(expectedRectangles); tree.InsertRange(otherRectangles); TestAssert.Equal(expectedRectangles, tree.GetOverlappingCorners(targetRectangle), $"Target: {targetRectangle}"); }
private static void Test( IReadOnlyList <Rectangle> initialRectangles, Rectangle boundingRectangle, Rectangle targetRectangle) { var expectedRectangles = new List <Rectangle>(); var otherRectangles = new List <Rectangle>(); var tree = new YBoundedIntervalTree(boundingRectangle.XProjection); foreach (var initialRectangle in initialRectangles) { Assert.True(tree.Insert(initialRectangle)); if (initialRectangle.XProjection.Encloses(targetRectangle.XProjection) && (targetRectangle.YProjection.Contains(initialRectangle.YProjection.Beginning) || targetRectangle.YProjection.Contains(initialRectangle.YProjection.End))) { expectedRectangles.Add(initialRectangle); } else { otherRectangles.Add(initialRectangle); } } TestAssert.Equal(expectedRectangles, tree.GetEnclosing(targetRectangle), $"Target: {targetRectangle}"); foreach (var rectangle in otherRectangles) { Assert.True(tree.Remove(rectangle)); } TestAssert.Equal(expectedRectangles, tree.GetEnclosing(targetRectangle), $"Target: {targetRectangle}"); foreach (var rectangle in expectedRectangles) { Assert.True(tree.Remove(rectangle)); } Assert.Empty(tree.GetEnclosing(targetRectangle)); tree.InsertRange(expectedRectangles); tree.InsertRange(otherRectangles); TestAssert.Equal(expectedRectangles, tree.GetEnclosing(targetRectangle), $"Target: {targetRectangle}"); }
private static void Test(IReadOnlyList <Rectangle> initialRectangles, Rectangle boundingRectangle, Rectangle targetRectangle) { var expectedRectangles = new List <Rectangle>(); var otherRectangles = new List <Rectangle>(); var tree = new RectangleIntervalTree(boundingRectangle); foreach (var initialRectangle in initialRectangles) { Assert.True(tree.Insert(initialRectangle)); if (initialRectangle.Overlaps(targetRectangle)) { expectedRectangles.Add(initialRectangle); } else { otherRectangles.Add(initialRectangle); } } TestAssert.Equal(expectedRectangles, tree.GetOverlapping(targetRectangle), $"Target: {targetRectangle}"); foreach (var rectangle in otherRectangles) { Assert.True(tree.Remove(rectangle)); } TestAssert.Equal(expectedRectangles, tree.GetOverlapping(targetRectangle), $"Target: {targetRectangle}"); foreach (var rectangle in expectedRectangles) { Assert.True(tree.Remove(rectangle)); } Assert.Empty(tree.GetOverlapping(targetRectangle)); tree.InsertRange(expectedRectangles); tree.InsertRange(otherRectangles); TestAssert.Equal(expectedRectangles, tree.GetOverlapping(targetRectangle), $"Target: {targetRectangle}"); }
private void BuildRoomTest(string map) { var game = TestHelper.CreateGame((uint)Environment.TickCount); var fragment = new DefiningMapFragment { Map = map, Layout = new EmptyLayout { Coverage = 0 }, CreatureGenerator = new CreatureGenerator { ExpectedInitialCount = 0 }, ItemGenerator = new ItemGenerator { ExpectedInitialCount = 0 } }; fragment.EnsureInitialized(game); var byteMap = fragment.ByteMap; fragment.ByteMap = new byte[0]; var(level, rooms) = TestHelper.CreateLevel(fragment, game); var input = new List <Point>(); var expectedInside = new List <Point>(); var expectedPerimeter = new List <Point>(); var expectedOutside = new List <Point>(); fragment.ByteMap = byteMap; fragment.WriteMap(new Point(0, 0), level, (c, point, _, s) => { if (c != ' ') { input.Add(point); } switch (c) { case 'I': s.expectedInside.Add(point); break; case 'P': s.expectedPerimeter.Add(point); break; case 'O': s.expectedOutside.Add(point); break; } }, (expectedInside, expectedPerimeter, expectedOutside)); var inside = new List <Point>(); var perimeter = new List <Point>(); var outside = new List <Point>(); var room = fragment.BuildRoom(level, input, inside.Add, perimeter.Add, outside.Add); try { Assert.Equal(fragment.Width, room.BoundingRectangle.Width); Assert.Equal(fragment.Height, room.BoundingRectangle.Height); TestAssert.Equal(expectedInside, inside, ""); TestAssert.Equal(expectedPerimeter, perimeter.Distinct(), ""); TestAssert.Equal(expectedOutside, outside, ""); Point?firstPoint = null; Point?lastPoint = null; foreach (var point in perimeter) { if (firstPoint == null) { firstPoint = point; lastPoint = point; } else { Assert.Equal(1, lastPoint.Value.OrthogonalDistanceTo(point)); lastPoint = point; } } if (perimeter.Count > 1) { Assert.Equal(1, firstPoint.Value.OrthogonalDistanceTo(lastPoint.Value)); } } catch (Exception e) { throw new InvalidOperationException("Seed: " + level.Game.InitialSeed, e); } }