Пример #1
0
        public void BentleyFaustPreparataAlgorithmComputeConvexHullTest()
        {
            // test case 1: convex polygon

            List <Coordinate> shell = new List <Coordinate>
            {
                new Coordinate(0, 0), new Coordinate(10, 0),
                new Coordinate(10, 10), new Coordinate(0, 10),
                new Coordinate(0, 0)
            };

            IList <Coordinate> convexHull = BentleyFaustPreparataAlgorithm.ApproximateConvexHull(shell);

            Assert.AreEqual(shell.Count, convexHull.Count);

            for (Int32 i = 0; i < shell.Count; i++)
            {
                Assert.AreEqual(shell[i], convexHull[i]);
            }


            // test case 2: concave polygon

            shell = new List <Coordinate>
            {
                new Coordinate(0, 0), new Coordinate(2, 1), new Coordinate(8, 1),
                new Coordinate(10, 0), new Coordinate(9, 2), new Coordinate(9, 8),
                new Coordinate(10, 10), new Coordinate(8, 9), new Coordinate(2, 9),
                new Coordinate(0, 10), new Coordinate(1, 8), new Coordinate(1, 2),
                new Coordinate(0, 0)
            };

            List <Coordinate> expected = new List <Coordinate>
            {
                new Coordinate(0, 0), new Coordinate(10, 0),
                new Coordinate(10, 10), new Coordinate(0, 10),
                new Coordinate(0, 0)
            };

            convexHull = BentleyFaustPreparataAlgorithm.ApproximateConvexHull(shell);

            Assert.AreEqual(expected.Count, convexHull.Count);

            for (Int32 i = 0; i < expected.Count; i++)
            {
                Assert.AreEqual(expected[i], convexHull[i]);
            }
        }
Пример #2
0
        public void BentleyFaustPreparataAlgorithmComputeConvexHullTest()
        {
            // convex polygon
            List <Coordinate> shell = new List <Coordinate>
            {
                new Coordinate(0, 0), new Coordinate(10, 0),
                new Coordinate(10, 10), new Coordinate(0, 10),
                new Coordinate(0, 0)
            };

            IReadOnlyList <Coordinate> convexHull = BentleyFaustPreparataAlgorithm.ApproximateConvexHull(shell);

            convexHull.Count.ShouldBe(shell.Count);

            for (Int32 shellIndex = 0; shellIndex < shell.Count; shellIndex++)
            {
                convexHull[shellIndex].ShouldBe(shell[shellIndex]);
            }

            // concave polygon
            shell = new List <Coordinate>
            {
                new Coordinate(0, 0), new Coordinate(2, 1), new Coordinate(8, 1),
                new Coordinate(10, 0), new Coordinate(9, 2), new Coordinate(9, 8),
                new Coordinate(10, 10), new Coordinate(8, 9), new Coordinate(2, 9),
                new Coordinate(0, 10), new Coordinate(1, 8), new Coordinate(1, 2),
                new Coordinate(0, 0)
            };

            List <Coordinate> expected = new List <Coordinate>
            {
                new Coordinate(0, 0), new Coordinate(10, 0),
                new Coordinate(10, 10), new Coordinate(0, 10),
                new Coordinate(0, 0)
            };

            convexHull = BentleyFaustPreparataAlgorithm.ApproximateConvexHull(shell);
            convexHull.Count.ShouldBe(expected.Count);

            for (Int32 index = 0; index < expected.Count; index++)
            {
                convexHull[index].ShouldBe(expected[index]);
            }
        }
        /// <summary>
        /// Computes the convex hull of the specified <see cref="IGeometry" /> instance.
        /// </summary>
        /// <param name="geometry">The geometry.</param>
        /// <returns>The convex hull of the <see cref="IGeometry" /> instance.</returns>
        /// <exception cref="System.ArgumentNullException">The geometry is null.</exception>
        /// <exception cref="System.ArgumentException">The operation is not supported with the specified geometry type.</exception>
        public IGeometry ConvexHull(IGeometry geometry)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry", "The geometry is null.");
            }

            IGeometryFactory factory = _geometryFactory ?? geometry.Factory;

            if (geometry is IPoint)
            {
                return(factory.CreatePoint((geometry as IPoint).Coordinate));
            }
            if (geometry is ICurve)
            {
                return(factory.CreatePolygon(BentleyFaustPreparataAlgorithm.ApproximateConvexHull((geometry as ILineString).Coordinates, geometry.PrecisionModel)));
            }
            if (geometry is IPolygon)
            {
                return(factory.CreatePolygon(BentleyFaustPreparataAlgorithm.ApproximateConvexHull((geometry as IPolygon).Shell.Coordinates, geometry.PrecisionModel)));
            }

            throw new ArgumentException("The operation is not supported with the specified geometry type.");
        }