xxxxxxxxxx
105
let grid=[];
let resolution=1;
let step_length = 1;
let numX, numY;
function setup() {
createCanvas(500, 500);
numX = int(width/resolution);
numY = int(height/resolution);
for (let i =0; i< numX; i++) {
// have to create it like this apparently.
grid[i]=[];
for (let j = 0; j<numY; j++) {
// populating it with random numbers
//grid[i][j]=random();
//scaled_x = i * 0.005;
//scaled_y = j * 0.005;
scaled_x = i * 0.005;
scaled_y = j * 0.005;
// get our noise value, between 0.0 and 1.0
noise_val = noise(scaled_x, scaled_y);
// translate the noise value to an angle (betwen 0 and 2 * PI)
angle = map(noise_val, 0.0, 1.0, 0.0, PI * 2.0);
grid[i][j] = angle;
}
}
background(255,253,242);
stroke(0,25);
//strokeWeight(2);
// okay now; let's try the curve drawing!
for (i=0; i<600; i++) {
//draw_curve(3000,random()*width,random()*height);
draw_curve(3000,random()*width,random()*height);
}
}
function draw_curve(num_steps, startX, startY) {
x = startX;
y = startY;
print(x,y);
// basically does a walk where each step in the walk is given
// by these angles.
origcolor = random(255);
stroke(origcolor,25);
if (inBox(x,y)) {
// starting inside the box
stroke(0,random(120),random(120),25)
}
thickness = 1;
for (let i =0; i<num_steps;i++) {
// pick a color at random if its inside a box
// a fun thing to do.
//if (x > 100 && x < 200) {
// stroke(0,0,random(255),25)
//}
angle = grid[gridPoint(x)][gridPoint(y)];
deltaX = step_length*cos(angle);
deltaY = step_length*sin(angle);
//print(x);
//print(y);
// have the thickness undergo a random walk.
thickness = thickness + random() - 0.5;
strokeWeight(thickness);
line(x,y,x+deltaX,y+deltaY);
//circle(x,y,thickness);
if (inBox(x+deltaX,y+deltaY) && !inBox(x,y)) {
// entered box
stroke(0,random(120),random(120),25);
}
if (!inBox(x+deltaX,y+deltaY) && inBox(x,y)) {
// leaving box
stroke(origcolor,25);
}
x = x+deltaX;
y = y+deltaY;
}
}
function gridPoint(val) {
// note this does assume an isotropic grid.
return max(0,min(numX-1,int(val/resolution)));
}
function inBox(x,y) {
if (x > 150 && x < 350 && y > 150 && y < 350) {
return true;
}
else {
return false;
}
}
function draw() {
//line(0,0,100,100);
}