const red = new Uint8Array([255, 0, 0]);
const green = new Uint8Array([0, 255, 0]);
const blue = new Uint8Array([0, 0, 255]);
const pallete = [red, green, blue];
const sensorAngle = Math.PI / 4;
const turnAngle = Math.PI / 4;
mouseIsPressed && line(pmouseX, pmouseY, mouseX, mouseY);
for (let i = 10; i--; ) {
this.dir = random(TWO_PI);
this.type = floor(random(3));
const angle = this.dir + dirOffset;
let x = floor(this.x + sensorOffset * cos(angle));
let y = floor(this.y + sensorOffset * sin(angle));
y = (y + height) % height;
const index = (x + y * width) * 4;
return +pixels[index] - pixels[index + 1] - pixels[index + 2];
return -pixels[index] + pixels[index + 1] - pixels[index + 2];
return -pixels[index] - pixels[index + 1] + pixels[index + 2];
const right = this.sense(+sensorAngle);
const center = this.sense(0);
const left = this.sense(-sensorAngle);
if (center < left || center < right) {
} else if (left > right) {
this.x = (this.x + width) % width;
this.y = (this.y + height) % height;
const index = (floor(this.x) + floor(this.y) * width)*4;
const oldPixels = pixels.subarray(index, index + 3);
const agentColor = pallete[this.type];
const newPixels = new Uint8Array([
oldPixels[0] | agentColor[0],
oldPixels[1] | agentColor[1],
oldPixels[2] | agentColor[2],
pixels.set(newPixels, index);
this.agents = Array(agentsNum)
.map((e) => new Agent());
this.agents.forEach((e) => e.updateDirection());
this.agents.forEach((e) => e.updatePosition());