LaserMeasurement { @NonNull public static List<LaserMeasurement> reorder(@NonNull List<LaserMeasurement> points) { final Range xRange = new Range(); final Range yRange = new Range(); for (LaserMeasurement point : points) { xRange.expand(point.x); yRange.expand(point.y); } if (xRange.min < 0 && xRange.max <= 0 && yRange.min >= 0) { final List<LaserMeasurement> swapped = new ArrayList<>(); for (LaserMeasurement point : points) { swapped.add(new LaserMeasurement(point.y, point.x)); } Collections.sort(swapped, (l1, l2) -> Double.compare(l1.x, l2.x)); return swapped; } else if (yRange.min >= 0 && yRange.max > 0) { final List<LaserMeasurement> reversed = new ArrayList<>(); for (LaserMeasurement point : points) { reversed.add(new LaserMeasurement(xRange.max - point.x, point.y - yRange.max)); } reversed.add(new LaserMeasurement(xRange.max, -yRange.max)); Collections.sort(reversed, (l1, l2) -> Double.compare(l1.x, l2.x)); return reversed; } else { final List<LaserMeasurement> sorted = new ArrayList<>(points); Collections.sort(sorted, (l1, l2) -> Double.compare(l1.x, l2.x)); return sorted; } } LaserMeasurement(double x, double y); @NonNull @Override String toString(); @NonNull static List<LaserMeasurement> parse(@NonNull String pointString, boolean metric, boolean strict); @NonNull static List<LaserMeasurement> parseSafe(@NonNull String pointString, boolean metric); @NonNull static CharSequence render(@NonNull List<LaserMeasurement> points, boolean metric); @NonNull static List<LaserMeasurement> reorder(@NonNull List<LaserMeasurement> points); final double x; final double y; }
@Test public void reorder() { final List<LaserMeasurement> points = testPoints(); points.add(new LaserMeasurement(50, -50)); assertEquals("100.0, -100.0\n50.0, -50.0\n", LaserMeasurement.render(points, true).toString()); final List<LaserMeasurement> reordered = LaserMeasurement.reorder(points); assertEquals("50.0, -50.0\n100.0, -100.0\n", LaserMeasurement.render(reordered, true).toString()); }