Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed centerpoint algorithm to return a point in the input rather tha…
…n a new point.
  • Loading branch information
Andrew Lawson authored and Andrew Lawson committed Dec 1, 2014
1 parent 4cef74d commit 8394787
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions geometric_separators.pde
Expand Up @@ -129,9 +129,20 @@ PVector getGeometricMedian(ArrayList<PVector> input) {
} }
else { else {
// Get the point // Get the point
float x = getAxisMin(input, false); HashMap<PVector, Float> total = new HashMap<PVector, Float>();
float y = getAxisMin(input, true); HashMap<PVector, Float> x = getAxisMin(input, false);
return new PVector(x, y); 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();
} }
} }


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


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


// Draw // Draw
Expand Down

0 comments on commit 8394787

Please sign in to comment.