# How-to: Epic visualizations of 3D fractals?

I have seen videos like this before in the past of things like "3D Mandlebulbs" and similar fractal sets, but can anyone tell me what sorts of programs are actually used create these visualizations? How computationally expensive are these visualizations to make? Also, what sort of simulations are these? I mean, what sort of coordinate system, or mesh, are these sorts of structures realized on? I have most of my experience with computational simulations in FORTRAN, Python, and Matlab, and I just don't see how you would even begin to undertake creating the data to produce these sorts of visualizations in those languages.

Thanks.

• This open source software (mandelbulber.com/download.php) seems like a basic step in understanding what the simulations are, but I'd still like to know what is going on under the hood of the simulations more thoroughly if anyone can explain. – Loonuh Dec 4 '14 at 22:08
• The question is a bit unclear. Are you interested in how fractals are drawn or how they are defined? There are no "simulations", strictly speaking, they are carefully defined mathematical objects. If drawing is what you're after, you could just learn how to use pbrt or povray, that would be enough. – Kirill Dec 4 '14 at 23:18
• Well I have in my mind a basic fractal like the Koch curve, where I start out with some stencil, and then I iterate that stencil 'N' many times until I have drawn something that looks like a fractal (so I guess I am interested in how they are drawn by a computer). What sort of algorithm is ticking away to draw them? I guess with the Koch curve the algorithm is obvious, but are these super advanced visualizations just running the same sorts of iterative algorithms on monstrously larger levels? – Loonuh Dec 4 '14 at 23:36
• It is really unclear to me why you think there is an "algorithm ticking away", or why the algorithms must be iterative. You define the object you want to draw first (e.g., a finite approximation to Koch snowflake), then render it. In 2d it's much easier as you don't need to do as much computer graphics work. – Kirill Dec 4 '14 at 23:40
• Ok, I understand now how I was wrong to think of an algorithm ticking away. The last bit I need to clarify for myself then is how the surfaces are actually "computed" (in the sense how points in the Mandelbrot set are computed). – Loonuh Dec 5 '14 at 0:03

Recently I happened to wonder somewhat similar questions expect I was, in particular, interested in real time visualization of three-dimensional fractals. Since other answers consider two-dimensional fractals, I decided to include some knowledge I have acquired on three-dimensional rendering and ray marching.

## Definition of a surface and ray tracing

One way to define a surface in three-dimensions is through a function $$f:\mathbb{R}^3\rightarrow\mathbb{R}$$ which satisfies $$f(\boldsymbol{x})=0,$$ for every point $$\boldsymbol{x}\in \mathbb{R}^3$$ that belongs to the surface. Thus, the surface $$S$$ can be defined as $$S=\{\boldsymbol{x} \in \mathbb{R}^3\,|\,f(\boldsymbol{x})=0\}.$$ For one to visualize the surface $$S$$, the intersection point $$\boldsymbol{p} \in \mathbb{R}^3$$ of a ray—starting from the camera position $$\boldsymbol{c} \in \mathbb{R}^3$$ to some direction $$\boldsymbol{d} \in \mathbb{R}^3$$, $$\|\boldsymbol{d}\|=1$$—and the surface must be sought. After these intersection points are known for sufficiently many rays, a visualization can be constructed (some examples follow).

One way to find the intersection point $$\boldsymbol{p}$$ is through analytical calculations: the surface defined by $$S$$ is sometimes so simple that this calculation can be done by hand. An example that considers the intersection of a ray and a sphere can be found from Wikipedia. This is exactly what the traditional ray tracing is all about.

In a more general setting, no analytical relationship exists for finding the intersection point $$\boldsymbol{p}$$. In such a case, one must rely to numerical methods.

## Numerical methods for finding the intersection point

A proficient applied mathematician would immediately attempt to apply, e.g., Secant method to the problem: find $$t \in \mathbb{R}$$ such that $$f(\boldsymbol{c}+t\boldsymbol{d}) \approx 0.$$ There is, however, a caveat. For a general $$f$$ the function $$g(t):=f(\boldsymbol{c}+t\boldsymbol{d})$$ might have several roots and in Secant method (or similar) nothing guarantees that a root nearest to the camera is recovered. You might end up drawing the wrong side of the object of interest! Thus, the applied numerical method should be guaranteed to find a root nearest to the camera.

### Ray marching and distance fields

A somewhat trivial method to find the nearest (or approximately nearest) root is to fix a sufficiently small constant step size $$\Delta t$$ and then find the smallest $$n \in \mathbb{N} \cup \{0\}$$ such that $$f(\boldsymbol{c}+n \Delta t \boldsymbol{d})\approx 0.$$ This is known as ray marching and is indeed a valid method for visualizing the surface $$S$$. The problem is that it's tremendously slow since one must try every possible $$n$$ starting from zero!

Fortunately, there exists a clever trick to make the computation faster by ensuring that $$f$$ defines a distance field of $$S$$. This means that at every point $$\boldsymbol{x}$$ in the space $$\mathbb{R}^3$$ the value $$f(\boldsymbol{x})$$ represents the minimum distance from $$\boldsymbol{x}$$ to the surface $$S$$. This sounds like a hard restriction to satisfy but for many fractals the distance fields (or approximate distance fields) have already been derived by enthusiasts (see Fractal Forums).

The algorithm goes as follows:

1. $$\boldsymbol{p}:=\boldsymbol{c}$$
2. $$\boldsymbol{p}:=\boldsymbol{p}+f(\boldsymbol{p})\boldsymbol{d}$$
3. If $$f(\boldsymbol{p})$$ is larger than some tolerance go back to step 2. Otherwise, break.

Output of the prescribed algorithm is the intersection point $$\boldsymbol{p}$$.

## Visualizing fractals in practice

If you happen to own a sufficiently fast graphics processing unit and know the function $$f$$ for some fractal then this method can be implemented in real time using, e.g., OpenGL Shading Language. See the following Shadertoy entry I constructed as an example: There is no reason at all for the fractals to be defined by iterative algorithms: fractals are carefully defined mathematical objects. For example, the Mandelbrot set is defined as the set of points $$M= \{z \in\mathbb{C} \mid \text{the iteration }f_z(f_z(\cdots(f_z(0))\cdots) \text{ does not diverge}\}, \qquad f_z(x) = x^2+z.$$
Once you have this set of points (and it really doesn't matter how you define it, except that the final images will be more or less interesting), the next task is to render it. Drawing an object means that you have a rectangular array of pixels in an image, and to each pixel you need to assign a colour; the colour is then chosen based on the set of points you want to draw. The simplest method is to map the centre of each pixel $p$ to a point $z_p$ in the complex plane, and draw it with one colour if $z_p\in M$, and another if $z_p\notin M$.