![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhckon9bV3Oa2fDBjFcSjl388vGhlSx5dhVDjbmOnCpQKIF86i6GpIvglsTI1cy_dmldDBkFoXLPo144uRTYafyh4fq7nW_n3gGnWmNdQf3MP4FAlcGO3XVY1bmYxfhnkwRPCk0tWu6Vu0/s200/2.png)
- Let's say we have some pattern P (at the beginning P = {C})
- Let Q be a reversed and inverted P (e.g. if P = {CACC}, then Q = {AACA})
- Obtain new value for P by concatenation: P = PCQ
- Repeat until the required depth is reached
CalculateCurve
function does:![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWXsqgzDxHStGRYWBz3weEG2Y524ADypymJzPB3jnYsf1bLeUarlyeQGm4yZFXlZ-u98HFUSQxUJtntnLcmbnNYdro7WZCiuZft0_nkR1uzon_5KKY5pY2xbke4IXdqlCmGsD_tI3YXvo/s400/code_1.gif)
It takes two arguments - depth (which is self-explanatory), and folding, which is a function returning the folding direction (1 or -1) on the given step / depth.
To understand the code better, let's extract
f = #1~Join~{folding[#2]}~Join~Reverse[-#1]&;
Clearly
f
performs steps 2 and 3 of the algorithm described. Then Fold[f, {folding[0]}, Range[0, depth]]
is equivalent to f[f[f[{folding[0]}, 0], 1], 2]
and so on.For the standard Dragon fractal the paper is always folded in the same direction, so that folding function is just a constant, e.g. -1. In this case a sample output of
CalculateCurve
with depth 2 looks like {-1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1}. Now in order to get the graphical representation of this sequence, we need to implement some kind of the simple automata.![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEig-EbeQ_3T083bcE7nXCzPCPbU-22-a50kCFB_GUgiza77Xej-ELjuxeGp9-l9r_9zBibxQ81niMOamnZzPsgOSYFtz2nw5D3I4nqTPLoer7pX8NmkM25KOT5pgf6EcfoQ7QgjWmJyBxU/s400/code_2.gif)
Again, for simplicity let's define
f = (a += #; {x, y} += {Sin[a angle], Cos[a angle]})&;
It is easy to see that this function increments the current "direction angle" by 1 or -1, and then it appends the increment in the current direction to the {x, y} coordinates (increment angle is most often PI / 2, but other values sometimes give interesting results as well). Note that the current {x, y} value is returned as a result. Applying this function to the folding sequence (f /@ CalculateCurve[depth, folding]
) yields a list of bend point coordinates. It is then interpolated with B-splines of the 2nd degree, and output as Graphics.Together with controls for changing fractal depth and bending angle the code can be made as short as this:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWUiNk6dGzxK7vK4HE8NhfGRQklRelWw71KntQkKOE59kzpItVa4TDqXmOadYNRxv16jX8Jrd3VOi8jVpPe_yR5rZ8laJS-A-MLa3U7VBxiVxsIPeDTvV608xBTIJCKXiq-SZeWIJv1dE/s400/code_3.gif)
and in this form it can be considered as a shorter alternative to the code from the Paperfolding Dragon Curve demo.
Some other examples of the growing Dragon Curves:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAksYqPe9R00UHFwF1TvVII6PtadLKZx2sD0WbazpFw3QgS7TXzyXMlhqJuYN55DTVAvG2NfpXUI_LndWcKvrcddlzWSmtNa8NCwKccZYw-Ieq3GqYbVpCiPAM-2kIUNcOwRDQfNmf-5I/s200/1.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAKOrg7WiJwtpPK-dAYnf4Aj9swrpZc2TcB39mf1qxfydWtKjmSbEGJDohgQMftM6b-UQ9AWUw_pTWsFvhNGHarY8K9VsgCT3kRNpkom1mBsmWYC_2G5wx87ziH_quydnnb6U9zxCxnWA/s200/3.png)
You can download complete notebook here.