Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Squashed bugs. Algorithm runs successfully - centerpoint approx. is i…
…ncorrect, needs some fixes.
  • Loading branch information
Andrew Lawson committed Dec 1, 2014
1 parent 26a5259 commit a063b77
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions geometric_separators.pde
Expand Up @@ -104,12 +104,12 @@ void mousePressed() {
// If mouse presses calculate button // If mouse presses calculate button
else if ((mouseX >= calc_x && mouseX <= (calc_x + calc_w)) && else if ((mouseX >= calc_x && mouseX <= (calc_x + calc_w)) &&
(mouseY >= calc_y && mouseY <= (calc_y + calc_h))) { (mouseY >= calc_y && mouseY <= (calc_y + calc_h))) {
// Run algorithm // Run algorithm
centerPoint = approxCenterpoint(rawInput); centerPoint = approxCenterpoint(rawInput);
if (centerPoint != null) { if (centerPoint != null) {
System.out.println("x-coordinate: " + Float.toString(centerPoint.x)); System.out.println("x-coordinate: " + Float.toString(centerPoint.x));
System.out.println("y-coordinate: " + Float.toString(centerPoint.y)); System.out.println("y-coordinate: " + Float.toString(centerPoint.y));
} }
} }
else { else {
PVector new_point = new PVector(mouseX, mouseY); PVector new_point = new PVector(mouseX, mouseY);
Expand All @@ -120,41 +120,41 @@ 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) {
System.out.println("You don't have any input points!");
return null; return null;
} }
else if (input.size() == 1) { else if (input.size() == 1) {
return input.get(0); return input.get(0);
} }
else { else {
// Get the point // Get the point
float x = getAxisMin(input, false); float x = getAxisMin(input, false);
float y = getAxisMin(input, true); float y = getAxisMin(input, true);
return new PVector(x, y); return new PVector(x, y);
} }
} }


// Sample input points into sets of 4 // Sample input points into sets of 4
ArrayList<HashSet<PVector>> samplePoints(ArrayList<PVector> input) { ArrayList<ArrayList<PVector>> samplePoints(ArrayList<PVector> input) {
ArrayList<HashSet<PVector>> setList = new ArrayList<HashSet<PVector>>(); ArrayList<ArrayList<PVector>> setList = new ArrayList<ArrayList<PVector>>();
HashSet<PVector> pointSet = new HashSet<PVector>(); ArrayList<PVector> pointList = new ArrayList<PVector>();
// While there are still points, split into sets // While there are still points, split into sets
while (input.size() > 1) { while (input.size() > 0) {
System.out.println("Sampling...");
double rnd = new Random().nextDouble(); double rnd = new Random().nextDouble();
int index = (int)(rnd * 10) % input.size(); int index = (int)(rnd * 10) % input.size();
// Add to set // Add to set
pointSet.add(input.get(index)); pointList.add(input.get(index));
input.remove(index);
// Create new set on max size // Create new set on max size
if (pointSet.size() == 4) { if (pointList.size() == 4) {
setList.add(pointSet); setList.add(pointList);
pointSet = new HashSet<PVector>(); pointList = new ArrayList<PVector>();
} }
} }
// Add most recent unempty set to list // Add most recent unempty set to list
if (pointSet.size() > 0) { if (pointList.size() > 0) {
setList.add(pointSet); setList.add(pointList);
} }
return setList; return setList;
} }
Expand All @@ -170,11 +170,11 @@ PVector approxCenterpoint(ArrayList<PVector> input) {
// 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<HashSet<PVector>> setList = samplePoints(input); ArrayList<ArrayList<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 (HashSet<PVector> set : setList) { for (ArrayList<PVector> list : setList) {
PVector radonPoint = getGeometricMedian(input); PVector radonPoint = getGeometricMedian(list);
radonList.add(radonPoint); radonList.add(radonPoint);
// 3. Set input to be new radon points // 3. Set input to be new radon points
} }
Expand Down Expand Up @@ -230,7 +230,7 @@ HashMap<PVector, Float> getSums(ArrayList<PVector> input, boolean isY) {
HashMap<PVector, Float> sumSquaresMemoize = new HashMap<PVector, Float>(); HashMap<PVector, Float> sumSquaresMemoize = new HashMap<PVector, Float>();
ReturnTriple returnTriple = new ReturnTriple(0, 0, null); ReturnTriple returnTriple = new ReturnTriple(0, 0, null);
PVector currentPoint, nextPoint; PVector currentPoint, nextPoint;
float sum, dist; float sum, dist;
for (int i = 0; i < input.size() - 1; i++) { for (int i = 0; i < input.size() - 1; i++) {
currentPoint = input.get(i); currentPoint = input.get(i);
nextPoint = input.get(i + 1); nextPoint = input.get(i + 1);
Expand All @@ -243,8 +243,6 @@ HashMap<PVector, Float> getSums(ArrayList<PVector> input, boolean isY) {
sumSquaresMemoize = returnTriple.memoize; sumSquaresMemoize = returnTriple.memoize;
sumSquaresMemoize.put(nextPoint, returnTriple.sum + (float)(i * Math.pow(returnTriple.dist, 2)) + (2 * i * returnTriple.dist)); sumSquaresMemoize.put(nextPoint, returnTriple.sum + (float)(i * Math.pow(returnTriple.dist, 2)) + (2 * i * returnTriple.dist));
} }
System.out.println("Printing dict...");
System.out.println(sumSquaresMemoize);
return sumSquaresMemoize; return sumSquaresMemoize;
} }


Expand All @@ -260,7 +258,6 @@ Float getAxisMin(ArrayList<PVector> input, boolean isY) {
Collections.sort(input, compareX); Collections.sort(input, compareX);
} }
sumSquaresLeft = getSums(input, isY); sumSquaresLeft = getSums(input, isY);
System.out.println(sumSquaresLeft);
// Get right distances // Get right distances
if (isY) { if (isY) {
Collections.sort(input, compareYRev); Collections.sort(input, compareYRev);
Expand All @@ -269,7 +266,6 @@ Float getAxisMin(ArrayList<PVector> input, boolean isY) {
Collections.sort(input, compareXRev); Collections.sort(input, compareXRev);
} }
sumSquaresRight = getSums(input, isY); sumSquaresRight = getSums(input, isY);
System.out.println(sumSquaresRight);
// Calculate total sum and store it // Calculate total sum and store it
for (int i = 0; i < sumSquaresLeft.size(); i++) { for (int i = 0; i < sumSquaresLeft.size(); i++) {
PVector point = input.get(i); PVector point = input.get(i);
Expand Down Expand Up @@ -308,8 +304,7 @@ void draw() {
} }
// Draw center point // Draw center point
if (centerPoint != null) { if (centerPoint != null) {
strokeWeight(6); strokeWeight(6);
fill (255, 0, 0); point(centerPoint.x, centerPoint.y);
point(centerPoint.x, centerPoint.y);
} }
} }

0 comments on commit a063b77

Please sign in to comment.