xxxxxxxxxx
124
let points = [];
let size = 800;
let pointCount = 20;
let squareCount = 40;
let squareSize = size / squareCount;
let total = 0;
let count = 0;
let z = 0;
let stepZ = 1;
let values = Array(squareCount + 1).fill().map(() => Array(squareCount + 1).fill(0));
const vertices = [
[],
[5, 6, 7],
[3, 4, 5],
[3, 4, 5, 6, 7],
[1, 2, 3],
[1, 2, 3, 5, 6, 7],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7],
[0, 1, 7],
[0, 1, 5, 6, 7],
[0, 1, 3, 4, 5, 7],
[0, 1, 3, 4, 5, 6, 7],
[0, 1, 2, 3, 7],
[0, 1, 2, 3, 5, 6, 7],
[0, 1, 2, 3, 4, 5, 7],
[0, 1, 2, 3, 4, 5, 6, 7]
]
function setup() {
createCanvas(size, size);
pixelDensity(1);
for (let i = 0; i < pointCount; i++) {
points[i] = createVector(random(size), random(size), random(size));
}
noStroke();
fill(255, 0, 0, 20);
}
function draw() {
background(100, 100, 255)
for (let x = 0; x <= size; x += squareSize) {
for (let y = 0; y <= size; y += squareSize) {
strokeWeight(10);
let distances = [];
for (let i = 0; i < points.length; i++) {
let point = points[i];
let d = dist(x, y, z, point.x, point.y, point.z);
distances[i] = d;
}
let sortedDist = sort(distances);
let value = sortedDist[1]-sortedDist[0];
values[x / squareSize][y / squareSize] = value;
/*total+=value;
count++;
console.log(total/count);*/
}
}
z += stepZ;
if (z > size) {
stepZ = -stepZ;
}
let threshold = 0;
while (threshold < 200) {
for (let i = 0; i < values.length - 1; i++) {
for (let j = 0; j < values[0].length - 1; j++) {
let overThresholdA = 8 * (values[i][j] < threshold); //8
let overThresholdB = 4 * (values[i + 1][j] < threshold); //4
let overThresholdC = 1 * (values[i][j + 1] < threshold); //1
let overThresholdD = 2 * (values[i + 1][j + 1] < threshold); //2
let total = overThresholdA + overThresholdB + overThresholdC + overThresholdD;
beginShape();
for (let vert of vertices[total]) {
let coordinates = getPointFromLookup(i, j, vert)
vertex(coordinates[0], coordinates[1]);
}
endShape(CLOSE);
}
}
threshold += 20
}
}
function getPointFromLookup(i, j, vert) {
let x = i * squareSize;
let y = j * squareSize;
let step = squareSize / 2;
if (vert == 0) {
return [x, y];
}
if (vert == 1) {
return [x + step, y];
}
if (vert == 2) {
return [x + 2 * step, y];
}
if (vert == 3) {
return [x + 2 * step, y + step];
}
if (vert == 4) {
return [x + 2 * step, y + 2 * step];
}
if (vert == 5) {
return [x + step, y + 2 * step];
}
if (vert == 6) {
return [x, y + 2 * step];
}
if (vert == 7) {
return [x, y + step];
}
}