xxxxxxxxxx
190
let history = [];
let movers = [];
let dots = [];
let cols = ["#8ecae6","#57afd2","#219ebc","#1f6a92","#023047","#ffb703","#ff9d00","#fb8500","#ff6f00","#fb5f0a"];
let i = 0;
let dis = 0;
let max = 0;
let center;
function setup() {
createCanvas(900, 900);
center = createVector(0, 0);
for(i = 0; i < 1; i++){
movers[i] = new Mover(width / 2, height / 2);
}
background(255);
max = sqrt(sq(width) + sq(height));
}
function draw() {
for(i = 0; i < dots.length; i++){
dots[i].update();
}
if(dots.length < 1000){
for(i = 0; i < 1; i++){
movers[0].update();
}
if(frameCount % 80 == 0){
for(i = 0; i < dots.length; i++){
dots[i].closest();
}
}
}
}
class Mover{
constructor(x, y){
this.cpos = createVector(x, y);
this.npos = createVector(x, y);
this.vel = createVector(4, 0);
this.vel.setHeading(random(TWO_PI));
this.dist = p5.Vector.sub(center, this.cpos);
this.x = x;
this.y = y;
this.d = 0;
this.push = createVector(0, 0);
this.i = 0;
this.closest = undefined;
this.closestDist = 0;
this.c = 0;
this.col = color(cols[this.c])
}
update(){
this.nf = noise(this.cpos.x / 100, this.cpos.y / 100);
// if(frameCount % 200 == 0){
this.c = floor(this.nf * cols.length);
// }
this.col = color(cols[this.c]);
this.x = this.cpos.x;
this.y = this.cpos.y;
history[history.length] = createVector(this.x, this.y);
this.vel.setHeading(this.vel.heading() + this.nf)
if(this.t > 0){
this.t = this.t - 0.0001;
}
if(history.length > 50){
for(this.i = 0; this.i < history.length - 50; this.i ++){
this.d = dist(this.cpos.x, this.cpos.y, history[this.i].x, history[this.i].y);
if(this.closest == undefined){
this.closest = createVector(history[this.i].x, history[this.i].y);
this.closestDist = this.d;
}
if(this.d < this.closestDist){
this.closest = createVector(history[this.i].x, history[this.i].y);
this.closestDist = this.d;
}
}
this.push = p5.Vector.sub(this.cpos, this.closest);
this.vel.setHeading(this.push.heading() + map(this.closestDist, this.nf * 2 + 1, this.nf * 5 + 2, PI / 2, PI / 3) )
this.closest = undefined;
}
if(history.length > 10000) {
history.splice(0, 1);
}
this.vel.setMag(5);
if(frameCount % 4 == 0){
dots[dots.length] = new Dot(this.cpos.x, this.cpos.y, dots.length);
// dots[dots.length - 1].closest();
}
this.cpos.add(this.vel);
}
}
class Dot{
constructor(x, y, index){
this.x = x;
this.y = y;
this.index = index;
this.r = random(255);
this.g = random(255);
this.b = random(255);
this.dist = undefined;
this.c1 = undefined;
this.c1dist = undefined;
this.c2 = undefined;
this.c2dist = undefined;
this.c3 = undefined;
this.c3dist = undefined;
this.c4 = undefined;
this.c4dist = undefined;
this.col = color(this.r, this.g, this.b);
// dots[this.index].closest();
this.nf1 = 0;
this.nf2 = 0;
this.nf3 = 0;
}
closest(){
this.c1 = undefined;
this.c2 = undefined;
this.c3 = undefined;
this.c4 = undefined;
for(this.i = 0; this.i < dots.length; this.i++){
if(this.i !== this.index && dots.length > 5){
this.dist = dist(this.x, this.y, dots[this.i].x, dots[this.i].y);
if(this.c1 == undefined){
this.c1 = dots[this.i];
this.c1dist = this.dist;
} else if(this.c2 == undefined){
this.c2 = dots[this.i];
this.c2dist = this.dist;
} else if(this.c3 == undefined){
this.c3 = dots[this.i];
this.c3dist = this.dist;
} else if(this.c4 == undefined){
this.c4 = dots[this.i];
this.c4dist = this.dist;
} else if(this.dist < this.c1dist) {
this.c1dist = this.dist;
this.c1 = dots[this.i];
} else if(this.dist < this.c2dist){
this.c2dist = this.dist;
this.c2 = dots[this.i];
} else if(this.dist < this.c3dist){
this.c3dist = this.dist;
this.c3 = dots[this.i];
} else if(this.dist < this.c4dist){
this.c4dist = this.dist;
this.c4 = dots[this.i];
}
}
}
}
update(){
this.nf1 = noise(this.index / 50, frameCount / 50, dots.length / 50);
this.nf2 = noise(dots.length / 50, this.index / 50, frameCount / 50);
this.nf3 = noise(frameCount / 50, dots.length / 50, this.index / 50);
if(this.index % 20 !== 0){
if(this.c4 !== undefined){
this.col = color(lerpColor(this.col, this.c1.col, 0.5));
this.col = color(lerpColor(this.col, this.c2.col, 0.5));
this.col = color(lerpColor(this.col, this.c3.col, 0.5));
this.col = color(lerpColor(this.col, this.c4.col, 0.5));
}
}
if(this.index % 20 == 0){
this.r = this.nf1 * 255;
this.g = this.nf2 * 255;
this.b = this.nf3 * 255;
this.col = color(this.r, this.g, this.b);
}
fill(this.col);
noStroke();
circle(this.x, this.y, 30);
}
}