hilbert-curve-latex.a68
1 COMMENT
2
3 @section Synopsis
4
5 Generate LaTeX code to plot a Hilbert curve.
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 COMMENT
41
42 INT points = 384;
43
44 PROC draw member = (INT n) VOID:
45 BEGIN INT d = points OVER (2 ^ n);
46 MODE POINT = STRUCT (INT x, y);
47 POINT origin := (d OVER 2, d OVER 2);
48 print (("\begin{picture}(", whole (points, 0), ", ", whole(points, 0), ")", newline));
49 go right (n);
50 print (("\end{picture}"));
51
52 PROC line to = (POINT end) VOID:
53 (POINT vector = (x OF end - x OF origin, y OF end - y OF origin);
54 print (("\put(", whole (x OF origin, 0), ", ", whole (y OF origin, 0), "){",
55 "\line(", whole(SIGN x OF vector, 0), ", ", whole (SIGN y OF vector, 0), "){",
56 whole (ENTIER sqrt (x OF vector ^ 2 + y OF vector ^ 2), 0), "}}", newline));
57 origin := end
58 );
59
60 PROC go up = (INT n) VOID:
61 IF n > 0
62 THEN go right (n - 1);
63 line to ((x OF origin + d, y OF origin));
64 go up (n - 1);
65 line to ((x OF origin, y OF origin + d));
66 go up (n - 1);
67 line to ((x OF origin - d, y OF origin));
68 go left (n - 1)
69 FI;
70
71 PROC go down = (INT n) VOID:
72 IF n > 0
73 THEN go left (n - 1);
74 line to ((x OF origin - d, y OF origin));
75 go down (n - 1);
76 line to ((x OF origin, y OF origin - d));
77 go down (n - 1);
78 line to ((x OF origin + d, y OF origin));
79 go right (n - 1)
80 FI;
81
82 PROC go left = (INT n) VOID:
83 IF n > 0
84 THEN go down (n - 1);
85 line to ((x OF origin, y OF origin - d));
86 go left (n - 1);
87 line to ((x OF origin - d, y OF origin));
88 go left (n - 1);
89 line to ((x OF origin, y OF origin + d));
90 go up (n - 1)
91 FI;
92
93 PROC go right = (INT n) VOID:
94 IF n > 0
95 THEN go up (n - 1);
96 line to ((x OF origin, y OF origin + d));
97 go right (n - 1);
98 line to ((x OF origin + d, y OF origin));
99 go right (n - 1);
100 line to ((x OF origin, y OF origin - d));
101 go down (n - 1)
102 FI;
103 SKIP
104 END;
105
106 draw member(4)
© 2002-2024 J.M. van der Veer (jmvdveer@xs4all.nl)
|