xxxxxxxxxx
85
//https://tylerxhobbs.com/essays/2020/flow-fields
let numColumns;
let numRows;
let resolution;
let defaultAngle;
let steps = 100;
let grid = [];
function setup() {
createCanvas(400, 400);
let left = int(width * -0.5);
let right = int(width * 1.5);
let top = int(height * -0.5);
let bottom = int(height * -1.5);
resolution = int(width * 0.05);
numColumns = (right - left) / resolution;
numRows = (top - bottom) / resolution;
defaultAngle = PI * 0.4;
background(220);
console.log(numColumns, numRows);
for (x = 0; x < numColumns; x++) {
grid[x] = [];
for (y = 0; y < numRows; y++) {
let angle = (y / numRows) * PI;
grid[x][y] = angle;
let lineLength = resolution / 2;
let a = cos(angle) * lineLength;
let o = sin(angle) * lineLength;
line(
x * resolution,
y * resolution,
x * resolution + a,
y * resolution + o
);
circle(x * resolution, y * resolution, 3);
}
}
// console.log(grid)
for(let c = 0; c < 100; c++){
makeCurve(random(400), 100, left, top);
}
}
function makeCurve(x,y,left, top) {
// make curve
let curveX = x;
let curveY = y;
noFill();
stroke(255, 0, 0);
strokeWeight(3);
beginShape();
for (s = 0; s < steps; s++) {
curveVertex(curveX, curveY);
let xOffset = curveX - left;
let yOffset = curveY - top;
let columnIndex = int(xOffset / resolution);
let rowIndex = int(yOffset / resolution);
//check bounds...
if (grid[columnIndex][rowIndex]) {
let gridAngle = grid[columnIndex][rowIndex];
let xStep = steps * cos(gridAngle);
let yStep = steps * sin(gridAngle);
curveX += xStep;
curveY += yStep;
}
}
endShape();
}
// function draw() {
// }