xxxxxxxxxx
93
let order = 7;
let N;
let total;
let path = [];
function setup() {
N = int(pow(2, order));
total = N * N;
createCanvas(512, 512)
colorMode(HSB,360,255,255);
background(0);
for (let i = 0; i < total; i++) {
path[i] = hilbert(i)
let len = width / N;
path[i].mult(len);
path[i].add(len / 2, len / 2)
}
}
function hilbert(i) {
points = [createVector(0, 0),
createVector(0, 1),
createVector(1, 1),
createVector(1, 0)
];
let index = i & 3;
let v = points[index];
for (let j = 1; j < order; j++) {
i = i >>> 2;
index = i & 3;
let len = pow(2,j);
if (index == 0) {
let temp = v.x;
v.x = v.y;
v.y = temp;
} else if (index == 1) {
v.y += len;
} else if (index == 2) {
v.x += len;
v.y += len;
} else if (index == 3) {
let temp = len-1 - v.x;
v.x = len-1 - v.y;
v.y = temp;
v.x += len;
}
}
return v;
}
let counter = 0;
function draw() {
background(0);
stroke(255);
strokeWeight(2);
noFill();
for (let i = 1; i < counter; i++) {
let h = float(map(i,0,path.length,0,255));
stroke(h,255,255);
line(path[i].x, path[i].y,path[i-1].x, path[i-1].y);
}
counter+=10;
if ( counter >= path.length) {
counter=0;
}
// for (let i = 0; i < path.length; i++) {
// strokeWeight(4);
// point(path[i].x, path[i].y);
// strokeWeight(1);
// text(i, path[i].x + 5, path[i].y - 3);
// }
}