xxxxxxxxxx
81
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Flow Field Following
class FlowField {
constructor(r) {
// How large is each "cell" of the flow field
this.resolution = r;
// Determine the number of columns and rows based on sketch's width and height
this.cols = width / this.resolution;
this.rows = height / this.resolution;
// A flow field is a two dimensional array of p5.Vectors
// We can't make 2D arrays, but this is sort of faking it
this.field = this.make2Darray(this.cols);
this.init();
}
make2Darray(n) {
let array = [];
for (let i = 0; i < n; i++) {
array[i] = [];
}
return array;
}
init() {
// Reseed noise so we get a new flow field every time
// Need to get noise working
noiseSeed(Math.floor(random(10000)));
let xoff = 0;
for (let i = 0; i < this.cols; i++) {
let yoff = 0;
for (let j = 0; j < this.rows; j++) {
let theta = map(noise(xoff, yoff), 0, 1, 0, TWO_PI);
//let theta = map(sin(xoff)+cos(yoff),-2,2,0,TWO_PI);
// Polar to cartesian coordinate transformation to get x and y components of the vector
this.field[i][j] = createVector(cos(theta), sin(theta));
yoff += 0.1;
}
xoff += 0.1;
}
}
// Draw every vector
display() {
for (let i = 0; i < this.cols; i++) {
for (let j = 0; j < this.rows; j++) {
this.drawVector(this.field[i][j], i * this.resolution, j * this.resolution, this.resolution - 2);
}
}
}
lookup(lookup) {
let column = Math.floor(constrain(lookup.x / this.resolution, 0, this.cols - 1));
let row = Math.floor(constrain(lookup.y / this.resolution, 0, this.rows - 1));
//println(lookup.x);
return this.field[column][row].copy();
}
// Renders a vector object 'v' as an arrow and a location 'x,y'
drawVector(v, x, y, scayl) {
push();
let arrowsize = 4;
// Translate to location to render vector
translate(x, y);
stroke(0, 100);
// Call vector heading function to get direction (note that pointing to the right is a heading of 0) and rotate
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
let len = v.mag() * scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0, 0, len, 0);
//line(len,0,len-arrowsize,+arrowsize/2);
//line(len,0,len-arrowsize,-arrowsize/2);
pop();
}
}