Plan/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/line/alg/DouglasPeuckerAlgorithm.java

85 lines
2.7 KiB
Java
Raw Normal View History

/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.rendering.json.graphs.line.alg;
2018-01-31 18:48:59 +01:00
import com.djrapitops.plan.delivery.rendering.json.graphs.line.Line;
import com.djrapitops.plan.delivery.rendering.json.graphs.line.Point;
2017-07-22 10:33:38 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
2018-01-25 15:02:05 +01:00
* Ramer-Douglas-Peucker Point Reduction Algorithm Implementation for reducing points from graphs.
*
* https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
2017-07-22 10:33:38 +02:00
*
* @author Rsl1122
*/
2017-07-28 19:37:25 +02:00
public class DouglasPeuckerAlgorithm {
2017-07-22 10:33:38 +02:00
/**
* Constructor used to hide the public constructor
*/
private DouglasPeuckerAlgorithm() {
throw new IllegalStateException("Utility class");
}
2017-07-22 10:33:38 +02:00
public static List<Point> reducePoints(List<Point> points, double epsilon) {
2017-07-23 12:09:31 +02:00
if (points.isEmpty()) {
return points;
}
if (Double.compare(epsilon, -1) == 0) {
2017-07-22 10:33:38 +02:00
epsilon = 0.002;
}
2017-07-22 10:33:38 +02:00
int size = points.size();
final int lastIndex = size - 1;
2017-07-26 15:46:19 +02:00
final Point start = points.get(0);
2017-07-22 10:33:38 +02:00
final Point end = points.get(lastIndex);
// Max distance and it's index.
double dMax = 0;
int index = 0;
for (int i = 1; i < size; i++) {
double d = perpendicularDistance(points.get(i), new Line(start, end));
if (d > dMax) {
dMax = d;
index = i;
}
}
List<Point> results;
if (dMax > epsilon) {
List<Point> results1 = reducePoints(points.subList(0, index), epsilon);
List<Point> results2 = reducePoints(points.subList(index, lastIndex), epsilon);
results = new ArrayList<>();
results.addAll(results1.subList(0, results1.size() - 1));
results.addAll(results2);
} else {
return Arrays.asList(points.get(0), points.get(lastIndex));
2017-07-22 10:33:38 +02:00
}
return results;
}
private static double perpendicularDistance(Point point, Line line) {
return line.getPerpendicularDistance(point);
2017-07-26 15:46:19 +02:00
}
2017-07-22 10:33:38 +02:00
}