sourcedemos/julia/julia.xtl

1⍝!/usr/bin/env xetal 2⍝# Julia sets and the Mandelbrot set from one function, u:i_terate. 3⍝# Both iterate z becoming z * z + c and count the steps z stays 4⍝# within 2 of the origin. For the Mandelbrot set, c is the grid of 5⍝# points and z starts at 0; for a Julia set, z starts at the grid and 6⍝# c is one number, which scalar extension spreads over the whole 7⍝# grid: the step is written once and works both ways. X_eTaL has no 8⍝# complex numbers yet, so z and c are each a real and an imaginary 9⍝# part (see the demo's README Workarounds). Run it with "xetal run 10⍝# demos/julia/julia.xtl"; it prints the grid's shape, a 11⍝# Julia set (c = -0.8 + 0.156i) drawn with characters, and the 12⍝# Mandelbrot set from the same function. The live page lets you pick 13⍝# c on the Mandelbrot map and watch the Julia set change: 14⍝# https://softwarewrighter.github.io/X_eTaL-demos/julia/ 15 16⍝## The view 17 18⍝# rows, cols: the picture's size. cx, cy, w: the view's center and 19⍝# width. k: how many steps. aspect: how tall a cell is for its width 20⍝# (2.0 for terminal characters). 21rows ← 22 22cols ← 64 23cx ← 0.0 24cy ← 0.0 25w ← 3.2 26k ← 40 27aspect ← 2.0 28 29⍝# Numbers centered on 0 (u:c_entred n): 0 1 2 3 becomes 30⍝# -1.5 -0.5 0.5 1.5. 31ᵘc̲entred ← { n → (f̲loat o̲ffsets n) − (f̲loat n − 1) ÷ 2 } 32 33⍝## The grid of points, by broadcasting 34 35⍝# re, im: one row of real parts and one column of imaginary parts. 36⍝# grid: both parts spread over the whole grid with t_able, as a pair 37⍝# (gr, gi): every point's c, for the Mandelbrot set, or every 38⍝# starting z0, for a Julia set. 39re ← cx + w × (ᵘc̲entred cols) ÷ f̲loat cols 40im ← cy − aspect × w × (ᵘc̲entred rows) ÷ f̲loat cols 41grid ← ((o̲ffsets rows) 'r̲ight t̲able re, im 'l̲eft t̲able o̲ffsets cols) 42(gr, gi) ← grid 43 44⍝## c u:i_terate z0: k steps of z * z + c from z0 45 46⍝# c and z0 are each a pair (real, imaginary): two numbers (scalar 47⍝# extension) or two planes of the grid, taken apart by the patterns 48⍝# on both sides (D7, landed). The state adds a count of steps spent 49⍝# inside; a point outside keeps its z. 50ᵘi̲terate ← { (cr, ci) (zr0, zi0) → 51 ⍝ One step, as a local function (s_tep, bare and scoped to this 52 ⍝ lambda, PN6): the same arithmetic as mandelbrot.xtl's u:s_tep. 53 s̲tep ← { (zr, zi, counts) → 54 inside ← f̲loat 4 ≥ (zr × zr) + zi × zi 55 a ← (inside × cr + (zr × zr) − zi × zi) + (1 − inside) × zr 56 b ← (inside × ci + 2 × zr × zi) + (1 − inside) × zi 57 (a, b, inside + counts) 58 } 59 k 's̲tep p̲ower (zr0, zi0, 0.0 × zr0) 60} 61⍝ -- end of the core --------------------------------------------------- 62 63s̲hape gr 64 65⍝# julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 66⍝# the grid. 67(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid 68s̲hape julia 69(1 + f̲loor (9.0 × julia) ÷ f̲loat k) s̲elect " .:-=+*#%@" 70 71⍝# mandel: the Mandelbrot set from the same function: c the grid, 72⍝# z0 = 0. 73(mzr, mzi, mandel) ← grid ᵘi̲terate (0.0 × gr, 0.0 × gi) 74(1 + f̲loor (9.0 × mandel) ÷ f̲loat k) s̲elect " .:-=+*#%@"