This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Hilbert.pde
89 lines (76 loc) · 1.82 KB
/
Hilbert.pde
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
// Hilbert Curve
// Coding in the Cabana
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/CodingInTheCabana/003-hilbert-curve.html
// https://youtu.be/
// Processing Sketch: https://github.com/CodingTrain/website/tree/master/CodingInTheCabana/Cabana_003_Hilbert_Curve/Processing
// p5js Sketch: https://editor.p5js.org/codingtrain/sketches/LPf9PLmp
int order = 9;
int N = int(pow(2, order));
int total = N * N;
PVector[] path = new PVector[total];
void setup() {
size(1024, 1024);
colorMode(HSB, 360, 255, 255);
background(0);
float len = width / N;
for (int i = 0; i < total; i++) {
path[i] = hilbert(i);
path[i].mult(len);
path[i].add(len/2, len/2);
}
}
int counter = 0;
void draw() {
background(0);
stroke(255);
strokeWeight(1);
noFill();
//beginShape();
for (int i = 1; i < counter; i++) {
float h = map(i, 0, path.length, 0, 360);
stroke(h, 255, 255);
line(path[i].x, path[i].y, path[i-1].x, path[i-1].y);
}
//endShape();
counter+=50;
if (counter >= path.length) {
counter = 0;
}
//strokeWeight(4);
//for (int i = 0; i < path.length; i++) {
// point(path[i].x, path[i].y);
// text(i, path[i].x+5, path[i].y);
//}
}
PVector hilbert(int i) {
PVector[] points = {
new PVector(0, 0),
new PVector(0, 1),
new PVector(1, 1),
new PVector(1, 0)
};
int index = i & 3;
PVector v = points[index];
for (int j = 1; j < order; j++) {
i = i >>> 2;
index = i & 3;
float len = pow(2, j);
if (index == 0) {
float 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) {
float temp = len - 1 - v.x;
v.x = len - 1 - v.y;
v.y = temp;
v.x += len;
}
}
return v;
}