public static void simplifyDPStep(NFP points, int first, int last, double?sqTolerance, NFP simplified) { var maxSqDist = sqTolerance; var index = -1; var marked = false; for (var i = first + 1; i < last; i++) { var sqDist = getSqSegDist(points[i], points[first], points[last]); if (sqDist > maxSqDist) { index = i; maxSqDist = sqDist; } } if (maxSqDist > sqTolerance || marked) { if (index - first > 1) { simplifyDPStep(points, first, index, sqTolerance, simplified); } simplified.push(points[index]); if (last - index > 1) { simplifyDPStep(points, index, last, sqTolerance, simplified); } } }
// simplification using Ramer-Douglas-Peucker algorithm public static NFP simplifyDouglasPeucker(NFP points, double?sqTolerance) { var last = points.Length - 1; var simplified = new NFP(); simplified.AddPoint(points[0]); simplifyDPStep(points, 0, last, sqTolerance, simplified); simplified.push(points[last]); return(simplified); }