Contains two programs, mandelbrot
and mandelviewer
.
mandelbrot
is run from the command line and renders a supersampled image of the Mandelbrot set to a png file. It is possible to change which part of the set is rendered, how zoomed in the image is, the number of iterations to use, as well as a few other things.
This was one of my first projects to learn rust.
mandelviewer
shows a view of the fractal and lets you pan and zoom around in it, change settings, and save images. This part is currently under development as I am using it to learn the iced UI crate.
- Install Rust and git
- Open a terminal in the folder you want to install the program in
- Clone this git repo with
git clone https://www.github.com/JSorngard/mandelrust.git
- Go into the repository with
cd mandelrust
- Compile
mandelbrot
withcargo build --release -p mandelbrot
- Run it with
./target/release/mandelbrot.exe
. The resulting image can be found in the folder the program was ran in, or the location specified by the-o
flag - You can specify where the image is focused, how zoomed it is and how many iterations to do (among other things) with command line arguments. For an exhaustive list run the program with the
--help
argument mandelviewer
can be compiled withcargo build --release -p mandelviewer
and run with./target/release/mandelviewer.exe
. If you are on Linux you will need to have the dependencies of the file save dialog installed.
I have tried to make the program faster over time. Some of the techniques used are:
- If the image contains the real axis, split the image there and only render the larger half, then mirror the smaller half from it.
- There are closed form expressions for checking if a point is inside the main cardioid or the period 2 bulb, these are used to skip a large amount of iteration.
- Rayon is used to parallelize the rendering.
- Supersampling is done only close to the border of the set.
- The iteration loop has been restructured to use the minimum number of multiplications.
- Link time optimization can been enabled by changing
--release
to--profile release-lto
. - The number of codegen units has been set to 1
- Cargo is set to enable optimization with every instruction set available on the compiling CPU.
The program can render a nine times supersampled 4k image of the set in around 580 ms on my laptop with a quad core i7-7500U CPU, while a non-supersampled 1080p image finishes in around 80 ms.
You can easily test the performance of various renders on your own machine with cargo bench
. The results will be printed to the terminal, but more detailed data can be found in target/criterion/report/index.html
in the form of a web page.
The main goal of this program is to generate pretty looking pictures, "pretty" is of course subjective, but here is a list of what I've done to make the resulting images look better in my eyes:
- Use a color palette that is smooth i.e. small differences in escape speed should map to small differences in color. In this program this is achieved by the color palette being a continuous function that maps escape speeds to colors.
- Do not abort the iteration when |z| > 2, but at a larger absolute value (in this program I have chosen 6). Together with using a function that smoothly maps iteration count and absolute value to a number between 0 and 1 this completely removes color banding.
- Supersample the image to remove graininess.
Default settings:
We can zoom in on details in the above image:
If we want to zoom even deeper we can change the maximum number of iterations in order to keep the image crisp:
Images in grayscale without any color mapping can also be made:
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.