xxxxxxxxxx
283
let targets, ants;
let numtargets, numants;
let cellsize;
let ease = 0.005;
const numiterations = 1000;
let iteration = 0;
let border;
function setup() {
createCanvas(800, 800);
pixelDensity(1);
border = width * 0.05;
background(220);
numtargets = 20;
numants = 50;
cellsize = 2; //5;
targets = [];
ants = [];
noStroke();
for (let i = 0; i < numtargets; i++) {
let x = random(border, width - 1 - 2*border - cellsize) | 0;
let y = random(border, height - 1 - 2*border - cellsize) | 0;
targets.push({ x: x, y: y });
fill(color(0, 220, 0));
square(x, y, cellsize);
}
for (let i = 0; i < numants; i++) {
let tour = [];
while (tour.length < numtargets) {
let next = random(numtargets) | 0;
if (tour.indexOf(next) < 0) tour.push(next);
}
tour.push(tour[0]);
let x = targets[tour[0]].x;
let y = targets[tour[0]].y;
ants.push({
tour: tour,
idx: 0,
x: x,
y: y,
color: color(20, 20, 20, 40), //255, 0, 255, 40),
steps: 0,
});
}
while (iteration < numiterations) {
// let gfx = createGraphics(width, height);
for (let t of targets) {
fill(color(0, 220, 0));
square(t.x, t.y, cellsize);
}
let tdone = 0;
while (tdone < ants.length) {
for (let a of ants) {
if (a.done) {
fill(255);
square(a.x, a.y, cellsize);
tdone++;
// console.log(a.steps);
} else {
fill(a.color);
square(a.x, a.y, cellsize);
let nextID = a.idx + 1;
if (nextID > a.tour.length - 1) {
a.done = true;
continue;
} //nextID = 0;
let nextX = targets[a.tour[nextID]].x;
let nextY = targets[a.tour[nextID]].y;
let dx = nextX - a.x;
let dy = nextY - a.y;
a.x += dx * ease + random(-1,1);
a.y += dy * ease + random(-1,1);
a.steps++;
// console.log(a.x, a.y, nextX, nextY, dx, dy, nextID, a.idx);
if (dx < 0.01 && dy < 0.01) a.idx = nextID;
}
}
}
// done with iteration
console.log(`Iteration ${iteration} complete`);
// for (let a of ants) console.log(a.steps);
// noLoop();
iteration++;
if (iteration < numiterations-1) {
fill(40,40,40,40);
rect(0,0,border,height);
rect(border,0,width-border,border);
rect(width-border,border,border,height-border);
rect(border,height-border,width-border,border);
dither(null);
// fill(220, 220, 220, 10);
// rect(0, 0, width, height);
}
// image(gfx, 0, 0);
// gfx.remove();
}
console.log("done");
}
function draw() {
noLoop();
// background(20);
// for (let t of targets) {
// fill(color(0, 220, 0));
// square(t.x, t.y, cellsize);
// }
// let tdone = 0;
// for (let a of ants) {
// if (a.done) {
// fill(255);
// square(a.x, a.y, cellsize);
// tdone++;
// // console.log(a.steps);
// } else {
// fill(a.color);
// square(a.x, a.y, cellsize);
// let nextID = a.idx + 1;
// if (nextID > a.tour.length - 1) {
// a.done = true;
// continue;
// } //nextID = 0;
// let nextX = targets[a.tour[nextID]].x;
// let nextY = targets[a.tour[nextID]].y;
// let dx = nextX - a.x;
// let dy = nextY - a.y;
// a.x += dx * ease; // + random(-1,1);
// a.y += dy * ease; // + random(-1,1);
// a.steps++;
// // console.log(a.x, a.y, nextX, nextY, dx, dy, nextID, a.idx);
// if (dx < 0.01 && dy < 0.01) a.idx = nextID;
// }
// }
// // done with iteration
// if (tdone == ants.length) {
// console.log(`Iteration ${iteration} complete`);
// // for (let a of ants) console.log(a.steps);
// // noLoop();
// iteration++;
// if (iteration < numiterations) {
// fill(220, 220, 220, 50);
// rect(0, 0, width, height);
// }
// }
// if (iteration >= numiterations) {
// console.log("done");
// noLoop();
// }
}
// https://p5js.org/reference/#/p5/drawingContext
function drawShadow(x, y, b, c, g = null) {
if (g == null) {
drawingContext.shadowOffsetX = x;
drawingContext.shadowOffsetY = y;
drawingContext.shadowBlur = b; // * scale;
drawingContext.shadowColor = c;
} else {
g.drawingContext.shadowOffsetX = x;
g.drawingContext.shadowOffsetY = y;
g.drawingContext.shadowBlur = b; // * scale;
g.drawingContext.shadowColor = c;
}
}
function index(g, x, y) {
if (g == null) return (x + y * width) * 4;
else return (x + y * g.width) * 4;
}
function DivideBy255(value) {
return (value + 1 + (value >> 8)) >> 8;
}
function dither(g) {
let referenceSize = 1000;
let hasMaxSize = false;
if (g == null) {
let _scale = Math.ceil(1, map(width, 0, referenceSize, 0, 1, hasMaxSize));
loadPixels();
for (let y = 0; y < height - _scale; y++) {
for (let x = _scale; x < width - _scale; x++) {
let oldr = pixels[index(g, x, y)];
let oldg = pixels[index(g, x, y) + 1];
let oldb = pixels[index(g, x, y) + 2];
let newr = (DivideBy255(oldr) * 255) | 0;
let newg = (DivideBy255(oldg) * 255) | 0;
let newb = (DivideBy255(oldb) * 255) | 0;
pixels[index(g, x, y)] = newr;
pixels[index(g, x, y) + 1] = newg;
pixels[index(g, x, y) + 2] = newb;
for (let _y = 1; _y <= _scale; _y++) {
for (let _x = 1; _x <= _scale; _x++) {
pixels[index(g, x + _x, y)] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x + _x, y) + 1] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x + _x, y) + 2] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x - _x, y + _y)] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x - _x, y + _y) + 1] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x - _x, y + _y) + 2] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x, y + _y)] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x, y + _y) + 1] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x, y + _y) + 2] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x + _x, y + _y)] += ((oldr - newr) * 1) >> 4;
pixels[index(g, x + _x, y + _y) + 1] += ((oldr - newr) * 1) >> 4;
pixels[index(g, x + _x, y + _y) + 2] += ((oldr - newr) * 1) >> 4;
}
}
}
}
updatePixels();
} else {
let _scale = Math.ceil(1, map(g.width, 0, referenceSize, 0, 1, hasMaxSize));
g.loadPixels();
for (let y = 0; y < g.height - _scale; y++) {
for (let x = _scale; x < g.width - _scale; x++) {
let oldr = g.pixels[index(g, x, y)];
let oldg = g.pixels[index(g, x, y) + 1];
let oldb = g.pixels[index(g, x, y) + 2];
let newr = (DivideBy255(oldr) * 255) | 0;
let newg = (DivideBy255(oldg) * 255) | 0;
let newb = (DivideBy255(oldb) * 255) | 0;
g.pixels[index(g, x, y)] = newr;
g.pixels[index(g, x, y) + 1] = newg;
g.pixels[index(g, x, y) + 2] = newb;
for (let _y = 1; _y <= _scale; _y++) {
for (let _x = 1; _x <= _scale; _x++) {
g.pixels[index(g, x + _x, y)] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x + _x, y) + 1] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x + _x, y) + 2] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x - _x, y + _y)] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x - _x, y + _y) + 1] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x - _x, y + _y) + 2] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x, y + _y)] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x, y + _y) + 1] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x, y + _y) + 2] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x + _x, y + _y)] += ((oldr - newr) * 1) >> 4;
g.pixels[index(g, x + _x, y + _y) + 1] += ((oldr - newr) * 1) >> 4;
g.pixels[index(g, x + _x, y + _y) + 2] += ((oldr - newr) * 1) >> 4;
}
}
}
}
g.updatePixels();
}
}