Julia Set Visualisation

About

The Julia set is a chaotic set of fractals defined by iterating on a function fc(z), where:

fc(z)=z2+c

z and c are complex parameters.

The colour of a given pixel is defined by how many recursive iterations of fc(z) it takes, until the magnitude of the returned value is greater than some R.

Pseudo-code

def julia(z):
    i = 0

    while len(z) < R && i < LIMIT:
        z = (z * z) + c
        i = i + 1

    return i

for (x, y) in pixels:
    zx = map(x, 0, width, -R, R)
    zy = map(y, 0, height, -R, R)

    z = complex(zx, zy)

    value = julia(z)

    pixels[x, y] = grey(value)

Parameters For The Above Sketch

By default the demonstration above uses a c value where:

c=0.7885eia

a is animated over the range ain[0,2π] with a period of 4π seconds.

In-case you haven't worked with complex numbers for a while, the above definition of c is in the form:

c=Aeiθ

This, in practice, is a representation of a polar coordinate, where A is magnitude and θ is the angle (radians).

To represent this in code, you can do the following (pseudo-code):

cx = A * cos(theta)
cy = A * sin(theta)

c = complex(cx, cy)
out vec4 outColor; 

#define R 2.0
#define ITERATIONS 200
#define TAU 6.2831855
#define MAG 0.7885

void main() {
	// coordinates from 0,0 (bottom left) to 1,1 (top right).
	vec2 uv = gl_FragCoord.xy / uResolution;
	vec2 z = 3.0 * uv - vec2(1.5);

	float theta = mod(1.0 + uTime / 5.0, TAU);
	vec2 c = MAG * vec2(cos(theta), sin(theta));

	int i = 0;

	while (length(z) < R && i < ITERATIONS) {
		float x = z.x * z.x - z.y * z.y;
		float y = 2.0 * z.x * z.y;

		z = vec2(x, y) + c;

		i = i + 1;
	}

	if (i == ITERATIONS) i = 0;

	float j = float(i) / float(ITERATIONS);

	outColor = vec4(vec3(j), 1.0);
}