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 Dec 1, 2014
1 parent 01c859f commit 30c0b2a
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions geometric_separators.pde
Expand Up @@ -31,6 +31,16 @@ private class ReturnTriple {
} }
} }


// Double used for returning centerpoint and sphere
private class CenterAndSphere {
public PVector center;
public PShape sphere;
public CenterAndSphere(PVector center, PShape sphere) {
this.center = center;
this.sphere = sphere;
}
}

// Setup // Setup
void setup() { void setup() {
background(255); background(255);
Expand Down Expand Up @@ -101,40 +111,41 @@ void mousePressed() {
// Reset input and background // Reset input and background
rawInput.clear(); rawInput.clear();
centerPoint = null; centerPoint = null;
sphere = null;
} }
// 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); CenterAndSphere returnVals = getSeparator(rawInput);
if (centerPoint != null) { sphere = returnVals.sphere;
System.out.println("x-coordinate: " + Float.toString(centerPoint.x)); centerPoint = returnVals.center;
System.out.println("y-coordinate: " + Float.toString(centerPoint.y));
}
} }
else { else {
// Create new 2D point from mouse coordinates // Create new 2D point from mouse coordinates
PVector prelim_point = new PVector(mouseX, mouseY); PVector prelim_point = new PVector(mouseX, mouseY);
// Lift to 3D with the squared magnitude of the original point // Lift to 3D with the squared magnitude of the original point
PVector final_point = new PVector(prelim_point.x, prelim_point.y, PVector final_point = new PVector(prelim_point.x, prelim_point.y,
(float)Math.pow(prelim_point.mag(), 2)) (float)Math.pow(prelim_point.mag(), 2));
rawInput.add(final_point); rawInput.add(final_point);
} }
redraw(); redraw();
} }


// Get geometric separator // Get geometric separator
PShape getSeparator() { CenterAndSphere getSeparator(ArrayList<PVector> input) {
// Algorithm // Algorithm
// 1. Get centerpoint // 1. Get centerpoint
PVector centerPoint = approxCenterpoint(rawInput); PVector centerPoint = approxCenterpoint(input);
// 2. Get random unit vector in 3D // 2. Get random unit vector in 3D
PVector unitVector = PVector.Random3D(); PVector unitVector = PVector.random3D();
// 3. Get radius // 3. Get radius
float radius = getRadius(centerPoint, unitVector); float radius = getRadius(centerPoint, unitVector);
// 4. Output sphere separator // 4. Output sphere separator
float[4] sphereAttributes = getSphereAttr(centerPoint, unitVector, radius); float[] sphereAttributes = getSphereAttr(centerPoint, unitVector, radius);
sphere = createShape(ELLIPSE, sphereAttributes); PShape separator = createShape(ELLIPSE, sphereAttributes);
CenterAndSphere returnVals = new CenterAndSphere(centerPoint, separator);
return returnVals;
} }


// Get radius for our separator // Get radius for our separator
Expand All @@ -143,12 +154,13 @@ float getRadius(PVector centerPoint, PVector unitVector) {
return num / Math.abs(unitVector.z); return num / Math.abs(unitVector.z);
} }


// Get the attributes for the sphere separators
float[] getSphereAttr(PVector centerPoint, PVector unitVector, float radius) { float[] getSphereAttr(PVector centerPoint, PVector unitVector, float radius) {
PVector center = centerPoint.sub(unitVector.mult(radius)); PVector center = centerPoint.sub(unitVector.mult(radius));
float x = center.x - radius; float x = center.x - radius;
float y = center.y - radius; float y = center.y - radius;
float z = center.z - radius; float z = center.z - radius;
float[4] attributes = {x, y, 2 * radius, 2 * radius}; float[] attributes = {x, y, 2 * radius, 2 * radius};
return attributes; return attributes;
} }


Expand Down Expand Up @@ -328,13 +340,15 @@ void draw() {
text("Calculate.", calc_x + 25, calc_y + 15); text("Calculate.", calc_x + 25, calc_y + 15);
// Draw input // Draw input
for (PVector point : rawInput) { for (PVector point : rawInput) {
strokeWeight(4); strokeWeight(4);
point(point.x, point.y); point(point.x, point.y);
} }
// Draw center point // Draw center point
if (centerPoint != null) { if (centerPoint != null) {
strokeWeight(6); strokeWeight(6);
point(centerPoint.x, centerPoint.y); point(centerPoint.x, centerPoint.y);
System.out.println("x-coordinate: " + Float.toString(centerPoint.x));
System.out.println("y-coordinate: " + Float.toString(centerPoint.y));
} }
// Draw separator sphere projected to 2D // Draw separator sphere projected to 2D
if (sphere != null) { if (sphere != null) {
Expand Down

0 comments on commit 30c0b2a

Please sign in to comment.