public void BoudingRectangle_ReturnsBoundingBox()
        {
            {
                var p = GridPolygon.GetRectangle(2, 4);

                var boundingRectangle = p.BoundingRectangle;
                var expected          = new GridRectangle(new IntVector2(0, 0), new IntVector2(2, 4));

                Assert.AreEqual(expected, boundingRectangle);
            }

            {
                var p = new GridPolygonBuilder()
                        .AddPoint(0, 0)
                        .AddPoint(0, 6)
                        .AddPoint(3, 6)
                        .AddPoint(3, 3)
                        .AddPoint(7, 3)
                        .AddPoint(7, 0)
                        .Build();

                var boundingRectangle = p.BoundingRectangle;
                var expected          = new GridRectangle(new IntVector2(0, 0), new IntVector2(7, 6));

                Assert.AreEqual(expected, boundingRectangle);
            }
        }
        public void Constructor_CounterClockwise_Throws()
        {
            {
                // Square
                var p = new GridPolygonBuilder()
                        .AddPoint(3, 0)
                        .AddPoint(3, 3)
                        .AddPoint(0, 3)
                        .AddPoint(0, 0);

                Assert.Throws <ArgumentException>(() => p.Build());
            }

            {
                // L Shape
                var p = new GridPolygonBuilder()
                        .AddPoint(6, 0)
                        .AddPoint(6, 3)
                        .AddPoint(3, 3)
                        .AddPoint(3, 6)
                        .AddPoint(0, 6)
                        .AddPoint(0, 0);

                Assert.Throws <ArgumentException>(() => p.Build());
            }
        }
        public void OverlapAlongLine_ComplexCase()
        {
            var p1 = GetPlusShape();
            var p2 = new GridPolygonBuilder()
                     .AddPoint(0, 0)
                     .AddPoint(0, 8)
                     .AddPoint(8, 8)
                     .AddPoint(8, 2)
                     .AddPoint(6, 2)
                     .AddPoint(6, 6)
                     .AddPoint(2, 6)
                     .AddPoint(2, 0)
                     .Build();

            var line = new OrthogonalLine(new IntVector2(0, -2), new IntVector2(15, -2));

            var result   = polygonOverlap.OverlapAlongLine(p1, p2, line);
            var expected = new List <Tuple <IntVector2, bool> >()
            {
                Tuple.Create(new IntVector2(0, -2), true),
                Tuple.Create(new IntVector2(2, -2), false),
                Tuple.Create(new IntVector2(3, -2), true),
                Tuple.Create(new IntVector2(6, -2), false),
            };

            Assert.IsTrue(expected.SequenceEqual(result));
        }
        public void NormalizePolygon_Polygon_ReturnsNormalizedPolygon()
        {
            var polygon = new GridPolygonBuilder()
                          .AddPoint(0, 5)
                          .AddPoint(5, 5)
                          .AddPoint(5, 0)
                          .AddPoint(2, 0)
                          .AddPoint(2, 3)
                          .AddPoint(0, 3)
                          .Build();

            var normalized     = utils.NormalizePolygon(polygon);
            var squarePoints   = normalized.GetPoints();
            var expectedPoints = new List <IntVector2>()
            {
                new IntVector2(0, 3),
                new IntVector2(0, 5),
                new IntVector2(5, 5),
                new IntVector2(5, 0),
                new IntVector2(2, 0),
                new IntVector2(2, 3),
            };

            Assert.IsTrue(expectedPoints.SequenceEqual(squarePoints));
        }
