-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hilbert.wxm
282 lines (231 loc) · 8.93 KB
/
Hilbert.wxm
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 22.04.0 ] */
/* [wxMaxima: input start ] */
/* COPYRIGHT NOTICE
Copyright (C) 2009 Mario Rodriguez Riotorto
This program is free software; you can redistribute
it and/or modify it under the terms of the
GNU General Public License as published by
the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at
http://www.gnu.org/copyleft/gpl.html
*/
/*
This is a small package for turtle graphics in Maxima.
Internal variables concerning object 'turtle':
- xcor
- ycor
- heading
- pendownp
Functions for object 'turtle':
- forward(n)
- arc(angle,radius)
- left(n)
- right(n)
- back(n)
- setxy(n,n)
- setx(n)
- sety(n)
- home()
- towards(number,number)
- penup()
- pendown()
- repeat(n, ....)
- to(name, ....)
- setpencolor(number)
- setpensize(number)
- setheading(angle)
- make(variable, number)
- when(condition, ..., ...)
- ifelse(condition,[...],[...])
*/
put('turtle, 1, 'version) $
if not get('draw,'version) then load("draw") $
turtle([logo]):=
block([codraw:[points_joined = true,
transparent = true,
point_type = dot],
numer: true,
to_routines, var,
_segment,_forward,_backward,_jump,_substvar,_parse],
var[heading]: 90,
var[xcor]: 0,
var[ycor]: 0,
var[pendownp]: true,
local(_segment,_forward,_backward,_jump,_substvar,_parse),
_segment(p1,p2):=
codraw: endcons(points([p1,p2]), codraw),
_forward(d):=
block([rad: var[heading]*%pi/180],
_segment([var[xcor], var[ycor]],
[var[xcor], var[ycor]]:
[var[xcor], var[ycor]]+d*[cos(rad), sin(rad)])),
_backward(d):=
block([rad: var[heading]*%pi/180+%pi],
_segment([var[xcor], var[ycor]],
[var[xcor], var[ycor]]:
[var[xcor], var[ycor]]+d*[cos(rad), sin(rad)])),
_jump(x,y):=
block([pos2:[var[xcor], var[ycor]]],
if not x = false then pos2[1]: x,
if not y = false then pos2[2]: y,
_segment([var[xcor], var[ycor]], pos2),
[var[xcor], var[ycor]]: pos2),
_substvar(expr):=
subst(map("=",
flatten(sublist(arrayinfo(var),listp)),
flatten(listarray(var))),
expr),
_parse(l):=
for s in l do
(if not atom(s) and op(s) = 'make
then s: make(args(s)[1], _substvar(args(s)[2]))
else if not atom(s) and op(s) # 'repeat and op(s) # 'to
then s: _substvar(s),
if not atom(s) and op(s) = 'forward
then _forward(ev(first(args(s)), nouns))
elseif not atom(s) and op(s) = 'back
then _backward(ev(first(args(s)), nouns))
elseif not atom(s) and op(s) = 'right
then var[heading]: var[heading] - ev(first(args(s)),nouns)
elseif not atom(s) and op(s) = 'left
then var[heading]: var[heading] + ev(first(args(s)),nouns)
elseif not atom(s) and op(s) = 'setxy
then _jump(ev(args(s)[1],nouns),
ev(args(s)[2],nouns))
elseif not atom(s) and op(s) = 'setx
then _jump(ev(args(s)[1],nouns), false)
elseif not atom(s) and op(s) = 'sety
then _jump(false, ev(args(s)[1],nouns))
elseif not atom(s) and op(s) = 'home
then _jump(0,0)
elseif not atom(s) and op(s) = 'towards
then var[heading]:
block([u,v,fu,fv,a],
[u,v]: ev(args(s),nouns) - [var[xcor], var[ycor]],
[fu,fv]: float([u,v]),
if fu=0.0
then if fv>0.0
then 90
elseif fv<0.0
then -90
else var[heading]
else (a: atan(v/u)*180/%pi,
if fu>0.0
then a
else 180+a))
elseif not atom(s) and op(s) = 'penup
then (var[pendownp]: true,
codraw: endcons(points_joined = false, codraw))
elseif not atom(s) and op(s) = 'pendown
then (var[pendownp]: false,
codraw: endcons(points_joined = true, codraw))
elseif not atom(s) and op(s) = 'repeat
then
for i:1 thru args(s)[1] do
_parse(rest(args(s)))
elseif not atom(s) and op(s) = 'to
then to_routines[args(s)[1]]: rest(args(s))
elseif not atom(s) and op(s) = 'setpencolor
then block([colores:["black", "blue", "green", "cyan", "red",
"magenta", "yellow", "white", "brown",
"#d2b48c"/*tan*/, "forest-green", "aquamarine",
"salmon", "purple", "orange", "grey"]],
codraw: endcons(color = colores[ev(mod(args(s)[1],
length(colores))+1, nouns)],
codraw))
elseif not atom(s) and op(s) = 'setpensize
then codraw: endcons(line_width = round(abs(ev(args(s)[1],nouns))), codraw)
elseif not atom(s) and op(s) = 'setheading
then var[heading]: ev(first(args(s)),nouns)
elseif not atom(s) and op(s) = 'arc
then codraw: endcons(ellipse(var[xcor],
var[ycor],
ev(args(s)[2],nouns),
ev(args(s)[2],nouns),
var[heading],
ev(args(s)[1],nouns)),
codraw)
elseif not atom(s) and op(s) = 'when and is(ev(args(s)[1],nouns))
then _parse(rest(args(s)))
elseif not atom(s) and op(s) = 'ifelse
then if is(ev(args(s)[1],nouns))
then _parse(part(s,2))
else _parse(part(s,3))
elseif not atom(s) and listp(to_routines[op(s)])
then if length(to_routines[op(s)][1]) > 0
then /* routine with parameters */
_parse(subst(map("=", to_routines[op(s)][1], args(s)),
rest(to_routines[op(s)])))
else /* routine without parameters */
_parse(to_routines[op(s)])
elseif not atom(s) and op(s) = 'make
then var[args(s)[1]]: ev(args(s)[2],nouns)
),
_parse(args(logo)),
remarray(to_routines, var),
codraw)$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
set_draw_defaults(
terminal = svg,
dimensions = [350,350],
proportional_axes = xy) $
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
showtime:true$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
/* Hilbert curve */
/* Around 18 s for lhilbert(6,10) */
wxdraw2d(
turtle(
to(lhilbert,[depth,side],
ifelse(depth=0,
[
],
[
left(90),
rhilbert(depth-1,side),
forward(side),
right(90),
lhilbert(depth-1,side),
forward(side),
lhilbert(depth-1,side),
right(90),
forward(side),
rhilbert(depth-1,side),
left(90)
]
)
),
to(rhilbert,[depth,side],
ifelse(depth=0,
[
],
[
right(90),
lhilbert(depth-1,side),
forward(side),
left(90),
rhilbert(depth-1,side),
forward(side),
rhilbert(depth-1,side),
left(90),
forward(side),
lhilbert(depth-1,side),
right(90)
]
)
),
lhilbert(6,10)
)
);
/* [wxMaxima: input end ] */
/* Old versions of Maxima abort on loading files that end in a comment. */
"Created with wxMaxima 22.04.0"$