Skip to content

Commit

Permalink
Added 3rd dimension. Implemented remainder of algorithm. Needs testing.
Browse files Browse the repository at this point in the history
  • 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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int calc_h = 25;


PShape reset; PShape reset;
PShape calculate; PShape calculate;
PShape sphere;
PVector centerPoint; PVector centerPoint;
ArrayList<PVector> rawInput = new ArrayList<PVector>(); ArrayList<PVector> rawInput = new ArrayList<PVector>();
Comparator<PVector> compareX, compareXRev, compareY, compareYRev; Comparator<PVector> compareX, compareXRev, compareY, compareYRev;
Expand Down Expand Up @@ -112,30 +113,45 @@ void mousePressed() {
} }
} }
else { else {
PVector new_point = new PVector(mouseX, mouseY); // Create new 2D point from mouse coordinates
rawInput.add(new_point); 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(); redraw();
} }


// Get geometric separator // Get geometric separator
PShape getSeparator() { PShape getSeparator() {
// Algorithm
// 1. Get centerpoint // 1. Get centerpoint
PVector centerPoint = approxCenterpoint(rawInput); PVector centerPoint = approxCenterpoint(rawInput);
// 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);
sphere = createShape(ELLIPSE, sphereAttributes);
} }


// Get radius // Get radius for our separator
float getRadius(PVector centerPoint, PVector unitVector) { float getRadius(PVector centerPoint, PVector unitVector) {
float num = (float)Math.sqrt(centerPoint.z - centerPoint.mag()); float num = (float)Math.sqrt(centerPoint.z - centerPoint.mag());
return num / Math.abs(unitVector.z); 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 // Get geometric median
PVector getGeometricMedian(ArrayList<PVector> input) { PVector getGeometricMedian(ArrayList<PVector> input) {
if (input.size() == 0) { if (input.size() == 0) {
Expand Down Expand Up @@ -312,12 +328,17 @@ 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);
}
// Draw separator sphere projected to 2D
if (sphere != null) {
strokeWeight(6);
shape(sphere);
} }
} }

0 comments on commit 01c859f

Please sign in to comment.