createCanvas(wind, wind);
colors = [createVector(211, 183, 62), createVector(163, 164, 122), createVector(137,164,143), createVector(139,52,31)];
for(var num = 0; num < circlesLoc.length; num++){
circle(circlesLoc[num].x, circlesLoc[num].y, circlesSize[num]);
for(var c = 0; c < circlesLoc.length; c++){
for(var d = 0; d < c; d++){
fill(colors[circleColor[c]].x, colors[circleColor[c]].y, colors[circleColor[c]].z, 100)
stroke(colors[circleColor[c]].x, colors[circleColor[c]].y, colors[circleColor[c]].z, 100);
intersect(circlesLoc[c].x, circlesLoc[c].y , circlesSize[c] , circlesLoc[d].x , circlesLoc[d].y , circlesSize[d], c);
for(var x = 0; x < 20; x++){
circlesLoc.push(createVector(int(random(wind/7,wind - wind/7)),int(random(wind/7,wind - wind/7))));
circlesSize.push(int(random(wind/12,wind/7)));
circleSpeed.push(createVector(random(-4,4),random(-4,4)));
circleColor.push(int(random(4)));
for(var s = 0; s < circlesLoc.length; s++){
circlesLoc[s].x += circleSpeed[s].x;
circlesLoc[s].y += circleSpeed[s].y;
if(circlesLoc[s].x <= circlesSize[s]/2){
circleSpeed[s].x = circleSpeed[s].x*(-1);
else if(circlesLoc[s].x >= wind - circlesSize[s]/2){
circleSpeed[s].x = circleSpeed[s].x*(-1);
else if(circlesLoc[s].y <= circlesSize[s]/2){
circleSpeed[s].y = circleSpeed[s].y*(-1);
else if(circlesLoc[s].y >= wind - circlesSize[s]/2){
circleSpeed[s].y = circleSpeed[s].y*(-1);
function intersect(x1,y1,d1,x2,y2,d2,c){
if( (x1 + d1/2 < x2 + d2/2) && (x1 - d1/2 > x2 - d2/2) && (y1 + d1/2 < y2 + d2/2) && (y1 - d1/2 > y2 - d2/2) ){
} else if( (x2 + d2/2 < x1 + d1/2) && (x2 - d2/2 > x1 - d1/2) && (y2 + d2/2 < y1 + d1/2) && (y2 - d2/2 > y1 - d1/2) ){
dist_between = sqrt(sq(y2-y1) + sq(x2-x1));
dist_between = - sqrt(sq(y2-y1) + sq(x2-x1));
var x_dist = (sq(dist_between) - sq(r2) + sq(r1))/(2*dist_between);
var y_dist = ((1/dist_between) * (sqrt(4*sq(dist_between)*sq(r2) - ((sq(sq(dist_between) - sq(r1) + sq(r2)))))))/2;
var y_mid = y1 + (sin(Angle_1)*x_dist);
var x_mid = x1 + (cos(Angle_1)*x_dist);
var b_per = (y_mid) - (-(1/m)*(x_mid));
var y_dist2 = sin(Angle_2) * y_dist;
var x_dist2 = cos(Angle_2) * y_dist;
var x_point1 = x_mid - x_dist2;
var x_point2 = x_mid + x_dist2;
var y_point1 = y_mid + y_dist2;
var y_point2 = y_mid - y_dist2;
var hyp1 = dist(x1, y1, x_point1, y_point1);
var hyp2 = dist(x1, y1, x_point2, y_point2);
var hyp3 = dist(x2, y2, x_point1, y_point1);
var hyp4 = dist(x2, y2, x_point2, y_point2);
var angle_x1 = asin( (x_point1 - x1)/ hyp1);
var angle_x2 = asin( (x_point2 - x1)/ hyp2);
var angle_x3 = asin( (x_point1 - x2)/ hyp3);
var angle_x4 = asin( (x_point2 - x2)/ hyp4);
var newAngle_x1 = arc_angle(y1, y_point1, angle_x1);
var newAngle_x2 = arc_angle(y1, y_point2, angle_x2);
var newAngle_x3 = arc_angle(y2, y_point1, angle_x3);
var newAngle_x4 = arc_angle(y2, y_point2, angle_x4);
if( (x_value * y_value) <= 0 ){
arc(x1,y1,d1,d1, newAngle_x1, newAngle_x2, CHORD);
arc(x2,y2,d2,d2, newAngle_x4, newAngle_x3, CHORD);
arc(x1,y1,d1,d1, newAngle_x2, newAngle_x1, CHORD);
arc(x2,y2,d2,d2, newAngle_x3, newAngle_x4, CHORD);
function arc_angle(y_orig, y_point, angle1){
if((y_point - y_orig) >= 0){