# 2D genetic fractals makers guide (part 5): Going for 3D!

Coral growth (genetic fractal)

Previous article: Arbitrary branchings

In the previous parts of this series we introduced the idea of genetic fractals, based on a simple formulation, aka the genetic equation. This equation is controlled by driver functions. Initially we started out with a driver function for the progression of the fractals, $D_{r}(s)$,  and the way in which it bends and swirls, $D_{\varphi}(s)$. We then added accessory functions for color, branch width and even a control for branch points. We could go on and play around graphic attributes and make the branches programmable from an external source. One interesting addition would be to make an accessory function that responds to the environment and use that o influence growth and bending. But all of that is incremental and you can figure this out quite easily.

The last thing we should do is to see how robust the mathematics is and extend the model to 3D. There is a 3D version of the genetic equation. Remember that the two-dimensional exponential form of the Creation Equation is:

$C =\int_s D_{r} e^{i\int_s D _{\varphi }}$

Or in trigonometric form:

$C(s)=\left\{\begin{matrix}\int_s D_{r}sin(\int_s D_{\varphi}ds)ds\\ \int_s D_{r}cos(\int_sD_{\varphi}ds)ds\end{matrix}\right.$

Using the Spherical Coordinate system, we can write the Genetic Equation in the three dimensional form as shown below. We use a spherical coordinate system with radial distance r, azimuth  $\varphi$ and inclination $\vartheta$. Refer to Wikipedia for a good description of this good but tricky coordinate system.

$C(s)=\left\{\begin{matrix}x(s)\\ y(s)\\ z(s)\end{matrix}\right.=\left\{\begin{matrix}\int_s D_r sin(\int_s D_{\varphi}ds)sin(\int_s D_{\vartheta}ds)ds\\ \int_s D_r cos(\int_s D_{\varphi}ds)sin(\int_s D_{\vartheta}ds)ds\\ \int_s D_r cos(\int_s D_{\vartheta}ds)ds \end{matrix}\right.$

, where $D_{r}$ , $D_{\varphi}$ and $D_{\vartheta}$ are the driver functions. As before, don’t be too scared of the large number of integrals, they are essentially sums that allow is to “remember” where we are in the tree graph.

To test this, we can simply translate the maths into Ruby code, as shown below. As before, we approximate integration by summation.

def creationEquation3D(dR,dPhi, dTheta, lastPoint, lastPhi, lastTheta)

newPhi=lastPhi+dPhi #integrate over phi (it's summation really)
newTheta=lastTheta+dTheta #integrate over phi (it's summation really)

return [
lastPoint[0]+dR*sin(newPhi)*sin(newTheta),
lastPoint[1]+dR*cos(newPhi)*sin(newTheta),
lastPoint[2]+dR*cos(newTheta)],
newPhi,
newTheta
end


You might want to compare that with the 2D version of the creation equation in Ruby:

#the Creation Equation is implemented in its trigonemtric form for simplicity

def creationEquation(dR,dPhi, lastPoint, lastPhi)
newPhi=lastPhi+dPhi #integrate over phi (it's summation really)
return [
lastPoint[0]+dR*sin(newPhi),
lastPoint[1]+dR*cos(newPhi)],
newPhi
end



Surprisingly (to me anyway, having been through the wars of 3D CG programming), this 3D version of the creation equation just works, as shown in the image at the top of this post (click to magnify). No tweaks needed. That, is the power of maths, and that is also the perfect message to end this series with. Hope you liked it 🙂

Previous article: Arbitrary branchings