public static double[][] SimplifyPolygon(double[][] points, double epsilon) { if (points[0].Length <= 2) { return(points); } return(SimplifyCurve.SimplifyBetween(points, epsilon, 0, points[0].Length - 1)); }
/// <summary> /// Simplify the specified points using epsilon. /// </summary> /// <param name="points">Points.</param> /// <param name="epsilon">Epsilon.</param> /// <param name="first">First.</param> /// <param name="last">Last.</param> public static PointF2D[] SimplifyBetween(PointF2D[] points, double epsilon, int first, int last) { if (points == null) { throw new ArgumentNullException("points"); } if (epsilon <= 0) { throw new ArgumentOutOfRangeException("epsilon"); } if (first > last) { throw new ArgumentException(string.Format("first[{0}] must be smaller or equal than last[{1}]!", first, last)); } if (first + 1 != last) { // find point with the maximum distance. double maxDistance = 0; int foundIndex = -1; // create the line between first-last. LineF2D line = new LineF2D(points[first], points [last]); for (int idx = first + 1; idx < last; idx++) { double distance = line.Distance(points[idx]); if (distance > maxDistance) { // larger distance found. maxDistance = distance; foundIndex = idx; } } if (foundIndex > 0 && maxDistance > epsilon) // a point was found and it is far enough. { PointF2D[] before = SimplifyCurve.SimplifyBetween(points, epsilon, first, foundIndex); PointF2D[] after = SimplifyCurve.SimplifyBetween(points, epsilon, foundIndex, last); // build result. PointF2D[] result = new PointF2D[before.Length + after.Length - 1]; for (int idx = 0; idx < before.Length - 1; idx++) { result [idx] = before [idx]; } for (int idx = 0; idx < after.Length; idx++) { result [idx + before.Length - 1] = after [idx]; } return(result); } } return(new PointF2D[] { points[first], points[last] }); }
public static PointF2D[] SimplifyBetween(PointF2D[] points, double epsilon, int first, int last) { if (points == null) { throw new ArgumentNullException("points"); } if (epsilon <= 0.0) { throw new ArgumentOutOfRangeException("epsilon"); } if (first > last) { throw new ArgumentException(string.Format("first[{0}] must be smaller or equal than last[{1}]!", new object[2] { (object)first, (object)last })); } if (first + 1 != last) { double num1 = 0.0; int num2 = -1; LineF2D lineF2D = new LineF2D(points[first], points[last]); for (int index = first + 1; index < last; ++index) { double num3 = lineF2D.Distance(points[index]); if (num3 > num1) { num1 = num3; num2 = index; } } if (num2 > 0 && num1 > epsilon) { PointF2D[] pointF2DArray1 = SimplifyCurve.SimplifyBetween(points, epsilon, first, num2); PointF2D[] pointF2DArray2 = SimplifyCurve.SimplifyBetween(points, epsilon, num2, last); PointF2D[] pointF2DArray3 = new PointF2D[pointF2DArray1.Length + pointF2DArray2.Length - 1]; for (int index = 0; index < pointF2DArray1.Length - 1; ++index) { pointF2DArray3[index] = pointF2DArray1[index]; } for (int index = 0; index < pointF2DArray2.Length; ++index) { pointF2DArray3[index + pointF2DArray1.Length - 1] = pointF2DArray2[index]; } return(pointF2DArray3); } } return(new PointF2D[2] { points[first], points[last] }); }
public static double[][] SimplifyBetween(double[][] points, double epsilon, int first, int last) { if (points == null) { throw new ArgumentNullException("points"); } if (points.Length != 2) { throw new ArgumentException(); } if (epsilon < 0.0) { throw new ArgumentOutOfRangeException("epsilon"); } if (first > last) { throw new ArgumentException(string.Format("first[{0}] must be smaller or equal than last[{1}]!", new object[2] { (object)first, (object)last })); } if (epsilon == 0.0) { return(points); } if (first == last) { return new double[2][] { new double[1] { points[0][first] }, new double[1] { points[1][first] } } } ; if (points[0][first] == points[0][last] && points[1][first] == points[1][last]) { double[][] numArray1 = SimplifyCurve.SimplifyBetween(points, epsilon, first, last - 1); double[][] numArray2 = new double[2][] { new double[numArray1[0].Length + 1], new double[numArray1[0].Length + 1] }; for (int index = 0; index < numArray1[0].Length; ++index) { numArray2[0][index] = numArray1[0][index]; numArray2[1][index] = numArray1[1][index]; } numArray2[0][numArray1[0].Length] = points[0][last]; numArray2[1][numArray1[0].Length] = points[1][last]; return(numArray2); } if (first + 1 != last) { double num1 = 0.0; int num2 = -1; LineF2D lineF2D = new LineF2D(new PointF2D(points[0][first], points[1][first]), new PointF2D(points[0][last], points[1][last])); for (int index = first + 1; index < last; ++index) { double num3 = lineF2D.Distance(new PointF2D(points[0][index], points[1][index])); if (num3 > num1) { num1 = num3; num2 = index; } } if (num2 > 0 && num1 > epsilon) { double[][] numArray1 = SimplifyCurve.SimplifyBetween(points, epsilon, first, num2); double[][] numArray2 = SimplifyCurve.SimplifyBetween(points, epsilon, num2, last); double[][] numArray3 = new double[2][] { new double[numArray1[0].Length + numArray2[0].Length - 1], new double[numArray1[0].Length + numArray2[0].Length - 1] }; for (int index = 0; index < numArray1[0].Length - 1; ++index) { numArray3[0][index] = numArray1[0][index]; numArray3[1][index] = numArray1[1][index]; } for (int index = 0; index < numArray2[0].Length; ++index) { numArray3[0][index + numArray1[0].Length - 1] = numArray2[0][index]; numArray3[1][index + numArray1[0].Length - 1] = numArray2[1][index]; } return(numArray3); } } return(new double[2][] { new double[2] { points[0][first], points[0][last] }, new double[2] { points[1][first], points[1][last] } }); }
public static PointF2D[] Simplify(PointF2D[] points, double epsilon) { return(SimplifyCurve.SimplifyBetween(points, epsilon, 0, points.Length - 1)); }
/// <summary> /// Simplify the specified points using epsilon. /// </summary> /// <param name="points">Points.</param> /// <param name="epsilon">Epsilon.</param> /// <param name="first">First.</param> /// <param name="last">Last.</param> public static double[][] SimplifyBetween(double[][] points, double epsilon, int first, int last) { if (points == null) { throw new ArgumentNullException("points"); } if (points.Length != 2) { throw new ArgumentException(); } if (epsilon < 0) { throw new ArgumentOutOfRangeException("epsilon"); } if (first > last) { throw new ArgumentException(string.Format("first[{0}] must be smaller or equal than last[{1}]!", first, last)); } if (epsilon == 0) { // no simplification is possible. return(points); } if (first == last) { // first and last are equal, no simplification possible. return(new double[][] { new double[] { points[0][first] }, new double[] { points[1][first] } }); } double[][] result; // check for identical first and last points. if (points[0][first] == points[0][last] && points[1][first] == points[1][last]) { // first and last point are indentical. double[][] before = SimplifyCurve.SimplifyBetween(points, epsilon, first, last - 1); // build result. result = new double[2][]; result[0] = new double[before[0].Length + 1]; result[1] = new double[before[0].Length + 1]; for (int idx = 0; idx < before[0].Length; idx++) { result[0][idx] = before[0][idx]; result[1][idx] = before[1][idx]; } result[0][before[0].Length] = points[0][last]; result[1][before[0].Length] = points[1][last]; return(result); } if (first + 1 != last) { // find point with the maximum distance. double maxDistance = 0; int foundIndex = -1; // create the line between first-last. LineF2D line = new LineF2D(new PointF2D( points[0][first], points[1][first]), new PointF2D( points[0][last], points[1][last])); for (int idx = first + 1; idx < last; idx++) { double distance = line.Distance(new PointF2D( points[0][idx], points[1][idx])); if (distance > maxDistance) { // larger distance found. maxDistance = distance; foundIndex = idx; } } if (foundIndex > 0 && maxDistance > epsilon) { // a point was found and it is far enough. double[][] before = SimplifyCurve.SimplifyBetween(points, epsilon, first, foundIndex); double[][] after = SimplifyCurve.SimplifyBetween(points, epsilon, foundIndex, last); // build result. result = new double[2][]; result[0] = new double[before[0].Length + after[0].Length - 1]; result[1] = new double[before[0].Length + after[0].Length - 1]; for (int idx = 0; idx < before[0].Length - 1; idx++) { result[0][idx] = before[0][idx]; result[1][idx] = before[1][idx]; } for (int idx = 0; idx < after[0].Length; idx++) { result[0][idx + before[0].Length - 1] = after[0][idx]; result[1][idx + before[0].Length - 1] = after[1][idx]; } return(result); } } result = new double[2][]; result[0] = new double[] { points[0][first], points[0][last] }; result[1] = new double[] { points[1][first], points[1][last] }; return(result); }