xxxxxxxxxx
37
/* To place circles in random fashion
only if they do not overlap with any
previous circles
*/
let circles = [];
function setup() {
createCanvas(640, 300);
background(255);
//fill the array of circles
while(circles.length < 70){
let circle = {
x: random(width),
y: random(height),
r: random(12, 30)
};
//circles.push(circle);
let overlapping = false;
for(let j = 0; j < circles.length; j++) {
let other = circles[j];
let d = dist(circle.x, circle.y, other.x, other.y);
if(d < circle.r + other.r) {
overlapping = true;
break;
}
}
if(!overlapping){
circles.push(circle);
}
}
// to draw the circles as required
for(let i1 = 0; i1 < circles.length; i1++) {
fill(255, 120, 170, 100);
noStroke();
ellipse(circles[i1].x, circles[i1].y, 2*circles[i1].r);
}
}