Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added 3rd dimension. Implemented remainder of algorithm. Needs testing.
  • Loading branch information
Andrew Lawson authored and Andrew Lawson committed Dec 1, 2014
1 parent 517ab55 commit 01c859f
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions geometric_separators.pde
Expand Up @@ -14,6 +14,7 @@ int calc_h = 25;

PShape reset;
PShape calculate;
PShape sphere;
PVector centerPoint;
ArrayList<PVector> rawInput = new ArrayList<PVector>();
Comparator<PVector> compareX, compareXRev, compareY, compareYRev;
Expand Down Expand Up @@ -112,30 +113,45 @@ void mousePressed() {
}
}
else {
PVector new_point = new PVector(mouseX, mouseY);
rawInput.add(new_point);
// Create new 2D point from mouse coordinates
PVector prelim_point = new PVector(mouseX, mouseY);
// Lift to 3D with the squared magnitude of the original point
PVector final_point = new PVector(prelim_point.x, prelim_point.y,
(float)Math.pow(prelim_point.mag(), 2))
rawInput.add(final_point);
}
redraw();
}

// Get geometric separator
PShape getSeparator() {
// Algorithm
// 1. Get centerpoint
PVector centerPoint = approxCenterpoint(rawInput);
// 2. Get random unit vector in 3D
PVector unitVector = PVector.Random3D();
// 3. Get radius
float radius = getRadius(centerPoint, unitVector);
// 4. Output sphere separator

float[4] sphereAttributes = getSphereAttr(centerPoint, unitVector, radius);
sphere = createShape(ELLIPSE, sphereAttributes);
}

// Get radius
// Get radius for our separator
float getRadius(PVector centerPoint, PVector unitVector) {
float num = (float)Math.sqrt(centerPoint.z - centerPoint.mag());
return num / Math.abs(unitVector.z);
}

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

// Get geometric median
PVector getGeometricMedian(ArrayList<PVector> input) {
if (input.size() == 0) {
Expand Down Expand Up @@ -312,12 +328,17 @@ void draw() {
text("Calculate.", calc_x + 25, calc_y + 15);
// Draw input
for (PVector point : rawInput) {
strokeWeight(4);
point(point.x, point.y);
strokeWeight(4);
point(point.x, point.y);
}
// Draw center point
if (centerPoint != null) {
strokeWeight(6);
point(centerPoint.x, centerPoint.y);
strokeWeight(6);
point(centerPoint.x, centerPoint.y);
}
// Draw separator sphere projected to 2D
if (sphere != null) {
strokeWeight(6);
shape(sphere);
}
}

0 comments on commit 01c859f

Please sign in to comment.