private static void DouglasPeuckerReduction(List <Point> points, Int32 firstPoint, Int32 lastPoint, Double tolerance, ref List <Int32> pointIndexsToKeep) { Double maxDistance = 0; Int32 indexFarthest = 0; for (Int32 index = firstPoint; index < lastPoint; index++) { Double distance = CustomMath.PerpendicularDistance(points [firstPoint], points [lastPoint], points [index]); if (distance > maxDistance) { maxDistance = distance; indexFarthest = index; } } if (maxDistance > tolerance && indexFarthest != 0) { //Add the largest point that exceeds the tolerance pointIndexsToKeep.Add(indexFarthest); CustomMath.DouglasPeuckerReduction(points, firstPoint, indexFarthest, tolerance, ref pointIndexsToKeep); CustomMath.DouglasPeuckerReduction(points, indexFarthest, lastPoint, tolerance, ref pointIndexsToKeep); } }
public static List <Point> DouglasPeuckerReduction(List <Point> points, Double tolerance) { if (points == null || points.Count < 3) { return(points); } Int32 firstPoint = 0; Int32 lastPoint = points.Count - 1; List <Int32> pointIndexsToKeep = new List <Int32> ( ); //Add the first and last index to the keepers pointIndexsToKeep.Add(firstPoint); pointIndexsToKeep.Add(lastPoint); //The first and the last point can not be the same while (points [firstPoint].Equals(points [lastPoint])) { lastPoint--; } CustomMath.DouglasPeuckerReduction(points, firstPoint, lastPoint, tolerance, ref pointIndexsToKeep); List <Point> returnPoints = new List <Point> ( ); pointIndexsToKeep.Sort( ); foreach (Int32 index in pointIndexsToKeep) { returnPoints.Add(points [index]); } return(returnPoints); }