Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created December 30, 2018 00:32
Show Gist options
  • Save jrjames83/4d0b66d056c4897e7b94acb4febc671f to your computer and use it in GitHub Desktop.
Save jrjames83/4d0b66d056c4897e7b94acb4febc671f to your computer and use it in GitHub Desktop.
// related to https://www.youtube.com/watch?v=XATr_jdh-44
// before watching
var circles = [];
var used_circles = [];
let LAPS = 0;
let N_TRIES = 10000;
function randomIntFromInterval(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
function mouseClicked() {
console.log(mouseX, mouseY)
}
function drawCircle(circle, ith) {
fill(250, 0, 150, 100);
noStroke();
ellipse(circle.x, circle.y, circle.r*2, circle.r*2);
text(ith, circle.x, circle.y)
}
function circlesOverlap(c1, c2) {
let dist_between_circles = dist(c1.x, c1.y, c2.x, c2.y);
let radius_x2 = c1.r + c2.r;
// if the distance is > the sum of the radii, they do not overlap
if (dist_between_circles > radius_x2) {
return false;
} else {
return true;
}
}
// Returns true if there is a conflict with this circle and any existing circles
// any conflict means we cannot proceed
function circleConflictsWithExistingCircles(cand_circle, existing_circles) {
for(i = 0; i < existing_circles.length; i++) {
if(circlesOverlap(cand_circle, existing_circles[i])) {
return true;
break;
}
}
return false;
}
function setup() {
createCanvas(640, 640)
for (var i = 0; i < N_TRIES; i++) {
var circle = {
x: random(width),
y: random(height),
r: randomIntFromInterval(5,25)
}
circles.push(circle);
}
// Draw the first circle
drawCircle(circles[0])
used_circles.push(circles[0])
circles.splice(0, 1);
//while (used_circles.length < 100)
for(c = 0; c < circles.length; c++) {
conflict = circleConflictsWithExistingCircles(circles[c], used_circles);
if (!conflict) {
drawCircle(circles[c], c); // OK to draw
used_circles.push(circles[c]); // add it to used circles for future reference
}
}
console.log(used_circles.length)
console.log(N_TRIES)
}
function draw() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment