Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed some bugs.
  • Loading branch information
Andrew Lawson authored and Andrew Lawson committed Nov 30, 2014
1 parent 28418f2 commit 26a5259
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions geometric_separators.pde
Expand Up @@ -120,6 +120,7 @@ void mousePressed() {


// Get geometric median // Get geometric median
PVector getGeometricMedian(ArrayList<PVector> input) { PVector getGeometricMedian(ArrayList<PVector> input) {
System.out.println("Geometric median...");
if (input.size() == 0) { if (input.size() == 0) {
return null; return null;
} }
Expand All @@ -135,24 +136,25 @@ PVector getGeometricMedian(ArrayList<PVector> input) {
} }


// Sample input points into sets of 4 // Sample input points into sets of 4
ArrayList<Set<PVector>> samplePoints(ArrayList<PVector> input) { ArrayList<HashSet<PVector>> samplePoints(ArrayList<PVector> input) {
ArrayList<Set> setList = new ArrayList<Set<PVector>>(); ArrayList<HashSet<PVector>> setList = new ArrayList<HashSet<PVector>>();
Set<PVector> pointSet = new Set<PVector>(); HashSet<PVector> pointSet = new HashSet<PVector>();
// While there are still points, split into sets // While there are still points, split into sets
while (input.size() > 0) { while (input.size() > 1) {
int rnd = new Random().nextInt(); System.out.println("Sampling...");
int index = (rnd * (input.size())); double rnd = new Random().nextDouble();
int index = (int)(rnd * 10) % input.size();
// Add to set // Add to set
pointSet.add(input.get(index)); pointSet.add(input.get(index));
// Create new set on max size // Create new set on max size
if (pointSet.size() == 4) { if (pointSet.size() == 4) {
setList.add(pointSet); setList.add(pointSet);
pointSet = new Set<PVector>(); pointSet = new HashSet<PVector>();
} }
} }
// Add most recent unempty set to list // Add most recent unempty set to list
if (pointSet.size() > 0) { if (pointSet.size() > 0) {
setList.add(pointSet) setList.add(pointSet);
} }
return setList; return setList;
} }
Expand All @@ -161,23 +163,22 @@ ArrayList<Set<PVector>> samplePoints(ArrayList<PVector> input) {
PVector approxCenterpoint(ArrayList<PVector> input) { PVector approxCenterpoint(ArrayList<PVector> input) {
if (input.size() == 0) { if (input.size() == 0) {
System.out.println("You don't have any input points!"); System.out.println("You don't have any input points!");
return null;
} }
else { else {
// Algorithm // Algorithm
// Repeat 1 - 3 until one point remains and return that point // Repeat 1 - 3 until one point remains and return that point
while (input.size > 1) { while (input.size() > 1) {
// 1. Sample points into groups of 4 // 1. Sample points into groups of 4
ArrayList<Set<PVector>> setList = samplePoints(input); ArrayList<HashSet<PVector>> setList = samplePoints(input);
// 2. Compute radon point of each group (geometric median) // 2. Compute radon point of each group (geometric median)
ArrayList<PVector> radonList = new ArrayList<PVector>(); ArrayList<PVector> radonList = new ArrayList<PVector>();
for (Set<PVector> set : setList) { for (HashSet<PVector> set : setList) {
PVector radonPoint = getGeometricMedian(input); PVector radonPoint = getGeometricMedian(input);
else { radonList.add(radonPoint);
radonList.add(radonPoint);
}
// 3. Set input to be new radon points // 3. Set input to be new radon points
input = radonList;
} }
input = radonList;
} }
return input.get(0); return input.get(0);
} }
Expand Down

0 comments on commit 26a5259

Please sign in to comment.