mandelbrot_set.anubis
13.4 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
read tools/basis.anubis
read tools/maybefloat.anubis
define Maybe(Float)
Int x / Maybe(Float) y
=
success(to_Float(x)) / y.
define Int
Word32 x / Int y
=
if (Maybe((Int,Int)))(to_Int(x)/y) is
{
failure then 0,
success(p) then if p is (q,r) then q
}.
define Int
Int x / Int y
=
if (Maybe((Int,Int)))(x/y) is
{
failure then 0,
success(p) then if p is (q,r) then q
}.
define Int
integral_part
(
Maybe(Float) x
) =
if x is
{
failure then 0,
success(f) then integral_part(f)
}.
define Maybe(Float)
Maybe(Float) x * Int y
=
x * success(to_Float(y)).
The computed image is just an array of Int, because for each point we just need the
number of iterations.
define (Maybe(Float) x,
Maybe(Float) y,
Int n) -> Int
compute_iterations_function
(
Maybe(Float) cx,
Maybe(Float) cy
) =
(Maybe(Float) x,
Maybe(Float) y,
Int n) |-f->
with x2 = x*x, y2 = y*y,
with sqnorm = x2 + y2,
if sqnorm > success(4.0) then n else
if n >= 256 then 0 else
f(x2 - y2 + cx, success(2.0)*x*y + cy, n+1).
define Int
compute_iterations
(
Maybe(Float) cx,
Maybe(Float) cy
) =
compute_iterations_function(cx,cy)(cx,cy,0).
define (Int x,
Int y,
Int n) -> Int
compute_iterations_function
(
Int cx,
Int cy
) =
(Int x,
Int y,
Int n) |-f->
with x2 = x*x, y2 = y*y,
with sqnorm = x2 + y2,
if sqnorm > 4 then n else
if n >= 256 then 0 else
f(x2 - y2 + cx, 2*x*y + cy, n+1).
define Int
compute_iterations
(
Int cx,
Int cy
) =
compute_iterations_function(cx,cy)(cx,cy,0).
define RGB
my_palette_1
(
Word8 t
) =
rgb(255-t,0,t).
define RGB
my_palette_2
(
Word8 t
) =
rgb((t&15)<<4,(t&63)<<2,t).
define RGB
my_palette_3
(
Word8 t
) =
rgb((t&7)<<5,((t>>3)&7)<<5,((t>>6)&3)<<6).
define RGB
my_palette_4
(
Word8 t
) =
rgb((t&7)<<5,((t>>3)&7)<<5,t).
define RGB
my_palette_5
(
Word8 t
) =
rgb(((t>>5)&7)<<5,((t>>2)&7)<<5,(t&3)<<6).
define One
paint_palette
(
HostWindow w,
Word8 -> RGB pal,
Word8 i
) =
with j = to_Word32(i),
paint_rectangle(w,rect(0,j,100,j+1),pal(i));
if i = 255 then unique else paint_palette(w,pal,i+1).
define (HostWindow w, List(Rectangle) l) -> One
palette_paint_method
(
Word8 -> RGB palette
) =
(HostWindow w, List(Rectangle) l) |->
paint_palette(w,palette,0).
define One
show_palette
(
Word8 -> RGB palette
) =
if open_host_window(rect((Int)0,100,100,356),
"Palette",
managed(not_resizable),
palette_paint_method(palette),
(HostWindow w, HostWindowEvent(One) e) |-> [],
identity) is
{
failure then print("Cannot open palette window.\n"),
success(pw) then show(pw)
}.
type ImageToolBox:
tool_box(HostWindow window,
HostImage buffer,
Word8 -> RGB palette,
ByteArray image,
Int window_width,
Int window_height,
Maybe(Float) center_x,
Maybe(Float) center_y,
Maybe(Float) scale,
Var(Bool) close_v).
define Int
to_Int
(
Maybe(Float) mbf
) =
if mbf is
{
failure then 0,
success(f) then integral_part(f)
}.
define One
draw_pixel
(
ImageToolBox tb,
Int position_x, // position in window (pixels)
Int position_y,
) =
if tb is tool_box(window,buffer,_,image,window_width,window_height,center_x,center_y,scale,c_v) then
//
// Compute the coordinates of pixel to be drawn in complex plane
//
with s = to_Int(scale),
x = to_Int(center_x) + ((position_x - window_width\2)) \ (s),
y = to_Int(center_y) + ((position_y - window_height\2)) \ (s),
//
// Compute the color (number of iterations)
//
iterations = compute_iterations(x,y),
//
// Draw the pixel
//
forget(put(image,position_x + window_width * position_y,truncate_to_Word8(iterations))).
define One
copy_line_to_buffer_and_window
(
ImageToolBox tb,
Int position_y,
Int position_x
) =
if tb is tool_box(window,buffer,pal,image,window_width,window_height,center_x,center_y,scale,c_v) then
if position_x >= window_width
then paint_image(window,
//rect(0,position_y,window_width,position_y+1),
rect(0,0,window_width,window_height),
0,0,
buffer)
else (with color = pal(
force_nth(position_x + window_width*position_y,image)
),
draw_pixel(buffer,truncate_to_Word32(position_x),truncate_to_Word32(position_y),color);
copy_line_to_buffer_and_window(tb,position_y,position_x+1)).
define One
draw_line
(
ImageToolBox tb,
Int position_x,
Int position_y
) =
if tb is tool_box(window,buffer,_,image,window_width,window_height,center_x,center_y,scale,c_v) then
if *c_v then unique else
if position_x >= window_width
then copy_line_to_buffer_and_window(tb,position_y,0) else
draw_pixel(tb,position_x,position_y);
draw_line(tb,position_x+1,position_y).
define One
draw_all_lines
(
ImageToolBox tb,
Int position_y
) =
if tb is tool_box(_,_,_,image,window_width,window_height,center_x,center_y,scale,c_v) then
if *c_v then unique else
if position_y >= window_height then unique else
draw_line(tb,0,position_y);
draw_all_lines(tb,position_y+1).
The next function runs within a delegated machine.
define One
make_drawing
(
HostWindow window,
HostImage buffer,
Word8 -> RGB palette,
Var(Maybe(Float)) scale_v,
Var(Maybe(Float)) center_x_v,
Var(Maybe(Float)) center_y_v,
Var(Bool) close_v
) =
if size(window) is (window_width,window_height) then
with tb = tool_box(window,buffer,
palette,
constant_byte_array(to_Int(window_width*window_height),0),
to_Int(window_width),to_Int(window_height),
*center_x_v,
*center_y_v,
*scale_v,
close_v),
draw_all_lines(tb,0).
define One
paint_small_rectangle
(
HostWindow window,
Int new_center_x,
Int new_center_y,
Maybe(Float) scale
) =
if size(window) is (window_width,window_height) then
with int_scale = integral_part(scale),
small_window_width = (Int)(window_width/int_scale),
small_window_height = (Int)(window_height/int_scale),
x = new_center_x - small_window_width/2,
y = new_center_y - small_window_height/2,
u = x + small_window_width,
v = y + small_window_height,
white = rgb(255,255,255),
paint_rectangle(window,rect(x,y,u,y+1),white);
paint_rectangle(window,rect(x,y,x+1,v),white);
paint_rectangle(window,rect(u,y,u+1,v),white);
paint_rectangle(window,rect(x,v,u,v+1),white).
define (HostWindow window, List(Rectangle) clip) -> One
paint_method
(
HostImage buffer,
Var(Int) new_center_x_v,
Var(Int) new_center_y_v,
Var(Maybe(Float)) scale_v,
Var(Maybe(Float)) new_scale_v
) =
(HostWindow window, List(Rectangle) clip) |->
if size(window) is (window_width,window_height) then
paint_image(window,
rect(0,0,window_width,window_height),
0,0,
buffer);
paint_small_rectangle(window,
*new_center_x_v,
*new_center_y_v,
(*new_scale_v)/(*scale_v)).
define One
new_window
(
Int window_width,
Int window_height,
Word8 -> RGB palette,
Maybe(Float) scale,
Maybe(Float) center_x,
Maybe(Float) center_y
).
define (HostWindow window, HostWindowEvent(One) e) -> List(Rectangle)
event_handler
(
HostImage buffer,
Word8 -> RGB palette,
Var(Maybe(Float)) scale_v,
Var(Maybe(Float)) new_scale_v,
Var(Maybe(Float)) center_x_v,
Var(Maybe(Float)) center_y_v,
Var(Int) new_center_x_v,
Var(Int) new_center_y_v,
Var(Bool) close_v
) =
(HostWindow window , HostWindowEvent(One) e) |->
if e is
{
quit then print("closing\n"); close_v <- true; [],
expose then if size(window) is (window_width,window_height) then
[rect(0,0,window_width,window_height)],
pointer_entering then [],
pointer_leaving then [],
key_down(ks,kk) then [],
mouse_move(ks,mx,my) then
if size(window) is (window_width,window_height) then
new_center_x_v <- to_Int(mx);
new_center_y_v <- to_Int(my);
[rect(0,0,window_width,window_height)],
mouse_click(ks,mc,mx,my) then
(if mc is left_down
then (if size(window) is (window_width,window_height) then
new_window(to_Int(window_width),to_Int(window_height),
palette,
*new_scale_v,
(*new_center_x_v - window_width/2)/(*scale_v) + *center_x_v,
(*new_center_y_v - window_height/2)/(*scale_v) + *center_y_v))
else unique);
[],
mouse_wheel(ks,md,mx,my) then
if size(window) is (window_width,window_height) then
(with sww = (*scale_v) * to_Int(window_width),
ns = *new_scale_v,
(if md >- 0
then new_scale_v <- (sww * ns)/(sww + success(2)*ns)
else new_scale_v <- (sww * ns)/(sww - success(2)*ns)));
[rect(0,0,window_width,window_height)],
tick then [],
repaint(_) then [],
specific(_) then []
}.
(if ((*new_scale_v) / (*scale_v)) > success(integer_to_float(window_height/2))
then unique else
define One
new_window
(
Int window_width,
Int window_height,
Word8 -> RGB palette,
Maybe(Float) scale,
Maybe(Float) center_x,
Maybe(Float) center_y
) =
with buffer = to_host_image(create_rgba_image(window_width,window_height,rgba(255,255,255,255)),1),
if screen_size is (screen_width,screen_height) then
with wx = to_Int(random(screen_width - truncate_to_Word32(window_width))),
wy = to_Int(random(screen_height - truncate_to_Word32(window_height))),
scale_v = var(scale),
new_scale_v = var((*scale_v)*success(8)),
center_x_v = var(center_x),
center_y_v = var(center_y),
new_center_x_v = var((Int)0),
new_center_y_v = var((Int)0),
close_v = var((Bool)false),
if open_host_window(rect(wx,wy,wx+window_width,wy+window_height),
"Mandelbrot",
managed(not_resizable),
paint_method(buffer,
new_center_x_v,new_center_y_v,
scale_v,new_scale_v),
event_handler(buffer,
palette,
scale_v,new_scale_v,
center_x_v,center_y_v,
new_center_x_v,new_center_y_v,
close_v),
identity) is
{
failure then print("Cannot open new window.\n"),
success(window) then
delegate make_drawing(window,buffer,palette,scale_v,center_x_v,center_y_v,close_v),
show(window)
}.
global define One
mandelbrot_set
(
List(String) args
) =
with palette = my_palette_5,
map_forget(show_palette,[
/*
my_palette_1,
my_palette_2,
my_palette_3,
my_palette_4, */
my_palette_5
]);
new_window(300,200,
palette,
success(70),
success(0.0),success(0.0)).