Exemplo n.º 1
0
        public void RandomPolygonGeneratorCreateRandomPolygonTest()
        {
            // check properties of generated polygons

            Int32      coordinateNumber = 10;
            Coordinate minCoordinate    = new Coordinate(10, 10);
            Coordinate maxCoordinate    = new Coordinate(20, 20);
            Double     convexityRatio   = 0.1;

            for (Int32 polygonNumber = 1; polygonNumber < 100; polygonNumber++)
            {
                IBasicPolygon randomPolygon = RandomPolygonGenerator.CreateRandomPolygon(coordinateNumber * polygonNumber, minCoordinate, maxCoordinate);

                Assert.AreEqual(coordinateNumber * polygonNumber + 1, randomPolygon.Shell.Count);            // number of coordinates
                Assert.AreEqual(Orientation.CounterClockwise, PolygonAlgorithms.Orientation(randomPolygon)); // orientation

                foreach (Coordinate coordinate in randomPolygon.Shell)
                {
                    Assert.True(coordinate.X > minCoordinate.X && coordinate.X <maxCoordinate.X &&
                                                                                coordinate.Y> minCoordinate.Y && coordinate.Y < maxCoordinate.Y); // all coordinates are located in the rectangle
                }

                Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(randomPolygon.Shell)); // no intersection
            }

            // check convexity

            for (Int32 polygonNumber = 1; polygonNumber < 100; polygonNumber++)
            {
                Assert.IsTrue(PolygonAlgorithms.IsConvex(RandomPolygonGenerator.CreateRandomPolygon(coordinateNumber * polygonNumber, minCoordinate, maxCoordinate, 1)));
            }

            for (Int32 polygonNumber = 1; polygonNumber < 100; polygonNumber++)
            {
                Assert.IsFalse(PolygonAlgorithms.IsConvex(RandomPolygonGenerator.CreateRandomPolygon(coordinateNumber * polygonNumber, minCoordinate, maxCoordinate, 0)));
            }

            // check exceptions

            Assert.Throws <ArgumentOutOfRangeException>(() => RandomPolygonGenerator.CreateRandomPolygon(1, minCoordinate, maxCoordinate, convexityRatio));
            Assert.Throws <ArgumentOutOfRangeException>(() => RandomPolygonGenerator.CreateRandomPolygon(coordinateNumber, maxCoordinate, minCoordinate, convexityRatio));
            Assert.Throws <ArgumentOutOfRangeException>(() => RandomPolygonGenerator.CreateRandomPolygon(coordinateNumber, minCoordinate, maxCoordinate, -1));
        }
        public void ShamosHoeyAlgorithmIntersectsTest()
        {
            // simple polygon, intersection

            IList <Coordinate> coordinates = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(1, 0), new Coordinate(0, 1) };

            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinates));


            // simple polygon, no intersection

            coordinates = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 0), new Coordinate(3, 1) };
            Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(coordinates));


            // simple line, no intersection

            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(n, n)).ToArray();
            Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(coordinates));


            // line string, no intersection

            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(n, n % 2)).ToArray();
            Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(coordinates));


            // line string, no intersection

            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(n % 2, n)).ToArray();
            Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(coordinates));


            // random line string, no intersection

            Random random = new Random();

            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(random.Next(1000), n)).ToArray();
            Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(coordinates));


            // random line string, no intersection

            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(n, random.Next(1000))).ToArray();
            Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(coordinates));


            // random line string, intersection

            coordinates = Enumerable.Range(0, 10000).Select(n => new Coordinate(random.Next(100), random.Next(100))).ToArray();
            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinates));


            // multiple line strings, intersection

            IList <Coordinate[]> coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1) },
                new Coordinate[] { new Coordinate(0, 1), new Coordinate(1, 0) }
            };

            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinateLists));


            // multiple line strings, intersection

            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2) },
                new Coordinate[] { new Coordinate(2, 0), new Coordinate(0, 2) }
            };
            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinateLists));


            // multiple line strings, intersection

            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(0, 1), new Coordinate(1, 0) }
            };
            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinateLists));


            // multiple line strings, intersection

            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(1, 2), new Coordinate(2, 1) }
            };
            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinateLists));


            // multiple line strings, intersection

            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(2, 3), new Coordinate(3, 2) }
            };
            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinateLists));


            // multiple line strings, no intersection

            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(0, -1), new Coordinate(-1, 0) },
                new Coordinate[] { new Coordinate(-1, -2), new Coordinate(-2, -1) },
                new Coordinate[] { new Coordinate(-2, -3), new Coordinate(-3, -2) }
            };
            Assert.IsFalse(ShamosHoeyAlgorithm.Intersects(coordinateLists));


            // multiple line strings, intersection

            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(0, -1), new Coordinate(-1, 0) },
                new Coordinate[] { new Coordinate(-1, -2), new Coordinate(-2, -1) },
                new Coordinate[] { new Coordinate(-2, -3), new Coordinate(-3, -2) },
                new Coordinate[] { new Coordinate(1, 2), new Coordinate(2, 1) }
            };
            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinateLists));


            // multiple line strings, intersection

            coordinateLists = new List <Coordinate[]>
            {
                Enumerable.Range(1, 1000).Select(n => new Coordinate(n, n)).ToArray(),
                Enumerable.Range(1, 1000).Select(n => new Coordinate(1000 - n, n)).ToArray()
            };
            Assert.IsTrue(ShamosHoeyAlgorithm.Intersects(coordinateLists));
        }
Exemplo n.º 3
0
        public void ShamosHoeyAlgorithmIntersectsTest()
        {
            // simple polygon, intersection
            Coordinate[] coordinates = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(1, 0), new Coordinate(0, 1) };
            ShamosHoeyAlgorithm.Intersects(coordinates).ShouldBeTrue();

            // simple polygon, no intersection
            coordinates = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 0), new Coordinate(3, 1) };
            ShamosHoeyAlgorithm.Intersects(coordinates).ShouldBeFalse();

            // simple line, no intersection
            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(n, n)).ToArray();
            ShamosHoeyAlgorithm.Intersects(coordinates).ShouldBeFalse();

            // line string, no intersection
            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(n, n % 2)).ToArray();
            ShamosHoeyAlgorithm.Intersects(coordinates).ShouldBeFalse();

            // line string, no intersection
            coordinates = Enumerable.Range(0, 1000).Select(n => new Coordinate(n % 2, n)).ToArray();
            ShamosHoeyAlgorithm.Intersects(coordinates).ShouldBeFalse();

            // multiple line strings, intersection
            List <Coordinate[]> coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1) },
                new Coordinate[] { new Coordinate(0, 1), new Coordinate(1, 0) }
            };

            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeTrue();

            // multiple line strings, intersection
            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2) },
                new Coordinate[] { new Coordinate(2, 0), new Coordinate(0, 2) }
            };
            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeTrue();

            // multiple line strings, intersection
            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(0, 1), new Coordinate(1, 0) }
            };
            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeTrue();

            // multiple line strings, intersection
            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(1, 2), new Coordinate(2, 1) }
            };
            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeTrue();

            // multiple line strings, intersection
            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(2, 3), new Coordinate(3, 2) }
            };
            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeTrue();

            // multiple line strings, no intersection
            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(0, -1), new Coordinate(-1, 0) },
                new Coordinate[] { new Coordinate(-1, -2), new Coordinate(-2, -1) },
                new Coordinate[] { new Coordinate(-2, -3), new Coordinate(-3, -2) }
            };
            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeFalse();

            // multiple line strings, intersection
            coordinateLists = new List <Coordinate[]>
            {
                new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(3, 3) },
                new Coordinate[] { new Coordinate(0, -1), new Coordinate(-1, 0) },
                new Coordinate[] { new Coordinate(-1, -2), new Coordinate(-2, -1) },
                new Coordinate[] { new Coordinate(-2, -3), new Coordinate(-3, -2) },
                new Coordinate[] { new Coordinate(1, 2), new Coordinate(2, 1) }
            };
            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeTrue();

            // multiple line strings, intersection
            coordinateLists = new List <Coordinate[]>
            {
                Enumerable.Range(1, 1000).Select(n => new Coordinate(n, n)).ToArray(),
                Enumerable.Range(1, 1000).Select(n => new Coordinate(1000 - n, n)).ToArray()
            };
            ShamosHoeyAlgorithm.Intersects(coordinateLists).ShouldBeTrue();
        }