Fractastic is a simple and approachable fractal generator written in C. It generates Julia set fractals by repeatedly iterating a complex quadratic function until divergence (or convergence). The number of iterations required to diverge at a point is recorded and used as part of the value to color the related pixel in the final, rendered image of the fractal.
run.sh
is a script that will handle everything for you automatically. It is heavily commented, so you can see the exact steps that it takes. The C program outputs to stdout
; run.sh
just captures its output and pipes it to a ppm
(and png
, if ImageMagick is installed) file.
Use run.sh
as follows:
./run.sh [output_file] [fractastic_options]
where [fractastic_options] are the options that you would pass in as if you had invoked fractastic
directly (as follows).
For Julia sets:
./fractastic J [width] [height]
[x_min] [x_max] [y_min] [y_max]
[max_iterations]
[color_multiplier]
[c_re] [c_im]
[d]
And for (generalized) Mandelbrot sets:
./fractastic M [width] [height]
[x_min] [x_max] [y_min] [y_max]
[max_iterations]
[color_multiplier]
[d]
output_file
: the name of the file to output to (do not include file extension;.ppm
and.png
are auto-generated)width
: the width of the output file in pixelsheight
: the height of the output file in pixelsx_min
: the x-coordinate of the left bound of the rendered image (left bound of the window)x_max
: the x-coordinate of the right bound of the rendered image (right bound of the window)y_min
: the y-coordinate of the lower bound of the rendered image (lower bound of the window)y_max
: the y-coordinate of the upper bound of the rendered image (upper bound of the window)max_iterations
: the number of iterations to try before determining that f does not divergecolor_multiplier
: a multiplier for the contrast of the image (lower values result in more black; higher values result in more white; honestly, just play with it until the fractal looks nice!)c_re
: the real component of the complex parameterc
to be passed into the iterated function f(z) = zd + cc_im
: the imaginary component of the complex parameterc
to be passed into the iterated function f(z) = zd + cd
: the real parameterd
to be passed into the iterated function f(z) = zd + c
Note that the scale and step of the render algorithm are automatically determined from the above parameters.
ImageMagick is available in most package managers, including homebrew
(Mac) and apt-get
(Debian, Ubuntu, etc.). Just install it, and be sure that the convert
command is available. If you do not install ImageMagick, fractastic
will still work, but you will not be able to get png
output (only ppm
).
To generate all of these examples, execute ./gen-examples.sh
. Note that this may take a while depending on the speed of your computer.
I deleted the ppm
output of these commands before uploading them to the git repository to save (massive amounts) of space. In normal usage, these commands will generate ppm
files.
f(z) = z2 - 0.618
./run.sh examples/julia1 J 2000 2000 -2 2 -2 2 1000 20 -0.618 0 2
f(z) = z2 + (-0.4 + 0.6i)
./run.sh examples/julia2 J 2000 2000 -2 2 -2 2 1000 1 -0.4 0.6 2
f(z) = z3 + (-0.4 + 0.6i)
./run.sh examples/julia3 J 2000 2000 -2 2 -2 2 1000 20 -0.4 0.6 3
f(z) = z2 + (-0.8 + 0.156i)
./run.sh examples/julia4 J 2000 2000 -2 2 -2 2 1000 1 -0.8 0.156 2
f(z) = z2 + c
./run.sh examples/mandel1 M 2000 2000 -2.5 1.5 -2 2 1000 20 2
f(z) = z3 + c
./run.sh examples/mandel2 M 2000 2000 -2 2 -2 2 1000 20 3
f(z) = z4 + c
./run.sh examples/mandel3 M 2000 2000 -2 2 -2 2 1000 20 4
f(z) = z6 + c
./run.sh examples/mandel4 M 2000 2000 -2 2 -2 2 1000 20 6
The purpose of this project was to create a simple yet powerful fractal generator in pure, best-practice C code without relying on external libraries. This was done so that I could learn more about fractals and C, as well as to serve as a pedagogical tool for other beginners to learn about C (in particular, how to generate images). Most of the resources I found online were in incomprehensible, difficult-to-read C or used advanced graphics libraries, so I wanted to provide an alternative.
- Color