Links
Some results: link
Repo: Git
Documentation: Doc
Team
Myself (Fractal Mechanics) and Michal Glinski (GUI and polish)
Motivation
This piece of goodness was made for the annual Functional Programming competition at the University of Edinburgh, The goal was to write something graphics-based in Haskell revolving around fractals.
How does it work?
I won't go into the nitty-gritty details and show you code, since you can do that at your own leisure following the links at the top, I will however go over the high-level operation of our application.
Basics
There are plenty of ways to create fractals but the one that we decided to use is called "The Newton Method". This involves finding the roots of a complex equation (ones with imaginary numbers, I am not talking about tough equations) algorithmically - via iterative calculus magic.
Essentially what we do is feed each pixel of the image into the Newton-Algorithm-Root-Finding-Thingy and then noting which root we converge to and after how many iterations (explained in detail here), and after that colouring the said pixel based on that knowledge.
Note that the final value is not discrete and we can land far from the actual root , so we likely need to already have the roots computed, so you'll probably need to find them using this method beforehand (meta, I know).
We used an epsilon value; a cut-off distance from the actual roots to tell the program to stop iterating infinitely.
Why does this produce fractals? Newton's method is quite sensitive to the starting approximation, and will produce different results given different initial values. It also tends to "overshoot" the real value (think about it as like an object converging to some target with smaller and smaller steps, it will oscillate around the target) producing these fascinating, infinitely repeating patterns, or fractals.
So in summary, the steps to create these are as follows:
- Create pixel grid
- Run algorithm on each pixel, where the pixel coordinate becomes the position on the complex plane and is then fed into the Newton Method algorithm, take note of which root we converge to and at which iteration
- Based on the converged value colorize the pixel (This is where we let our imagination run free), how you do it is completely up to you - maybe pre-calculate the roots and apply a different colour to each one, or take the distance to the closest root and calculate a colour based on that. Go wild!
- Save the image
- (Optional if you want to animate) change boundary coordinates for zoom effect and repeat steps above; possibly also animate on other parameters as well. At the end generate GIF (We just used an online converter)
Results
We managed to create some truly sweet looking fractals and won the whole thing, so I guess we did pretty well. Have a look at these bad boys yourself:
Variants
The software comes in two different versions:
CLI:
This one is fully-featured and is what we used to create most of our fractals. With a few inputs it allows you to create anything you'd like within reason:
GUI:
This version is designed for fiddling with the mechanics and being able to preview your output live:
This project was a truly great wrap up for an amazing course - yet we did have to go through all the stages of grief before getting convinced that Haskell - and Functional Programming in general, are real neat.
Big props to our professors: Philip Wadler and Don Sannella for showing us the light.
Lambda Gang
λ