Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Middle of working on DP distance problem.
  • Loading branch information
Andrew Lawson authored and Andrew Lawson committed Nov 29, 2014
1 parent 34da868 commit 462f6bc
Showing 1 changed file with 70 additions and 24 deletions.
94 changes: 70 additions & 24 deletions geometric_separators.pde
@@ -1,3 +1,5 @@
import java.util.*;

// Reset button coordinates
int reset_x = 140;
int reset_y = 460;
Expand All @@ -11,9 +13,11 @@ int calc_h = 25;
// Button Shapes
PShape reset;
PShape calculate;
// Input list
// Input lists
ArrayList<PVector> input = new ArrayList<PVector>();

ArrayList<Float> inputX, inputY;
// Misc
Comparator pointComparator;
// Setup
void setup() {
background(255);
Expand All @@ -22,51 +26,93 @@ void setup() {
reset = createShape(RECT, reset_x, reset_y, reset_w, reset_h);
calculate = createShape(RECT, calc_x, calc_y, calc_w, calc_h);
noLoop();
// Define new comparator
pointComparator = new Comparator<Float>() {
public int compare(Float p1, Float p2) {
if (p1 < p2) {
return -1;
}
else if (p2 < p1) {
return 1;
}
else {
return 0;
}
}
};
}

// On mouse press
void mousePressed() {
// If mouse presses reset button
if ((mouseX >= reset_x && mouseX <= (reset_x + reset_w)) &&
(mouseY >= reset_y && mouseY <= (reset_y + reset_h))) {
// Reset input and background
input.clear();
(mouseY >= reset_y && mouseY <= (reset_y + reset_h))) {
// Reset input and background
input.clear();
}
// If mouse presses calculate button
else if ((mouseX >= reset_x && mouseX <= (reset_x + reset_w)) &&
(mouseY >= reset_y && mouseY <= (reset_y + reset_h))) {
// Get sorted input points
else if ((mouseX >= calc_x && mouseX <= (calc_x + calc_w)) &&
(mouseY >= calc_y && mouseY <= (calc_y + calc_h))) {
// Build component lists
inputX = new ArrayList<Float>();
inputY = new ArrayList<Float>();
for (PVector point : input) {
inputX.add(point.x);
inputY.add(point.y);
}
// Sort lists
Collections.sort(inputX, pointComparator);
Collections.sort(inputY, pointComparator);
}
else {
PVector new_point = new PVector(mouseX, mouseY);
input.add(new_point);
PVector new_point = new PVector(mouseX, mouseY);
input.add(new_point);
}
redraw();
}

// Estimate centerpoint
void approxCenter(ArrayList<PShape> input) {
if (input.size() == 1) {
// Do nothing
// Do nothing
}
else {
// Sample points

// Do nothing
}
}

// Estimate the geometric median - dynamic programming
void geomMedian() {
// Memoization hash tables
HashMap<String, Double> left = new HashMap<String, Double>();
HashMap<String, Double> right = new HashMap<String, Double>();
HashMap<String, Double> up = new HashMap<String, Double>();
HashMap<String, Double> down = new HashMap<String, Double>();
// Sum of squares values
ArrayList<Double> leftSq = new ArrayList<Double>();
ArrayList<Double> rightSq = new ArrayList<Double>();
ArrayList<Double> upSq = new ArrayList<Double>();
ArrayList<Double> downSq = new ArrayList<Double>();
HashMap<Integer, Float> xMemoize = new HashMap<Integer, Float>();
HashMap<Integer, Float> yMemoize = new HashMap<Integer, Float>();
// Calculate for x-axis
// For each point
for (int i = 0; i < inputX.size(); i++) {
float currentPoint = inputX.get(i);
ArrayList<Double> leftSquares = new ArrayList<Double>();
ArrayList<Double> rightSquares = new ArrayList<Double>();
ArrayList<Double> upSquares = new ArrayList<Double>();
ArrayList<Double> downSquares = new ArrayList<Double>();
// Get distance to every previous point
}
}

// Calculate the distance with the input, memoization
void calcDist(float point, ArrayList<Float> input, HashMap<Integer, Float> memoize) {
if (memoize.contains(currentPoint)) {
// Reuse solution
return memoize.get(currentPoint);
}
else {
int dist = point - prevPoint;
for (int j = 0; j < i; j++) {
float prevPoint = input.get(j);
sum += currentPoint - prevPoint;
}
memoize.put(currentPoint, sum);
return sum;
}
}

// Draw
Expand All @@ -78,7 +124,7 @@ void draw() {
text("Reset.", reset_x + 30, reset_y + 15);
text("Calculate.", calc_x + 25, calc_y + 15);
for (PVector point : input) {
strokeWeight(4);
point(point.x, point.y);
strokeWeight(4);
point(point.x, point.y);
}
}

0 comments on commit 462f6bc

Please sign in to comment.