-
Notifications
You must be signed in to change notification settings - Fork 0
/
hilbert.js
155 lines (140 loc) · 3.72 KB
/
hilbert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let order = 1; // Initial order
let N = Math.floor(Math.pow(2, order)); // grids
let total = N*N; // total coordinates
let path = new Array(total);
let flag = false;
let img;
function setup() {
let cnv = createCanvas(windowHeight, windowHeight);
cnv.style('display', 'block');
cnv.style('margin-left', 'auto');
cnv.style('margin-right', 'auto');
colorMode(RGB);
for (let i = 0; i < total; i++){
path[i] = hilbert(i);
let len = width / N; // length of line segment of the curve
path[i].mult(len);
path[i].add(len/2,len/2);
}
}
function loadImageFromLocalStorage(callback) {
const imageData = localStorage.getItem(key);
if (!imageData) {
console.error('Image data not found in localStorage for key:', key);
return null;
}
const image = loadImage(imageData, () => {
callback(image);
});
}
document.addEventListener('render', () => {
order = 8;
flag = true;
path = new Array(total);
N = Math.floor(Math.pow(2, order));
total = N*N;
for (let i = 0; i < total; i++){
path[i] = hilbert(i);
let len = width / N; // length of line segment of the curve
path[i].mult(len);
path[i].add(len/2,len/2);
}
loadImageFromLocalStorage((image) => {
img = image;
localStorage.removeItem(key);
console.log(img);
});
});
let counter = 0;
function draw() {
background(255);
if(!flag){
stroke(0);
strokeWeight(2);
noFill();
// beginShape();
for (let i = 1; i < counter; i++){
let h = map(i, 0, path.length, 0, 360);
//stroke(h,255,255);
if(path[i]){
line(path[i].x, path[i].y, path[i-1].x, path[i-1].y);
}
}
// endShape();
if(counter < path.length){
// counter += 0.01*order*6.7;
counter += 0.08 * order**2
} else{
// Reset for next order
order = (order+1)%9;
// if (order > 8) {
// noLoop(); // Stop after order 8
// return;
// }
N = Math.floor(Math.pow(2, order));
total = N*N;
path = new Array(total);
counter = 0;
for (let i = 0; i < total; i++){
path[i] = hilbert(i);
let len = width / N; // length of line segment of the curve
path[i].mult(len);
path[i].add(len/2,len/2);
}
}
}else{
if(img){
background(255);
// Draw Hilbert curve incrementally
noFill();
strokeWeight(1);
for (let i = 1; i < min(counter, path.length); i++) {
// Get the corresponding pixel color from the image
let imgX = int(map(path[i].x, 0, width, 0, img.width - 1));
let imgY = int(map(path[i].y, 0, height, 0, img.height - 1));
let col = img.get(imgX, imgY);
stroke(col);
// Draw line segment of the curve
let prevX = map(path[i-1].x, 0, width, 0, img.width);
let prevY = map(path[i-1].y, 0, height, 0, img.height);
let x = map(path[i].x, 0, width, 0, img.width);
let y = map(path[i].y, 0, height, 0, img.height);
line(prevX, prevY, x, y);
}
if (counter < path.length) {
counter += 550;
}
}
}
}
function hilbert(i){
let points = [
createVector(0,0),
createVector(0,1),
createVector(1,1),
createVector(1,0)
];
let index = i & 3;
let v = points[index]; // get point from first order hilbert's curve
for(let k = 1; k < order; k++){
i = i >>> 2;
index = i & 3; // grid to place in
let len = pow(2,k);
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.y += len;
v.x += 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;
}