xxxxxxxxxx
79
let points = []
let n = 1
let rows = 3;
let k = 0
let max_d = 110;
let scl;
let dSlider;
let noise;
let ZSlider;
let res = 2;
let Width = 400
function create2DArray(rows, cols) {
return new Array(rows).fill().map(() => new Array(cols).fill(0));
}
function setup() {
createCanvas(Width, Width);
points = []
noise = create2DArray(width, height);
scl = (width) / rows
zSlider = createSlider(0, width, width/2, .1);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < rows; j++) {
let x = random((i*scl)+k, ((i+1)*scl)-k);
let y = random((j*scl)+k, ((j+1)*scl)-k);
let z = random(width);
points.push(createVector(x, y));
}
}
dSlider = createSlider(50, scl*3, scl*3, 5);
for (let i = 0; i < width; i += res) {
for (let j = 0; j < height; j += res) {
let ds = worley(i, j).slice(0, 3).map(d => map(d, 0, scl*3, 0, 255));
noise[i][j] = ds[0]
}
}
}
function draw() {
noStroke();
for (let i = 0; i < width; i += res) {
for (let j = 0; j < height; j += res) {
let d = noise[i][j]+frameCount
let k = (d%10)-5
fill(k <= 0 ? 255 : 0)
square(i, j, res)
}
}
}
function worley(x, y) {
const z = zSlider.value()
let space = createVector(x, y);
// let closest = closest_point(space);
// let d = space.dist(closest);
let distances = points.map(p => p.dist(space)).sort((a,b) => (a-b));
return (distances)
}
function index(i, j) {
return i * rows + j
}
function closest_point(p) {
let x = floor(p.x / scl);
let y = floor(p.y / scl);
let closest = points[0].copy()
let min_d = Infinity
for (const point of points) {
let d = dist(point.x, point.y, p.x, p.y);
if (d < min_d) {
min_d = d;
closest = point
}
}
return closest
}