diff --git a/geometric_separators.pde b/geometric_separators.pde index fa0526c..0946cd3 100644 --- a/geometric_separators.pde +++ b/geometric_separators.pde @@ -129,9 +129,20 @@ PVector getGeometricMedian(ArrayList input) { } else { // Get the point - float x = getAxisMin(input, false); - float y = getAxisMin(input, true); - return new PVector(x, y); + HashMap total = new HashMap(); + HashMap x = getAxisMin(input, false); + HashMap y = getAxisMin(input, true); + Set keys = x.keySet(); + for (PVector key : keys) { + total.put(key, x.get(key) + y.get(key)); + } + ArrayList> sortedList = new ArrayList>(total.entrySet()); + Collections.sort(sortedList, new Comparator>() { + public int compare(Entry entry1, Entry entry2) { + return (int)(entry1.getValue() - entry2.getValue()); + } + }); + return sortedList.get(0).getKey(); } } @@ -243,7 +254,7 @@ HashMap getSums(ArrayList input, boolean isY) { } // Get minimum point for given axis input -Float getAxisMin(ArrayList input, boolean isY) { +HashMap getAxisMin(ArrayList input, boolean isY) { HashMap sumSquaresLeft, sumSquaresRight; HashMap totalSumSquares = new HashMap(); // Get left distances @@ -267,19 +278,7 @@ Float getAxisMin(ArrayList input, boolean isY) { PVector point = input.get(i); totalSumSquares.put(point, sumSquaresLeft.get(point) + sumSquaresRight.get(point)); } - ArrayList> sortedList = new ArrayList>(totalSumSquares.entrySet()); - Collections.sort(sortedList, new Comparator>() { - public int compare(Entry entry1, Entry entry2) { - return (int)(entry1.getValue() - entry2.getValue()); - } - }); - // Return final point coordinate - if (isY) { - return sortedList.get(0).getKey().y; - } - else { - return sortedList.get(0).getKey().x; - } + return totalSumSquares; } // Draw