public void LineAlgorithmsCentroidTest() { // valid lines Assert.AreEqual(new Coordinate(4.5, 6.5), LineAlgorithms.Centroid(new Coordinate(2, 5), new Coordinate(7, 8))); Assert.AreEqual(new Coordinate(5, 51), LineAlgorithms.Centroid(new BasicLineString(new[] { new Coordinate(5, 2), new Coordinate(5, 100) }))); // invalid line Assert.IsFalse(LineAlgorithms.Centroid(new Coordinate(Double.NaN, 5), new Coordinate(7, 8)).IsValid); // valid line string List <Coordinate> lineString = new List <Coordinate> { new Coordinate(1, 1), new Coordinate(1, 3), new Coordinate(1, 7) }; Assert.AreEqual(new Coordinate(1, 4), LineAlgorithms.Centroid(lineString)); // valid lines with fixed precision PrecisionModel precision = new PrecisionModel(0.001); Assert.AreEqual(new Coordinate(22000, 19000), LineAlgorithms.Centroid(new Coordinate(23000, 16000), new Coordinate(20000, 22000), precision)); Assert.AreEqual(new Coordinate(434898000, 461826000), LineAlgorithms.Centroid(new Coordinate(864325000, 96041000), new Coordinate(5470000, 827611000), precision)); // valid lines at given fixed precision Assert.AreEqual(new Coordinate(22000, 19000), LineAlgorithms.Centroid(new Coordinate(23654, 16412), new Coordinate(19530, 22009), precision)); Assert.AreEqual(new Coordinate(434898000, 461826000), LineAlgorithms.Centroid(new Coordinate(864325203, 96041202), new Coordinate(5470432, 827611534), precision)); }
/// <summary> /// Partitions a polygon by triangles. /// </summary> public void Compute() { // source: http://www.codeproject.com/Articles/8238/Polygon-Triangulation-in-C _result = new List <Coordinate[]>(); IList <Coordinate> shell = new List <Coordinate>(_shell); IList <Coordinate> nextShell = new List <Coordinate>(shell); Coordinate[] triangle; Int32 coordinateCount = shell.Count - 1; Int32 coordinateIndex = 1; while (shell.Count != 4) { Coordinate centroid = LineAlgorithms.Centroid(shell[(shell.Count + coordinateIndex - 1) % coordinateCount], shell[(shell.Count + coordinateIndex + 1) % coordinateCount], PrecisionModel); nextShell.Remove(nextShell[coordinateIndex]); if (WindingNumberAlgorithm.IsInsidePolygon(shell, centroid, PrecisionModel) && !ShamosHoeyAlgorithm.Intersects(nextShell, PrecisionModel)) { triangle = new Coordinate[3] { shell[(coordinateCount + coordinateIndex - 1) % coordinateCount], shell[coordinateIndex], shell[(coordinateCount + coordinateIndex + 1) % coordinateCount] }; _result.Add(triangle); shell.Remove(shell[coordinateIndex]); coordinateIndex = 1; coordinateCount--; } else { coordinateIndex = (coordinateIndex + 1) % (shell.Count - 1); nextShell = new List <Coordinate>(shell); } } triangle = new Coordinate[3] { shell[0], shell[1], shell[2] }; _result.Add(triangle); _hasResult = true; }
/// <summary> /// Partitions a polygon by triangles. /// </summary> public void Compute() { this.result = new List <Coordinate[]>(); List <Coordinate> shell = new List <Coordinate>(this.Source); List <Coordinate> nextShell = new List <Coordinate>(this.Source); Coordinate[] triangle; Int32 coordinateCount = shell.Count - 1; Int32 coordinateIndex = 1; while (shell.Count != 4) { Coordinate centroid = LineAlgorithms.Centroid(shell[(shell.Count + coordinateIndex - 1) % coordinateCount], shell[(shell.Count + coordinateIndex + 1) % coordinateCount], this.PrecisionModel); nextShell.Remove(nextShell[coordinateIndex]); if (!WindingNumberAlgorithm.InExterior(shell, centroid, this.PrecisionModel) && !ShamosHoeyAlgorithm.Intersects(nextShell, this.PrecisionModel)) { triangle = new Coordinate[3] { shell[(coordinateCount + coordinateIndex - 1) % coordinateCount], shell[coordinateIndex], shell[(coordinateCount + coordinateIndex + 1) % coordinateCount] }; this.result.Add(triangle); shell.Remove(shell[coordinateIndex]); coordinateIndex = 1; coordinateCount--; } else { coordinateIndex = (coordinateIndex + 1) % (shell.Count - 1); nextShell = new List <Coordinate>(shell); } } triangle = new Coordinate[3] { shell[0], shell[1], shell[2] }; this.result.Add(triangle); this.hasResult = true; }