programdemos/julia/julia.xtl
Julia sets and the Mandelbrot set from one function, u:i_terate. Both iterate z becoming z * z + c and count the steps z stays within 2 of the origin. For the Mandelbrot set, c is the grid of points and z starts at 0; for a Julia set, z starts at the grid and c is one number, which scalar extension spreads over the whole grid: the step is written once and works both ways. X_eTaL has no complex numbers yet, so z and c are each a real and an imaginary part (see the demo's README Workarounds). Run it with "xetal run demos/julia/julia.xtl"; it prints the grid's shape, a Julia set (c = -0.8 + 0.156i) drawn with characters, and the Mandelbrot set from the same function. The live page lets you pick c on the Mandelbrot map and watch the Julia set change: https://softwarewrighter.github.io/X_eTaL-demos/julia/
The view
rows : Int
rows, cols: the picture's size. cx, cy, w: the view's center and width. k: how many steps. aspect: how tall a cell is for its width (2.0 for terminal characters).
rows ← 22
ᵘc̲entred : Int -> Float
Numbers centered on 0 (u:c_entred n): 0 1 2 3 becomes -1.5 -0.5 0.5 1.5.
ᵘc̲entred ← { n → (f̲loat o̲ffsets n) − (f̲loat n − 1) ÷ 2 }
The grid of points, by broadcasting
re : Float
re, im: one row of real parts and one column of imaginary parts. grid: both parts spread over the whole grid with t_able, as a pair (gr, gi): every point's c, for the Mandelbrot set, or every starting z0, for a Julia set.
re ← cx + w × (ᵘc̲entred cols) ÷ f̲loat cols
c u:i_terate z0: k steps of z * z + c from z0
ᵘi̲terate
c and z0 are each a pair (real, imaginary): two numbers (scalar extension) or two planes of the grid, taken apart by the patterns on both sides (D7, landed). The state adds a count of steps spent inside; a point outside keeps its z.
ᵘi̲terate ← { (cr, ci) (zr0, zi0) → ⍝ One step, as a local function (s_tep, bare and scoped to this ⍝ lambda, PN6): the same arithmetic as mandelbrot.xtl's u:s_tep. s̲tep ← { (zr, zi, counts) → inside ← f̲loat 4 ≥ (zr × zr) + zi × zi a ← (inside × cr + (zr × zr) − zi × zi) + (1 − inside) × zr b ← (inside × ci + 2 × zr × zi) + (1 − inside) × zi (a, b, inside + counts) } k 's̲tep p̲ower (zr0, zi0, 0.0 × zr0) }
%5 : Float
julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.
(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid
jzr : Float
julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.
(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid
jzi
julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.
(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid
julia
julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.
(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid