private static void DouglasPeukerReductionIterator(int firstPnt, int lastPnt, double tolerance, Line myLine) { double maxDistance = 0; int farthestIndex = 0; double distance; for (int i = firstPnt; (i < (lastPnt - 1)); i++) { distance = Point.PerpendicularDistance(myLine.getPoint(firstPnt), myLine.getPoint(lastPnt), myLine.getPoint(i)); if ((distance > maxDistance)) { maxDistance = distance; farthestIndex = i; } } if (((maxDistance > tolerance) & (farthestIndex != 0))) { reducedLineIndices.Add(farthestIndex); LineThinner.DouglasPeukerReductionIterator(firstPnt, farthestIndex, tolerance, myLine); LineThinner.DouglasPeukerReductionIterator(farthestIndex, lastPnt, tolerance, myLine); } }
// methods public static Line DouglasPeukerReduction(Line myLine, double tolerance) { reducedLineIndices = new List <int>(); int firstPnt = 0; int lastPnt = (myLine.getVerticesCount() - 1); Line newLine = new Line(); reducedLineIndices.Add(firstPnt); reducedLineIndices.Add(lastPnt); LineThinner.DouglasPeukerReductionIterator(firstPnt, lastPnt, tolerance, myLine); reducedLineIndices.Sort(); foreach (int index in reducedLineIndices) { Point pointToAdd = myLine.getPoint(index); newLine.AddPoint(pointToAdd); } return(newLine); }