xxxxxxxxxx
122
//https://youtu.be/QHEQuoIKgNE
let circles = [];
let circlesPerDrawCall = 1;
const maxAttempts = 200;
let boxX =0;
let boxY =0;
function Circle(x, y, _color) {
this.x = x;
this.y = y;
this.r = 1;
this.growing = true;
this.color = _color;
this.show = function (){
stroke(this.color);
noFill();
strokeWeight(1);
ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
this.grow = function(){
if (this.growing) {
this.r += 1;
}
}
this.edges = function(){
return (
this.x + this.r >= boxX ||
this.x - this.r <= 0 ||
this.y + this.r >= boxY ||
this.y - this.r <= 0
);
}
}
function setup() {
createCanvas(1200, 1200);
}
function draw() {
background(0);
frameRate(20);
//growing bounding box
for(let i = 0; i < 640; i++){
boxX = boxX + 0.001;
boxY = boxY + 0.001;
}
// //visualize growing box
// line(0,boxX,boxY,boxX);
// line(boxY,0,boxY,boxX);
let count = 0;
let attempts = 0;
while(count < circlesPerDrawCall){
let circle = newCircle();
if(circle !== null){
circles.push(circle);
count ++;
}
attempts++;
if(attempts > maxAttempts || circles.length > 2000 || boxX > width){
noLoop();
console.log('finished');
break;
}
}
for(let i = 0; i < circles.length; i++){
let circle = circles[i];
if(circle.growing){
if(circle.edges()){
circle.growing = false;
}else{
// does it touches other circles?
for(let j = 0; j< circles.length; j++){
let other = circles[j];
if(circle !== other){
let d = dist(circle.x, circle.y, other.x, other.y);
let sumRadius = circle.r + other.r;
if(d < sumRadius){
circle.growing = false;
break;
}
}
}
}
}
circle.grow();
circle.show();
}
}
function newCircle() {
let x = random(boxX);
let y = random(boxY);
let _color = random(40, boxX/5);
let valid = true;
for (let i = 0; i < circles.length; i++) {
let circle = circles[i];
let d = dist(x,y, circle.x,circle.y);
if(d < circle.r) {
valid = false;
break;
}
}
if(valid){
return new Circle(x, y, _color);
}else {
return null;
}
}