xxxxxxxxxx
156
var res = 8;
var rows, cols;
var points;
var zoff = 0;
//For interpolated version set to true;
var lerped = true;
function setup(){
createCanvas(600, 600, P2D);
rows = 1+ width/res;
cols = 1+ height/res;
points = [];
}
function draw(){
background('#CC7E85');
for(var k = 0; k < cols; k++){
points[k] = [];
for(var w = 0; w < rows; w++){
points[k][w] = noise(k/10, w/10, zoff);
}
}
zoff += 0.01;
for(var i = 0; i < cols -1; i++){
for(var j = 0; j < rows -1; j++){
var x = j * res;
var y = i * res;
let a, b, c, d;
if(lerped){
a = createVector(interpolate(x, (j + 1) * res, points[i][j], points[i][j+1]), y);
b = createVector(x + res, interpolate(y, (i + 1) * res, points[i][j+1], points[i+1][j+1]));
c = createVector(interpolate((j + 1) * res, x, points[i+1][j+1], points[i+1][j]), y + res);
d = createVector(x, interpolate((i + 1) * res, y, points[i+1][j], points[i][j]));
}else{
a = createVector(x + res*0.5, y);
b = createVector(x + res, y + res*0.5);
c = createVector(x + res*0.5, y + res);
d = createVector(x, y + res*0.5);
}
var state = parseInt(roundToOne(points[i][j]) + "" + roundToOne(points[i][j+1]) + "" + roundToOne(points[i+1][j+1]) + "" + roundToOne(points[i+1][j]), 2);
fill('#CF4D6F');
strokeWeight(0);
beginShape();
switch(state){
case 1:
vertex(x, (i+1) * res);
vertex(d.x, d.y);
vertex(c.x, c.y);
break;
case 2:
vertex((j+1) * res, (i+1) * res);
vertex(b.x, b.y);
vertex(c.x, c.y);
break;
case 3:
vertex((j+1) * res, (i+1) * res);
vertex(x, (i+1) * res);
vertex(d.x, d.y);
vertex(b.x, b.y);
break;
case 4:
vertex(a.x, a.y);
vertex(b.x, b.y);
vertex((j+1) * res, y);
break;
case 5:
vertex(a.x, a.y);
vertex((j+1) * res, y);
vertex(b.x, b.y);
vertex(c.x, c.y);
vertex(x, (i+1) * res);
vertex(d.x, d.y);
break;
case 6:
vertex(a.x, a.y);
vertex((j+1) * res, y);
vertex((j+1) * res, (i+1) * res);
vertex(c.x, c.y);
break;
case 7:
vertex(a.x, a.y);
vertex((j+1) * res, y);
vertex((j+1) * res, (i+1) * res);
vertex(x, (i+1) * res);
vertex(d.x, d.y);
break;
case 8:
vertex(a.x, a.y);
vertex(d.x, d.y);
vertex(x, y);
break;
case 9:
vertex(x, y);
vertex(a.x, a.y);
vertex(c.x, c.y);
vertex(x, (i+1) * res);
break;
case 10:
vertex(x, y);
vertex(a.x, a.y);
vertex(b.x, b.y);
vertex((j+1) * res, (i+1) * res);
vertex(c.x, c.y);
vertex(d.x, d.y);
break;
case 11:
vertex(x, y);
vertex(a.x, a.y);
vertex(b.x, b.y);
vertex((j+1) * res, (i+1) * res);
vertex(x, (i+1) * res);
break;
case 12:
vertex(x, y);
vertex((j+1) * res, y);
vertex(b.x, b.y);
vertex(d.x, d.y);
break;
case 13:
vertex(x, y);
vertex((j+1) * res, y);
vertex(b.x, b.y);
vertex(c.x, c.y);
vertex(x, (i+1) * res);
break;
case 14:
vertex(x, y);
vertex((j+1) * res, y);
vertex((j+1) * res, (i+1) * res);
vertex(c.x, c.y);
vertex(d.x, d.y);
break;
case 15:
vertex(x, y);
vertex((j+1) * res, y);
vertex((j+1) * res, (i+1) * res);
vertex(x, (i+1) * res);
break;
}
endShape();
}
}
}
function roundToOne(num){
if(num > 0.5) return 1;
return 0;
}
function interpolate(before, after, beforePoint, afterPoint){
return before + (after - before) * ((0.5 - beforePoint) / (afterPoint - beforePoint));
}