Skip to content
Permalink
Browse files

Fixed centerpoint algorithm to return a point in the input rather tha…

…n a new point.
  • Loading branch information
Andrew Lawson Andrew Lawson
Andrew Lawson authored and Andrew Lawson committed Dec 1, 2014
1 parent 4cef74d commit 8394787f4affbe4ba154d2af6bc84eb2b82189ea
Showing with 16 additions and 17 deletions.
  1. +16 −17 geometric_separators.pde
@@ -129,9 +129,20 @@ PVector getGeometricMedian(ArrayList<PVector> input) {
}
else {
// Get the point
float x = getAxisMin(input, false);
float y = getAxisMin(input, true);
return new PVector(x, y);
HashMap<PVector, Float> total = new HashMap<PVector, Float>();
HashMap<PVector, Float> x = getAxisMin(input, false);
HashMap<PVector, Float> y = getAxisMin(input, true);
Set<PVector> keys = x.keySet();
for (PVector key : keys) {
total.put(key, x.get(key) + y.get(key));
}
ArrayList<Entry<PVector, Float>> sortedList = new ArrayList<Entry<PVector, Float>>(total.entrySet());
Collections.sort(sortedList, new Comparator<Entry<PVector, Float>>() {
public int compare(Entry<PVector, Float> entry1, Entry<PVector, Float> entry2) {
return (int)(entry1.getValue() - entry2.getValue());
}
});
return sortedList.get(0).getKey();
}
}

@@ -243,7 +254,7 @@ HashMap<PVector, Float> getSums(ArrayList<PVector> input, boolean isY) {
}

// Get minimum point for given axis input
Float getAxisMin(ArrayList<PVector> input, boolean isY) {
HashMap<PVector, Float> getAxisMin(ArrayList<PVector> input, boolean isY) {
HashMap<PVector, Float> sumSquaresLeft, sumSquaresRight;
HashMap<PVector, Float> totalSumSquares = new HashMap<PVector, Float>();
// Get left distances
@@ -267,19 +278,7 @@ Float getAxisMin(ArrayList<PVector> input, boolean isY) {
PVector point = input.get(i);
totalSumSquares.put(point, sumSquaresLeft.get(point) + sumSquaresRight.get(point));
}
ArrayList<Entry<PVector, Float>> sortedList = new ArrayList<Entry<PVector, Float>>(totalSumSquares.entrySet());
Collections.sort(sortedList, new Comparator<Entry<PVector, Float>>() {
public int compare(Entry<PVector, Float> entry1, Entry<PVector, Float> 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

0 comments on commit 8394787

Please sign in to comment.
You can’t perform that action at this time.