public void GrahamScanAlgorithmComputeConvexHullTest() { // convex polygon Coordinate[] shell = new[] { new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(10, 10), new Coordinate(0, 10), new Coordinate(0, 0) }; IReadOnlyList <Coordinate> convexHull = GrahamScanAlgorithm.ComputeConvexHull(shell); convexHull.ShouldBe(shell); // concave polygon shell = new[] { 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) }; Coordinate[] expected = new[] { new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(10, 10), new Coordinate(0, 10), new Coordinate(0, 0) }; convexHull = GrahamScanAlgorithm.ComputeConvexHull(shell); convexHull.ShouldBe(expected); }
public void GrahamScanAlgorithmComputeConvexHullTest() { // 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 = GrahamScanAlgorithm.ComputeConvexHull(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 = GrahamScanAlgorithm.ComputeConvexHull(shell); Assert.AreEqual(expected.Count, convexHull.Count); for (Int32 i = 0; i < expected.Count; i++) { Assert.AreEqual(expected[i], convexHull[i]); } }
/// <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(GrahamScanAlgorithm.ComputeConvexHull((geometry as ILineString).Coordinates, geometry.PrecisionModel))); } if (geometry is IPolygon) { return(factory.CreatePolygon(GrahamScanAlgorithm.ComputeConvexHull((geometry as IPolygon).Shell.Coordinates, geometry.PrecisionModel))); } throw new ArgumentException("The operation is not supported with the specified geometry type."); }