Mandelbrot set.


The mandelbrot set is a pretty neat looking fractal that is one of the first examples that you see when you start to learn about fractals. It looks quite cool so I figured I would try my hand at having a computer render it.

The mandelbrot set is defined using the following function: $$ f(x) = x^2 + c $$ Where \( c \) is the point of interest represented as a complex number. For example, if we are dealing the pixel at cordinate \((x, y)\), that woud be converted into the complex number \(x+yi\). A given number (or equivalently, a point on the screen) is in the Mandelbrot set so long as if you iterate the function the series that it produces is bounded. For example, if you take the point \(1+2i\), you have the series $$ \begin{align} x_0 &= f(0) = 0^2 + c = c \\ x_1 &= f(x_0) = c^2 + c \\ x_2 &= f(x_1) \\ &...\\ x_n &= f(x_{n-1}) \end{align} $$ If \(x_n\) blows up toward infinity as \(n\) increases, then \(c\) is not in the mandelbrot set, otherwise it is. In order to generate the image you see above, I used the Julia programming language.

using Images, ImageInTerminal

function mandelbrot(c)
    return (f(x) = x*x + c)
end

n = 3
A = 0.6
squeeze(x) = max((x^n / (A + x^n))+0.25, 0.25)

function get_point_color(x, y)
    n = 30
    f = mandelbrot(x+(y)im)
    cur = 0.0+0.0im
    iter = [cur]
    for i in 1:n
        cur = f(cur)
        if abs(cur) > 10000
            return RGB{Float64}(0.0, 0.0, squeeze(i/n))
        end
        push!(iter, cur)
    end
    return RGB{Float64}(0.0, 0.0, 0.0)
end

function remap(value, old_min, old_max, new_min, new_max)
    t = (value-old_min) / (old_max - old_min)
    return (t * (new_max - new_min)) + new_min
end

height = 500
width = 1000
colors = Array{RGB{Float64}}(undef, height, width)
for x in 1:width
    for y in 1:height
        colors[y,x] = get_point_color(remap(x, 1, width, -3.0, 1), remap(y, 1, height, -1.2, 1.2))
    end
end

colorview(RGB, colors)

Outputs:

Breaking down the code a bit,