xxxxxxxxxx
//Created by Arnav Mehta
//Initial set up of variables as well as predetermined color palette.
var grid = [];
var CircleRad = [];
var bg = "#f1e7de";
var Pallete = ["#343d3a", "#5fd0d4", "#efa93a", "#fd2e58"];
//This allows you to change the number of cirlces to be created on the canvas
var numOfCirc = 300;
function setup() {
createCanvas(0.95*windowHeight, 0.95*windowHeight);
background(100);
gridsetup();
CircleDraw();
noStroke();
}
function draw() {
}
//If Enter is pressed, this function generates new locations for the various circles and recalculates the radiuses
//Uses similar functions to CircleDraw function
function keyPressed(){
if(keyCode === ENTER){
CircleDraw();
}
}
function gridsetup() {
for(var a = 0; a < numOfCirc; a++){
grid.push(createVector(a,a));
CircleRad.push(a);
}
}
//This function creates the circles, by generating a random location and determining its radius
function CircleDraw() {
for(var v = 0; v < numOfCirc; v++){
var LocX = int(random(0.95*windowHeight));
var LocY = int(random(0.95*windowHeight));
var tested = false;
while(tested === false){
var HoldPoint = createVector(LocX,LocY);
var count = 0;
for(var o = 0; o < v; o++){
var check = (grid[o].dist(HoldPoint) - CircleRad[o]);
if(check <= 0){
count+=1;
}
}
if(count === 0){
tested = true;
}
else{
LocX = int(random(0.95*windowHeight));
LocY = int(random(0.95*windowHeight));
}
}
var numsArray = [LocX, LocY, abs(0.95*windowHeight - LocX), abs(0.95*windowHeight - LocY)];
var length = min(numsArray);
grid[v].x = LocX;
grid[v].y = LocY;
CircleRad[v] = length;
// by checking its radius with the distance between 2 circles and the other circles radius,
//you can adjust the value to fit accordingly so no circles will intersect.
for(var i = 0; i < v; i++){
var test = abs(grid[i].dist(grid[v]) - CircleRad[i]);
if(test < CircleRad[v]){
CircleRad[v] = test;
}
}
}
background(bg);
for(var d = 0; d < grid.length; d++){
noStroke();
fill(Pallete[int(random(4))]);
ellipse(grid[d].x, grid[d].y, 2*CircleRad[d]);
}
}