sourcedemos/mandelbrot/mandelbrot.xtl
1⍝!/usr/bin/env xetal
2⍝# The Mandelbrot set as arrays: every point c of a view iterated at
3⍝# once, z becoming z * z + c, counting the steps each point stays
4⍝# within 2 of the origin. X_eTaL has no complex numbers yet (an ask:
5⍝# see the demo's README Workarounds), so z and c are each two Float
6⍝# arrays, their real and imaginary parts. Run it with "xetal run
7⍝# demos/mandelbrot/mandelbrot.xtl"; it prints the shapes
8⍝# of c and of the stepped state, the set drawn with characters, and
9⍝# one point's orbit (its real parts, then its imaginary parts). The
10⍝# live page steps k and lets you click a point for its orbit:
11⍝# https://softwarewrighter.github.io/X_eTaL-demos/mandelbrot/
12
13⍝## The view
14
15⍝# rows, cols: the picture's size. cx, cy, w: the view's center and
16⍝# width (in the plane, not pixels). k: how many steps. aspect: how
17⍝# tall a cell is for its width (2.0, since a character is about
18⍝# twice as tall as it is wide; a page of square pixels uses 1.0).
19rows ← 24
20cols ← 64
21cx ← -0.6
22cy ← 0.0
23w ← 3.0
24k ← 30
25aspect ← 2.0
26
27⍝# Numbers centered on 0 (u:c_entred n): 0 1 2 3 becomes
28⍝# -1.5 -0.5 0.5 1.5.
29ᵘc̲entred ← { n → (f̲loat o̲ffsets n) − (f̲loat n − 1) ÷ 2 }
30
31⍝## c for every point, by broadcasting
32
33⍝# re, im: one row of real parts and one column of imaginary parts,
34⍝# centered on (cx, cy) and w wide.
35⍝# cr, ci: c's real and imaginary parts spread over the whole grid:
36⍝# t_able spreads the row down and the column across, no loop over
37⍝# points.
38re ← cx + w × (ᵘc̲entred cols) ÷ f̲loat cols
39im ← cy − aspect × w × (ᵘc̲entred rows) ÷ f̲loat cols
40cr ← (o̲ffsets rows) 'r̲ight t̲able re
41ci ← im 'l̲eft t̲able o̲ffsets cols
42
43⍝## One step for every point at once
44
45⍝# u:s_tep (zr, zi, counts): the state is a tuple of three same-shaped
46⍝# arrays (z's real part, its imaginary part, and the count of steps
47⍝# spent inside), taken apart by the pattern and given back as a new
48⍝# tuple; no more packing them into one array (D7, landed). One step
49⍝# computes z * z + c everywhere, keeps z where the point has already
50⍝# left the disk |z|^2 > 4 (the mask inside), and adds inside to the
51⍝# count.
52ᵘs̲tep ← { (zr, zi, counts) →
53 inside ← f̲loat 4 ≥ (zr × zr) + zi × zi
54 a ← (inside × cr + (zr × zr) − zi × zi) + (1 − inside) × zr
55 b ← (inside × ci + 2 × zr × zi) + (1 − inside) × zi
56 (a, b, inside + counts)
57}
58
59⍝## k steps, from z = 0
60
61⍝# zr, zi, counts: the state after k steps (function power, from a
62⍝# zero state), taken apart by the same pattern as the binding reads
63⍝# it in. m2: |z|^2. counts: how many steps each point stayed inside;
64⍝# the points still inside after k steps are the set, so far.
65(zr, zi, counts) ← k 'ᵘs̲tep p̲ower (0.0 × cr, 0.0 × cr, 0.0 × cr)
66m2 ← (zr × zr) + zi × zi
67⍝ -- end of the core ---------------------------------------------------
68
69s̲hape cr
70s̲hape zr
71
72⍝ The picture: a character per point by how long it stayed inside, @
73⍝ for the points still inside after k steps (the set).
74(1 + f̲loor (9.0 × counts) ÷ f̲loat k) s̲elect " .:-=+*#%@"
75
76⍝## One point's orbit
77
78⍝# cr0, ci0: a point near the set's edge, c = -0.75 + 0.1i, which
79⍝# circles slowly rather than running away or settling.
80⍝# u:o_rbit s: the next z (z * z + c) for this one point, s its
81⍝# previous z (real part, imaginary part), no mask needed (a single
82⍝# point, always run the full k steps).
83cr0 ← -0.75
84ci0 ← 0.1
85ᵘo̲rbit ← { s → (cr0 + ((1 s̲elect s) × 1 s̲elect s) − (2 s̲elect s) × 2 s̲elect s) c̲at ci0 + 2 × (1 s̲elect s) × 2 s̲elect s }
86⍝ z0, z1, ..., z5 (real parts, then imaginary parts): the orbit, six
87⍝ points each run from z = 0 by function power, n steps for the nth.
88'{ n → 1 s̲elect n 'ᵘo̲rbit p̲ower 0.0 0.0 } e̲ach o̲ffsets 6
89'{ n → 2 s̲elect n 'ᵘo̲rbit p̲ower 0.0 0.0 } e̲ach o̲ffsets 6