From a063b77934ed2a6e978319b5d0910b3afebe7b87 Mon Sep 17 00:00:00 2001 From: Andrew Lawson Date: Mon, 1 Dec 2014 00:48:07 -0500 Subject: [PATCH] Squashed bugs. Algorithm runs successfully - centerpoint approx. is incorrect, needs some fixes. --- geometric_separators.pde | 67 +++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/geometric_separators.pde b/geometric_separators.pde index 2759947..df2b97a 100644 --- a/geometric_separators.pde +++ b/geometric_separators.pde @@ -104,12 +104,12 @@ void mousePressed() { // If mouse presses calculate button else if ((mouseX >= calc_x && mouseX <= (calc_x + calc_w)) && (mouseY >= calc_y && mouseY <= (calc_y + calc_h))) { - // Run algorithm - centerPoint = approxCenterpoint(rawInput); - if (centerPoint != null) { - System.out.println("x-coordinate: " + Float.toString(centerPoint.x)); - System.out.println("y-coordinate: " + Float.toString(centerPoint.y)); - } + // Run algorithm + centerPoint = approxCenterpoint(rawInput); + if (centerPoint != null) { + System.out.println("x-coordinate: " + Float.toString(centerPoint.x)); + System.out.println("y-coordinate: " + Float.toString(centerPoint.y)); + } } else { PVector new_point = new PVector(mouseX, mouseY); @@ -120,41 +120,41 @@ void mousePressed() { // Get geometric median PVector getGeometricMedian(ArrayList input) { - System.out.println("Geometric median..."); if (input.size() == 0) { + System.out.println("You don't have any input points!"); return null; } - else if (input.size() == 1) { + else if (input.size() == 1) { return input.get(0); - } - else { - // Get the point - float x = getAxisMin(input, false); - float y = getAxisMin(input, true); - return new PVector(x, y); + } + else { + // Get the point + float x = getAxisMin(input, false); + float y = getAxisMin(input, true); + return new PVector(x, y); } } // Sample input points into sets of 4 -ArrayList> samplePoints(ArrayList input) { - ArrayList> setList = new ArrayList>(); - HashSet pointSet = new HashSet(); +ArrayList> samplePoints(ArrayList input) { + ArrayList> setList = new ArrayList>(); + ArrayList pointList = new ArrayList(); // While there are still points, split into sets - while (input.size() > 1) { - System.out.println("Sampling..."); + while (input.size() > 0) { double rnd = new Random().nextDouble(); int index = (int)(rnd * 10) % input.size(); // Add to set - pointSet.add(input.get(index)); + pointList.add(input.get(index)); + input.remove(index); // Create new set on max size - if (pointSet.size() == 4) { - setList.add(pointSet); - pointSet = new HashSet(); + if (pointList.size() == 4) { + setList.add(pointList); + pointList = new ArrayList(); } } // Add most recent unempty set to list - if (pointSet.size() > 0) { - setList.add(pointSet); + if (pointList.size() > 0) { + setList.add(pointList); } return setList; } @@ -170,11 +170,11 @@ PVector approxCenterpoint(ArrayList input) { // Repeat 1 - 3 until one point remains and return that point while (input.size() > 1) { // 1. Sample points into groups of 4 - ArrayList> setList = samplePoints(input); + ArrayList> setList = samplePoints(input); // 2. Compute radon point of each group (geometric median) ArrayList radonList = new ArrayList(); - for (HashSet set : setList) { - PVector radonPoint = getGeometricMedian(input); + for (ArrayList list : setList) { + PVector radonPoint = getGeometricMedian(list); radonList.add(radonPoint); // 3. Set input to be new radon points } @@ -230,7 +230,7 @@ HashMap getSums(ArrayList input, boolean isY) { HashMap sumSquaresMemoize = new HashMap(); ReturnTriple returnTriple = new ReturnTriple(0, 0, null); PVector currentPoint, nextPoint; - float sum, dist; + float sum, dist; for (int i = 0; i < input.size() - 1; i++) { currentPoint = input.get(i); nextPoint = input.get(i + 1); @@ -243,8 +243,6 @@ HashMap getSums(ArrayList input, boolean isY) { sumSquaresMemoize = returnTriple.memoize; 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; } @@ -260,7 +258,6 @@ Float getAxisMin(ArrayList input, boolean isY) { Collections.sort(input, compareX); } sumSquaresLeft = getSums(input, isY); - System.out.println(sumSquaresLeft); // Get right distances if (isY) { Collections.sort(input, compareYRev); @@ -269,7 +266,6 @@ Float getAxisMin(ArrayList input, boolean isY) { Collections.sort(input, compareXRev); } sumSquaresRight = getSums(input, isY); - System.out.println(sumSquaresRight); // Calculate total sum and store it for (int i = 0; i < sumSquaresLeft.size(); i++) { PVector point = input.get(i); @@ -308,8 +304,7 @@ void draw() { } // Draw center point if (centerPoint != null) { - strokeWeight(6); - fill (255, 0, 0); - point(centerPoint.x, centerPoint.y); + strokeWeight(6); + point(centerPoint.x, centerPoint.y); } }