xxxxxxxxxx
90
let history = [];
let movers = [];
let cols = ["#ffa95e","#ffaf68","#ffb968","#ffc562","#c8ffec","#9ef5ff","#69c5ff","#5c9dff","#4667e1","#3030c2"];
let i = 0;
let dis = 0;
let max = 0;
let center;
let x = 0;
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));
x = 10;
stroke(0);
strokeWeight(1);
}
function draw() {
for(i = 0; i < 50; i++){
movers[0].update();
}
}
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.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]);
this.a = 0;
}
update(){
this.nf = noise(this.cpos.x / 100, this.cpos.y / 100);
this.c = floor(this.nf * cols.length);
this.col = color(cols[this.c]);
history[history.length] = createVector(this.cpos.x, this.cpos.y);
this.vel.setHeading(this.vel.heading() + 0.5)
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.a = floor(this.push.heading() + map(this.closestDist, this.nf * 1 + 0.5, this.nf * 10 + 1, PI / 10, PI / 2));
this.vel.setHeading(this.a);
this.closest = undefined;
}
if(history.length > 5000) {
history.splice(0, 1);
}
// if(this.cpos.x < 0 || this.cpos.x > width || this.cpos.y < 0 || this.cpos.y > height){
// history.splice(0, history.length);
// x = x + 50;
// movers[0] = new Mover(x, height / 2);
// }
this.vel.setMag(1);
this.npos = p5.Vector.add(this.cpos, this.vel);
line(this.cpos.x, this.cpos.y, this.npos.x, this.npos.y);
this.cpos.add(this.vel);
}
}