Curious for J1SK requirements
Sunday, March 23, 2014 » programming
other clever solution is the folding the dragon method. That was I wanted to use to generate the dragon fractal. The construction of dragon fractal can be represented by a binary sequence where the 1 value means a left turn and the 0 value is right turn (more details about the generation here). The binary sequence can be generated by the next formula:
bool turn = (((n & -n) << 1) & n) != 0
This exactly gives the binary sequence (more details here on Wikipedia). Because I was still curious what other solutions are available, some hours later I've found a really usefull site Integer sequences where can be found a particular chapter (and still growing) telling different informations about dragon fractal (click here). On this site is a recursive formula that was introduced by Michael Somos. I have not seen that this is used in any other demos came to my hand so I have just decided make this formula more popular using this one:
a(n) = if( n%2, a(n\2), 1 - (n/2%2))
and it draws the classical dragon curve fractal. The demo redraws in every 10 milliseconds a new expanded curve using more steps by elapsed time.
To make the demo more playful I have added some limited random parameters to the curve generation and binded them to the different colors. The algorithm is simple: when based on generated value turning in a direction left or right and drawing the line in this direction the length of the line must be added to one coordinate (on X or Y axis). Because the turning is always 90 angle so the line drawing is always with one coordinate on fix axis (and the other coordinate on other axis is not changing). This changing is a variable that is added to the non-changing coordinate. When this variable is zero that gives the classical 90 angle dragon curve fractal (pressing red button on demo). When the variable can be given by Math.random()/10 that results the curve generated by clicking on green button. When the variable can given by Math.random()/100 (pressing brown button) and when it's Math.random() and added some random colors to each line gives the last curve (pressing blue button). As I mentioned the curve is redrawn recursively, the positive changing random values always give new coordinates to dragon curve as if it moves.
As I was ignoring at the beginning the size of the code, at the end I was using techniques to minifing the result. Before using automatizations I have shortened the changing names (e.g.: variables) and optimalized the sequence of instructions and there is a clever list on jscrush site that strongly recommended to be gone through. The automatizations were first the closure-compile and the mentioned jscrush sites.
To the submitted code and showing the demo click here.