Skip to content

Instantly share code, notes, and snippets.

@vvzen
Last active October 6, 2016 01:13
Show Gist options
  • Save vvzen/7e4f264d68f2730f0290fd7972cd15f9 to your computer and use it in GitHub Desktop.
Save vvzen/7e4f264d68f2730f0290fd7972cd15f9 to your computer and use it in GitHub Desktop.
A brute force approach for sampling 2d unequal circles inside a rect. Openframeworks (C++)
// BRUTE FORCE APPROACH
int nOfTrials = 0;
// Stop when we've got 1024 circles inside circles vector
// circles is a vector of PackedCircle
while(circles.size() < 1024){
// A simple custom object for drawing circles
PackedCircle nextCircle;
// Add new circle at random position x and y and with random radius ranging from 4 to 64
nextCircle.setup(ofPoint(ofRandom(ofGetWidth()), ofRandom(ofGetHeight())), ofRandom(4,64));
bool overlapping = false;
// Loop inside the list of all circles made
for(int c = 0; c < circles.size(); c++){
PackedCircle otherCircle = circles[c];
float currentDistance = nextCircle.pos.distance(otherCircle.pos);
// Check if circle overlaps
if(currentDistance < nextCircle.radius + otherCircle.radius){
overlapping = true;
}
}
// If the current circle does not overlap, add it
if(!overlapping){
circles.push_back(nextCircle);
}
// Break the loop after a number of trials
nOfTrials++;
if(nOfTrials > 2000000){
break;
}
}
cout << "There are " << circles.size() << " circles" << endl;
// Draw all circles
for(int i = 0; i < circles.size(); i++){
circles[i].draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment