rdpRecursive function
Recursive RDP algorithm.
Implementation
List<MapEntry<DateTime, double>> rdpRecursive(
List<MapEntry<DateTime, double>> points,
double epsilon,
) {
if (points.length < 3) return points;
// Find the point with maximum distance from line segment.
var maxDistance = 0.0;
var maxIndex = 0;
final firstPoint = points.first;
final lastPoint = points.last;
for (var i = 1; i < points.length - 1; i++) {
final distance = perpendicularDistance(
points[i],
firstPoint,
lastPoint,
points.length,
);
if (distance > maxDistance) {
maxDistance = distance;
maxIndex = i;
}
}
// If max distance is greater than epsilon, recursively simplify.
if (maxDistance > epsilon) {
final left = rdpRecursive(points.sublist(0, maxIndex + 1), epsilon);
final right = rdpRecursive(points.sublist(maxIndex), epsilon);
// Combine results (remove duplicate middle point)
return [...left.sublist(0, left.length - 1), ...right];
} else {
// If max distance is less than epsilon, keep only endpoints.
return [firstPoint, lastPoint];
}
}