public SortedDictionary<double, double> CalculateReduction(SortedDictionary<double, double> input, double percentage) { if (input.Count <= 2) { return input; } else if (percentage<= 0) { var first = input.First(); var last = input.Last(); return new SortedDictionary<double, double>() { {first.Key, first.Value}, {last.Key,last.Value} }; } var m = new MathFunctions(); var temp = (from n in input select new Point(n.Key, n.Value)).ToList(); var result = temp.ToList(); var points = temp.Count; double factor = 1.5; double calcfactor = Math.Max(1.5, lastfactor / (factor * factor)); var euclistx = new List<double>(); for (int i = 0; i < temp.Count - 1; i++) { euclistx.Add(Math.Sqrt((temp[i].X - temp[i + 1].X) * (temp[i].X - temp[i + 1].X))); } var thresholdx = Convert.ToDouble(euclistx.Average()); var thresh = GetAveragePerpendicularDistance(temp); var xpercentage = 1.0; var difftotal = m.CalculateDifference(temp, new List<Point>() { temp.First(), temp.Last() }); while (xpercentage > percentage) { //var intermediate = m.DouglasPeuckerReduction(temp, calcfactor * thresh); var intermediate = m.DouglasPeuckerReductionNoStack(temp, calcfactor * thresh); calcfactor = calcfactor * factor; lastfactor = calcfactor; var diff = m.CalculateDifference(temp, intermediate); xpercentage = 1 - (diff / difftotal); if (xpercentage > percentage) result = intermediate.ToList(); } var resultdict = new SortedDictionary<double, double>(); foreach ( var n in result) resultdict.Add(n.X,n.Y); return resultdict; }
public SortedDictionary<double, double> CalculateReduction(SortedDictionary<double, double> input, int amountofpoints) { if (input.Count <= 2 ) { return input; } else if (amountofpoints <= 2) { var first = input.First(); var last = input.Last(); return new SortedDictionary<double, double>() { { first.Key, first.Value }, { last.Key, last.Value } }; } var m = new MathFunctions(); var temp = (from n in input select new Point(n.Key, n.Value)).ToList(); var result = temp.ToList(); double factor = 1.5; double calcfactor = Math.Max(1.5, lastfactor / (factor * factor)); var euclistx = new List<double>(); for (int i = 0; i < temp.Count - 1; i++) { euclistx.Add(Math.Sqrt((temp[i].X - temp[i + 1].X) * (temp[i].X - temp[i + 1].X))); } var thresholdx = Convert.ToDouble(euclistx.Average())/100.0; var thresh = GetAveragePerpendicularDistance(temp); var numberofpoints = input.Count; while (numberofpoints >= amountofpoints) { var intermediate = m.DouglasPeuckerReduction(temp, calcfactor * thresholdx); calcfactor = calcfactor * factor; lastfactor = calcfactor; numberofpoints = intermediate.Count; if (numberofpoints >= amountofpoints) result = intermediate.ToList(); } var resultdict = new SortedDictionary<double, double>(); foreach (var n in result) resultdict.Add(n.X, n.Y); return resultdict; }