We build a Mandelbrot set explorer with a Livebook. It is an Elixir code runner that lets you write Elixir in the browser.
Algorithm
An explanation can be found here
Given an image with dimensions (eg w x h of 1500 x 1000), max iteration (eg 100), we want to assign to each pixel a colour which represents the "stability" of the orbit of the underlying point.
Iterate over each pixel (i,j):
- map it into the 2D plane: compute its "complex coordinates"
- compute the iteration number
- compute a colour
- Sum-up and draw.
Explorer
We have to parts:
- one Livebook to visualise the orbits of a point, the iterates
- one Livebook to visualise the Mandelbrot set and zoom into any point.
This is done with the Numerical Elixir library Nx. A solution is also proposed using embedded Zig within the Livebook.
The Zig code can also be compiled to WebAssembly. You can view the WeAssembly version with the link below:
https://ndrean.github.io/zig-assembly-test/
The Kino.JS.Live library makes it possible to interact with the browser.
Exploring orbits
In this Livebook, you click on a point c of the 2D-plane and it computes the succession of points, the orbit, under the iterative map z(n+1) = z(n) * z(n) + c and z(0)=c.
Code source: https://github.com/ndrean/mandelbrot/blob/main/livebook/orbits.livemd
Explore the Mandelbrot set
In this Livebook, you can click into the 2D-plane to zoom into the area of interest.
We propose two solutions:
- one using only
Elixirfor the computations, - one using
Zigfor the heavy computations (you need to haveZiginstalled).
Although Zig gives you a huge boost, the performance of the Elixir code is good. This is because we use the numerical library Nx with the EXLA backend.
Code source: https://github.com/ndrean/mandelbrot/blob/main/livebook/mandelbrot.livemd


Top comments (0)