Beispiel #5
0
        public void GetPolygons_PlusShape()
        {
            var polygon = new GridPolygonBuilder()
                          .AddPoint(0, 2)
                          .AddPoint(0, 4)
                          .AddPoint(2, 4)
                          .AddPoint(2, 6)
                          .AddPoint(4, 6)
                          .AddPoint(4, 4)
                          .AddPoint(6, 4)
                          .AddPoint(6, 2)
                          .AddPoint(4, 2)
                          .AddPoint(4, 0)
                          .AddPoint(2, 0)
                          .AddPoint(2, 2)
                          .Build();

            var partitions = partitioner.GetPartitions(polygon);
            var expected   = new List <List <GridRectangle> >()
            {
                new List <GridRectangle>()
                {
                    new GridRectangle(new IntVector2(2, 0), new IntVector2(4, 2)),
                    new GridRectangle(new IntVector2(0, 2), new IntVector2(6, 4)),
                    new GridRectangle(new IntVector2(2, 4), new IntVector2(4, 6)),
                },
                new List <GridRectangle>()
                {
                    new GridRectangle(new IntVector2(0, 2), new IntVector2(2, 4)),
                    new GridRectangle(new IntVector2(2, 0), new IntVector2(4, 6)),
                    new GridRectangle(new IntVector2(4, 2), new IntVector2(6, 4)),
                }
            };

            var matched = false;

            foreach (var rectangles in expected)
            {
                foreach (var r in rectangles)
                {
                    if (!partitions.Contains(r))
                    {
                        break;
                    }
                }

                matched = true;
            }

            Assert.AreEqual(expected[0].Count, partitions.Count);
            Assert.AreEqual(true, matched);

            foreach (var p in polygon.GetAllRotations().Select(x => utils.NormalizePolygon(x)))
            {
                var rotated = partitioner.GetPartitions(p);
                Assert.AreEqual(expected[0].Count, rotated.Count);
            }
        }
        public void Constructor_EdgesNotOrthogonal_Trows()
        {
            var polygon1 = new GridPolygonBuilder()
                           .AddPoint(0, 0)
                           .AddPoint(0, 5)
                           .AddPoint(3, 4)
                           .AddPoint(4, 0);

            Assert.Throws <ArgumentException>(() => polygon1.Build());
        }
        public void Constructor_OverlappingEdges_Throws()
        {
            var polygon1 = new GridPolygonBuilder()
                           .AddPoint(0, 0)
                           .AddPoint(3, 0)
                           .AddPoint(3, 2)
                           .AddPoint(0, 2)
                           .AddPoint(0, 4);

            Assert.Throws <ArgumentException>(() => polygon1.Build());
        }
        public void Constructor_TooFewPoints_Throws()
        {
            var polygon1 = new GridPolygonBuilder()
                           .AddPoint(0, 0)
                           .AddPoint(0, 5);

            var polygon2 = new GridPolygonBuilder()
                           .AddPoint(0, 0)
                           .AddPoint(0, 5)
                           .AddPoint(5, 5);

            Assert.Throws <ArgumentException>(() => polygon1.Build());
            Assert.Throws <ArgumentException>(() => polygon2.Build());
        }
        public void Constructor_ValidPolygons()
        {
            var square = new GridPolygonBuilder()
                         .AddPoint(0, 0)
                         .AddPoint(0, 3)
                         .AddPoint(3, 3)
                         .AddPoint(3, 0);

            var lPolygon = new GridPolygonBuilder()
                           .AddPoint(0, 0)
                           .AddPoint(0, 6)
                           .AddPoint(3, 6)
                           .AddPoint(3, 3)
                           .AddPoint(6, 3)
                           .AddPoint(6, 0);

            Assert.DoesNotThrow(() => square.Build());
            Assert.DoesNotThrow(() => lPolygon.Build());
        }
        public void Equals_WhenNotEqual_ReturnsFalse()
        {
            {
                var p1 = new GridPolygonBuilder()
                         .AddPoint(0, 0)
                         .AddPoint(0, 3)
                         .AddPoint(3, 3)
                         .AddPoint(3, 0)
                         .Build();

                var p2 = new GridPolygonBuilder()
                         .AddPoint(3, 0)
                         .AddPoint(0, 0)
                         .AddPoint(0, 3)
                         .AddPoint(3, 3)
                         .Build();

                Assert.IsFalse(p1.Equals(p2));
            }
        }
        /// <summary>
        /// Helper method to create a rectangle with given sides.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static GridPolygon GetRectangle(int width, int height)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(width), "Both a and b must be greater than 0");
            }

            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(height), "Both a and b must be greater than 0");
            }

            var polygon = new GridPolygonBuilder()
                          .AddPoint(0, 0)
                          .AddPoint(0, height)
                          .AddPoint(width, height)
                          .AddPoint(width, 0);

            return(polygon.Build());
        }
        public void OverlapAlongLine_LAndL2()
        {
            var p1 = GetLShape();
            var p2 = new GridPolygonBuilder()
                     .AddPoint(0, 0)
                     .AddPoint(0, 9)
                     .AddPoint(3, 9)
                     .AddPoint(3, 3)
                     .AddPoint(6, 3)
                     .AddPoint(6, 0)
                     .Build();
            var line = new OrthogonalLine(new IntVector2(3, 8), new IntVector2(3, -2));

            var result   = polygonOverlap.OverlapAlongLine(p1, p2, line);
            var expected = new List <Tuple <IntVector2, bool> >()
            {
                Tuple.Create(new IntVector2(3, 2), true),
            };

            Assert.IsTrue(expected.SequenceEqual(result));
        }
        public void OverlapAlongLine_SquareAndL3()
        {
            var p1 = GridPolygon.GetSquare(6);
            var p2 = new GridPolygonBuilder()
                     .AddPoint(0, 0)
                     .AddPoint(0, 6)
                     .AddPoint(6, 6)
                     .AddPoint(6, 3)
                     .AddPoint(3, 3)
                     .AddPoint(3, 0)
                     .Build();
            var line = new OrthogonalLine(new IntVector2(3, 2), new IntVector2(3, -5));

            var result   = polygonOverlap.OverlapAlongLine(p1, p2, line);
            var expected = new List <Tuple <IntVector2, bool> >()
            {
                Tuple.Create(new IntVector2(3, 2), true),
                Tuple.Create(new IntVector2(3, -3), false),
            };

            Assert.IsTrue(expected.SequenceEqual(result));
        }