I have a working code that takes a collection and applies a filter based on a certain distance.
For example, the second point in the list has to be at least 1KM apart from the first, and any items in between those two are removed. The input is already sorted.
private List<Point> filterByMinimumDistance(List<Point> points, double minDistance) {
Point reference = points.get(0);
List<Point> filtered = new ArrayList<>();
filtered.add(reference);
for (Point point : points) {
double distance = getDistance(reference, point);
if (distance >= minDistance) {
reference = point;
filtered.add(point);
}
}
return filtered;
}
private double getDistance(Point p1, Point p2) {
double dx = p1.getX() - p2.getX();
double dy = p1.getY() - p2.getY();
return Math.sqrt(dx * dx + dy * dy);
}
So far I haven't been able to come up with something that replaces this with a fancier stream solution. Any help would be appreciated. Thanks!
reference
if the distance is larger than the minimum. What's the actual algorithm here trying to do?