Created
February 6, 2020 03:31
-
-
Save sasaki-shigeo/80741351cb875f79b23ab8173c4220c3 to your computer and use it in GitHub Desktop.
Hilbert Curve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float unit = 10; | |
void setup() { | |
size(500, 500); | |
noLoop(); | |
} | |
void draw() { | |
translate(10, 10); | |
hilbert(20, 20, 480, 4); | |
} | |
void hilbert(float x, float y, float sz, int depth) { | |
unit = sz / pow(2, depth); | |
pushMatrix(); | |
translate(x, y); | |
hilbertL(depth); | |
popMatrix(); | |
} | |
void forward() { | |
line(0, 0, unit, 0); | |
translate(unit, 0); | |
delay(10); | |
} | |
void hilbertL(int n) { | |
if (n <= 0) return; | |
rotate(PI/2); | |
hilbertR(n-1); | |
forward(); | |
rotate(-PI/2); | |
hilbertL(n-1); | |
forward(); | |
hilbertL(n-1); | |
rotate(-PI/2); | |
forward(); | |
hilbertR(n-1); | |
rotate(PI/2); | |
} | |
void hilbertR(int n) { | |
if (n <= 0) return; | |
rotate(-PI/2); | |
hilbertL(n-1); | |
forward(); | |
rotate(PI/2); | |
hilbertR(n-1); | |
forward(); | |
hilbertR(n-1); | |
rotate(PI/2); | |
forward(); | |
hilbertL(n-1); | |
rotate(-PI/2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See Wolfram Alpha in detail about the Hilbert Curves.
The algorithm of this program is the L-System code:
L = +RF-LFL-FR+
;R = -LF+RFR+FL-
; whereF
means go forward;+
means turn left 90°ree;;-
means turn right 90°ree;.