The classic example if the Sierpinski Triangle (which incidentally has a much cooler Clojure implementation [using a different technique] in 3d here). My 2D rendering kind of pales in comparison!
![2D Sierpinski image](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-fSa1CZMDLzR7iY1-5TnjNmct1KrXKEQAnUG8aLtg0SXlCZyagFxKZMZGWUEok_rSL_GZdzl-ZKWf2I2-xlyo_nd5KngLwBh3So5ZPG4Ni6XoLZCN_1W9EDpHUtPpIXsLZTFBgy5EVHM/s400/sierpinski-triangle.png)
This flavour of IFS is generated using a simple two dimensional affine transform. (this limits it to scale, rotation and translation, but not shearing transforms). The equation for this is shown below:
![Affine Transform Equation](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcyXYLUbFIGHPapLiAnGJc70eOLohCKqTz4X4VNyFtrVoRHnfcMXbmBP1u6oiZr4XuWtCL5E-I36vgKcqeAXZASMrEQV15i-2sHfDWd-J-zbbrzUGLkSsjG5nxWe-8zxtLeCyB0xb1zIE/s400/affine_eqn.gif)
An implementation of this in Clojure is shown below. The formula above is expanded out in
calculate-point
The second example draws a simple fern leaf pattern known as Barnsley's Fern.
![Barnsley's Fern](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjq9_v4E9n8d7K30Q6pdBI58V9oAnQ2BP9KrP7FOnQGPDL_DU3w2w7xbno4rHmx1Y1LZdFRxCZlBMtsKKmIw91sPMVVzsD_vYCtf2T8kQO_Z3OWMKxJpx407unn8-8pAzl1YqqRGKfe1YU/s400/barnsleys-fern.png)
The code to do the rendering uses a single agent to provide the animation (by repeatedly setting individual pixels). It's a bit yucky because I've no idea how to check the bounds of an arbitrary set of functions, so I make a guess by checking the first 10000 values to get an idea of min/max (
get-bounds
). The ugliness is compounded by the floor, abs and inc strategically located which get around the rounding errors. (big thanks to Hoeck from #clojure who suggested using take-while instead of the hideous gumph I had before which threw an exception to abort).Obviously the time taken is dependent on how many equations are used, but images are generated very quickly. The
get-bounds
function on my box computes 10000 iterations of the Sierpinski set in 30 ms!Full code available here in the misc folder.