Strange Fractal Generator


Ok, one of my experiments with CImg were The Strange Fractal Generator that you can download here. Or visualize online with HTML5 canvas here (you gonna need a Browser [IE isn’t a browser]) and hopefully soon ChromeExperiments.

This …. thing …. was coded at fist in C++ using lots of recursion.

Solving it with recursion is very simple, but the browsers have a security system that blocks execution for a long time, and for now it’s impossible to call a page flip to update the screen. For the canvas version, the stack had to be controlled manually with a lot of pushes and pops.

The script isn’t obfuscated and I haven’t optimized it, so its pretty easy to understand it.

The script language is very simple because it was inspired on the Logo programming language, that uses FD, BD, RT, LT as “Forward Draw”, “Backward Draw”, “Right Turn” and “Left Turn” navigate the cursor drawing shapes. This simple language uses F for Forward, R to turn Right an L to Left. Those commands are written always followed by a number that is the command parameter.

The most important difference between the Logo operation and the script used in the fractal generator is that the coordinates are always in fractions. The fraction represents the size of the segment to be drawn (the first segment haves the screen width).

For instance the command “F1” draws a line crossing the screen.

The most simple sequence of the presets is the following one:




This script always starts on the top-left corner. It moves the cursor to the horizontal center of the screen (from left to right), turns 90 degrees to right (pointing downwards), moves to the middle of the screen (vertical) turns 180 degrees left (pointing upwards), moves to the top, turns 90 degrees to right and moves to the top-right corner.

It is important to note that the script MUST END AT THE TOP-RIGHT corner to make the fractal be correctly generated.

You may also like...