xxxxxxxxxx
88
function setup() {
createCanvas(900, 900);
d = pixelDensity();
cols = ["#001219","#005f73","#0a9396","#94d2bd","#e9d8a6","#ee9b00","#ca6702","#bb3e03","#ae2012","#9b2226"];
spot = new Spot(random(width), random(height), random(PI), floor(random(cols.length)));
background(0);
fill(255);
noStroke();
}
function draw() {
loadPixels();
spot.update()
}
class Spot{
constructor(x, y, dir){
if(x <= 0 || x >= width || y <= 0 || y >= height){
this.pos = createVector(random(width), random(height));
} else {
this.pos = createVector(x, y);
}
this.npos = createVector(x, y);
this.sp = createVector(1, 0);
this.test = createVector(0, 0);
this.testp = 0;
this.testc = 0;
this.fail = false;
this.angle = false;
this.rad = false;
this.i = 0;
this.dir = dir;
this.c = c;
this.col = color(cols[this.c])
}
update(){
if(this.pos.x <= 0 || this.pos.x >= width || this.pos.y <= 0 || this.pos.y >= height){
spot = new Spot(random(width), random(height), random(PI));
}
for(this.i = 0; this.i < 100; this.i++){
this.test = p5.Vector.add(this.pos, this.sp);
if(this.test.x <= 0 || this.test.x >= width || this.test.y <= 0 || this.test.y >= height){
spot = new Spot(random(width), random(height), random(PI));
}
this.testp = 4 * (d * floor(this.test.y)) * (d * width) + 4 * (d * floor(this.test.x));
this.testc = pixels[this.testp] + pixels[this.testp + 1] + pixels[this.testp + 2];
if(this.testc == 0 && this.test.x > 0 && this.test.x < width & this.test.y > 0 && this.test.y < height){
this.sp.setHeading(this.sp.heading() + PI * 0.01);
this.rad = false
} else {
this.rad = true;
this.i = 100;
}
}
if(this.rad == true){
this.testp = 4 * (d * floor(this.pos.y)) * (d * width) + 4 * (d * floor(this.pos.x));
this.testc = pixels[this.testp] + pixels[this.testp + 1] + pixels[this.testp + 2];
if(this.testc == 0){
circle(this.pos.x, this.pos.y, this.sp.mag() * 2 - 1);
} else {
spot = new Spot(random(width), random(height), random(PI), floor(random(cols.length)));
}
if(this.test.x <= 0 || this.test.x >= width || this.test.y <= 0 || this.test.y >= height){
this.sp.setHeading(this.sp.heading() + PI + random(-QUARTER_PI, QUARTER_PI));
this.sp.setMag(this.sp.mag() * 2);
this.npos = p5.Vector.add(this.pos, this.sp);
if(this.c < cols.length - 1){ this.c++} else {this.c = 0}
spot = new Spot(this.npos.x, this.npos.y, this.sp.heading(), this.c);
} else {
this.sp.setHeading(this.dir + random(-QUARTER_PI, QUARTER_PI));
this.sp.setMag(this.sp.mag() * 2);
this.npos = p5.Vector.add(this.pos, this.sp);
if(this.c < cols.length - 1){ this.c++} else {this.c = 0}
spot = new Spot(this.npos.x, this.npos.y, this.sp.heading(), this.c);
}
} else {
this.sp.setMag(this.sp.mag() + 1);
}
}
}