Fun With Fractals (Julia)

Ali Hamza
5 min readNov 7, 2018
A Julia Fractal, with c=-0.8+0.156i

Fractals

Something really simple I found in here:

A fractal is a never-ending pattern. Fractals are infinitely complex patterns that are self-similar across different scales. They are created by repeating a simple process over and over in an ongoing feedback loop. Driven by recursion, fractals are images of dynamic systems — the pictures of Chaos. Geometrically, they exist in between our familiar dimensions. Fractal patterns are extremely familiar, since nature is full of fractals. For instance: trees, rivers, coastlines, mountains, clouds, seashells, hurricanes, etc. Abstract fractals — such as the Mandelbrot Set — can be generated by a computer calculating a simple equation over and over.

What this says is, that fractals can be thought of as a process which is iterative, and repetitive. Consider the sunflower seeds in the below image:

Source: https://www.greenmylife.in/shop/seeds/flowering-annuals-seeds/sunflower-seeds/large-bloom-sunflower/

As you can see, an observable sequence exists in there. The pattern, which follows the Fibonacci sequence is the mathematical background behavior which is followed by the sunflower seeds. Since it’s iterative, and repeated, it can be thought of a factorial whose equation is highly dependent on the golden ration (1.618…), which is the ratio between a number, and the previous number in a Fibonacci sequence.

There are a lot of natural processes, which follow a very specific, repetitive process, and it appears as if a strong mathematical background is governing their behavior.

Julia Fractals

“Julia” is a class of complex mathematical functions, whose behavior, although repetitive, is said to be “Chaotic”. According to a Wikipedia article:

Chaos: When the present determines the future, but the approximate present does not approximately determine the future.

According to a very simple, yet elegant explanation here:

Julia set fractals are normally generated by initializing a complex number z = x + yi where i^2 = -1 and x and y are image pixel coordinates in the range of about -2 to 2. Then, z is repeatedly updated using: z = z^2 + c where c is another complex number that gives a specific Julia set. After numerous iterations, if the magnitude of z is less than 2 we say that pixel is in the Julia set and color it accordingly. Performing this calculation for a whole grid of pixels gives a fractal image.

Yep. That’s pretty much about it, and now we’ll see how we can actually implement all this in a very short python script.

First, we need some library to deal with the images, and another library to deal with the plotting.

Importing the necessary stuff

Next, we need to define some global constants fur us, which will help in the fractal generation process.

Next, we see the powerhouse of this entire post, the core function which actually generates a fractal, and returns a bitmap image. Some default inputs for the function are set, which will generate the exact fractal, as used in the cover photo of this post, but you can tweak around with different inputs to generate your very own unique fractals. The code is somewhat a modified form of the original core written in here.

Now since you have a defined function, you just need to call it, and plot it like this:

Just to make life easier for you, here’s the complete code in a single gist (just in case you don’t feel like copying individual snippets).

I tried with several configurations, and here are some interesting results:

# Get what the function returns
bitmap = generateFractal(cX=0.285, cY=0.01, colorTup=(14, 13, 7))
# to display the created fractal
plt.figure(figsize=(12, 18))
plt.imshow(bitmap)
Julia Fractal: cX = 0.285, cY = 0.01, colorTup = (14, 13, 7)
# Get what the function returns
bitmap = generateFractal(cX=1-goldenRatio, cY=0, maxIter=64, colorTup=(12, 17, 7))
# to display the created fractal
plt.figure(figsize=(12, 18))
plt.imshow(bitmap)
Julia Fractal: cX = 1-goldenRatio, cY = 0, maxIter = 64, colorTup = (12, 17, 7)

And finally, let’s try to get something “Gothic” by changing our inputs.

# Get what the function returns
bitmap = generateFractal(cX=-0.7269, cY=0.1888, maxIter=255, colorTup=(0, 0, 1))
# to display the created fractal
plt.figure(figsize=(12, 18))
plt.imshow(bitmap)
Julia Fractal: cX = -0.7269, cY = 0.1888, maxIter = 255, colorTup = (0, 0, 1)

As you can see, with a very little change (within less than 1 unit) in the inputs of our function, we can generate a totally new sequence. That’s chaos. The approximate present does not predict approximate future. To put it in a simple way,

It’s like I got a brush, I select some colors, but I have no idea what the brush will do as soon as it touches the canvas. It just paints chaos.

What next? We can generate 3D fractals, which look way more cooler than anything you might’ve seen, if rendered with proper details.

Here’s a nice challenge for you if you want to take it to a whole new level. If you can generate images like this, I’m sure you’d be able to generate videos as well, and might end up getting something as cool as this:

Source: https://en.wikipedia.org/wiki/Julia_set#/media/File:JSr07885.gif

This is just an introduction to Julia fractals. There are many other kinds of fractals, which can be generated through modifying the underlying equation, i-e z = z²+c. I’ll be really (like, reallyyy) glad if you actually figure out newer, better patterns using other kinds of fractals, and can come up with some stunning artistic output. In case you do, do let me know. Signing off nor now!

--

--