<a href="http://en.wikipedia.org/wiki/Fractal">Fractals</a> are known as a “self similar images”, that is these images are exactly similar to a part of itself. The <a href="http://en.wikipedia.org/wiki/Julia_set">Julia set</a> is one such fractal, discovered by French mathematician Gaston Maurice Julia. They form a variety of very beautiful patterns.

A popular Julia set quadratic polynomial function f<sub>C</sub>, is shown below:

f<sub>C</sub>: Z<sub>n+1</sub> = Z<sub>n</sub><sup>2</sup>+ C

It is a set of complex points Z on the complex plane for which the iterative series does not tend to infinity, where C is a constant complex number. Different values of C produce different Julia sets. Some examples of Julia set are shown below (image source: Wikipedia):

<b><img class="alignnone" alt="" src="https://lh4.googleusercontent.com/zCyyzDZRe0T0famg_RXiauZS97WI7n-KpK35yQO-DuC5S4BjanEhmC16LaqPo5AWYjftlnEoWjT8DQLJfWMz9JVkg1sRJrnBdWEtmU7KTNEvSTprYU_JSfWw" width="453px;" height="196px;" /></b>

Let us now see how the Julia set can be calculated and rendered easily using ArrayFire. You can download the full code from <a href="http://blog.accelereyes.com/blog/wp-content/uploads/2013/03/juliaSet.zip">here</a>.

The Julia set images can be created by mapping each point in a image to a point on the complex plane. The above equation is then applied to each of these points iteratively to determine whether they fall inside or outside the Julia Set:
<pre class="lang:default decode:true">array offset = seq(DIM*DIM); // holds sequential numbers 0,1,2,...,DIM
array x_index = (abs)(offset/DIM); // x index of each pixel
array y_index = (abs)(offset%DIM); // y index of each pixel

// Calculate the point in complex plane corresponding to the x and y index
float multiplier = 1.5; // zoom factor
array realPart =  multiplier * (DIM/2.0 - x_index) / (DIM/2.0); // calculate the real part of pt
array complexPart =  multiplier * (DIM/2.0 - y_index) / (DIM/2.0); // calculate the imag part of pt
array pt = complex(realPart,complexPart); // DIM*DIM complex points for each pixel of the image

cuComplex C = {0.285,0.01}; // constant

// Calculate pt(n+1) = pt(n)*pt(n) + C iteratively
for(int i=0; i&lt;50; i++){
	pt = pt*pt+ C;
}

//'result' holds 0 if point is in Julia Set or 1 if point is outside the Julia Set 
array result = (((real(pt)*real(pt)) + (imag(pt)*imag(pt))) &lt; 1000);</pre>
The next part consists of rendering the image onto the screen. This can be done in 3 simple steps:

1. Create an ArrayFire array “img” representing the image to be rendered:
<pre class="lang:default decode:true">array img(DIM, DIM, 3); // DIM*DIM 3 channel (RGB) image</pre>
2.  For each point in the image, set the point to some color if the point is in the Julia set, else set it to a different one. Here all the Julia set points are being set to blue and the rest (background) are set to black:
<pre class="lang:default decode:true">array img = constant(0, DIM, DIM, 3); // DIM*DIM 3 channel (RGB) image initialized to zero
img(span, span, 1) = 255 * result; // set the blue component of the image</pre>
3. In the last step, render the image on the screen:
<pre class="lang:default decode:true">image(img); // renders the image on screen.</pre>
That’s it! Run the program to see the beautiful Julia set fractal on screen.

Here is an example of a Julia set created with C=0.285+0.01i with 50 iterations

<b id="internal-source-marker_0.02422779379412532"><img alt="" src="https://lh4.googleusercontent.com/Cqw3lf_RUMP8hz-Vav5q9jmrzB9DkTTk96FE6nRaNWfa8-IBihbcfEhjiiffIGON0rfDCsYsFjTxFNuX9vjJkCedF2o_udXcheYUA1cR8y_Rnm1azRhcxXMV" width="678px;" height="652px;" /></b>

Another Julia set (in green) with C=-0.8+0.156i with 200 iterations.

<b id="internal-source-marker_0.7519505347590894"><b id="internal-source-marker_0.02422779379412532"><img alt="" src="https://lh4.googleusercontent.com/Sw4Nd9eAQqJwo3MTDARIVkeUd-3d4HuZYJVpGbpYWfyiSa2zwO-tsevjh9ZcuOUp2wmW9A5Fz55dmQBhmbvzwvQKCtHv7MHFJaFJbZC3TsQ5mgxre1tLqSul" width="677px;" height="651px;" /></b></b>

&nbsp;

&nbsp;

&nbsp;

&nbsp;