Skip to content

Instantly share code, notes, and snippets.

@vvzen
Last active October 6, 2016 15:11
Show Gist options
  • Save vvzen/3b37821d5acb9568e971d6d587b95d26 to your computer and use it in GitHub Desktop.
Save vvzen/3b37821d5acb9568e971d6d587b95d26 to your computer and use it in GitHub Desktop.
// Everything is like before, we have only modified these 2 methods
// 1. Method for generating a mitchell best candidate. v2 : NOW WITH DIFFERENT RADII INCLUDED!
// PackedCircle is just a wrapper for a 2d point with a radius and the methods for drawing it
PackedCircle ofApp::getMitchellBestCandidate(){
// The more time has past, the more I want to pick smaller radii
float maxRunTime = 30 * 1000; // time in ms
float randomMin = maxRunTime / ofGetElapsedTimeMillis() / 2;
// If anything goes below 4, clamp it to 4
randomMin = randomMin > 4 ? randomMin : 4;
// I use a Perlin Noise for a more organic generation of the radii
float radiusNoise = ofNoise(ofGetElapsedTimef());
float greatestDistance = 0.0f;
PackedCircle winningSample;
// Init at (0,0) with radius of 0
winningSample.setup(ofPoint(0,0), 0.0f);
int k = 64;
// Loop for k times, where k is the number of samples: the higher, the better
for(int i = 0; i < k; i++){
// Radius is modelled after noise and time that has past
float radius = ofRandom(1,6) * radiusNoise * randomMin;
// Create a new random x,y point
ofPoint position(ofRandom(ofGetWidth()-radius), ofRandom(ofGetHeight()-radius));
PackedCircle candidate;
candidate.setup(position, radius);
float currentDistance;
// If this is the first dot we're placing, return this as best candidate
if(placedCircles.size() == 0) return candidate;
// Start from the distance to the nearest placed sample
currentDistance = getDistanceToNearestSample(candidate);
// Find the greatest distance among all k samples
if(currentDistance > greatestDistance){
greatestDistance = currentDistance;
winningSample = candidate;
}
}
//cout << "Winning radius: " << winningSample.radius << endl;
return winningSample;
}
// 2. Method for returning distance from sample and nearest already placed sample. v2 : NOW WITH DIFFERENT RADII INCLUDED!
float ofApp::getDistanceToNearestSample(PackedCircle candidate){
float shortestDistance = 0.0f;
// Loop into all samples
for(int i = 0; i < placedCircles.size(); i++){
PackedCircle otherSample = placedCircles[i];
// Measure current distance between the two points
// Distance = distance from point a and point b - sum of the radii of the 2 circles
float currentDistance = candidate.pos.distance(otherSample.pos) - (otherSample.radius + candidate.radius);
// Get nearest sample (from previously placed ones)
if(shortestDistance == 0.0f || currentDistance < shortestDistance){
shortestDistance = currentDistance;
}
}
return shortestDistance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment