xxxxxxxxxx
99
let flowfield;
let cellSize = 20;
let minDist = cellSize / 2;
let noiseScale = .1;
let radius = 3;
let speed = minDist;
let s;
let distCells;
let dw, dh;
function smoothstep(edge0, edge1, x) {
if (x < 0)
return edge0;
if (x > 1)
return edge1;
return lerp(edge0, edge1, x * x * (3 - 2 * x));
}
function mod(a, b) {
return a - b*floor(a/b);
}
function setup() {
createCanvas(600, 600);
background(125);
//noStroke();
flowfield = new Flowfield(width / cellSize, height / cellSize);
dw = ceil(width / minDist);
dh = ceil(height / minDist);
distCells = new Array(dw * dh);
for (let i = 0; i < distCells.length; i++) {
distCells[i] = [];
}
s = new Streamline(width/2, height/2);
let d = cellSize / speed * 0.6;
stroke(0);
for (let x = 0; x < flowfield.width; x++) {
for (let y = 0; y < flowfield.height; y++) {
let px = (x + 0.5) * cellSize;
let py = (y + 0.5) * cellSize;
let f = flowfield.getField(x, y);
line(px, py, px + f.x * d, py + f.y * d);
}
}
}
function addPoint(p) {
let x = mod(ceil(p.x / minDist), dw);
let y = mod(ceil(p.y / minDist), dh);
distCells[x + y * dw].push(p);
}
function isValid(p) {
let xp = ceil(p.x / minDist);
let yp = ceil(p.y / minDist);
let d = Infinity;
for (let x = xp - 1; x <= xp + 1; x++) {
for (let y = yp - 1; y <= yp + 1; y++) {
let cx = mod(x, dw);
let cy = mod(y, dh);
let cell = distCells[cx + cy * dw];
for (let i = 0; i < cell.length; i++) {
let dc = dist(cell[i].x, cell[i].y, p.x, p.y);
d = min(d, dc);
if (d < minDist)
return false;
}
}
}
return true;
}
function draw() {
//if (frameCount % 2 == 0)
//background(220, 120, 20, 1);
noStroke();
fill(255);
s.update();
s.draw();
}