Mandelbrot Set

Problem #358

Tags: classical c-1 special mathematics lua

Who solved this?

No translations... yet
mandelbrot set in 8 iterations
Mandelbrot set colored with 8 iterations and starting point X0=(0, 0), red-green palette

Now we'll try our skills in graphic art - drawing infamous Mandelbrot Set with given parameters.

This problem should be written in Lua language, so please refer to Quick Intro to Lua if needed and try Hurkle problem if you haven't yet.

You'll need to recollect (or quicly study) the very basics of Complex Numbers, because Mandelbrot Set is, actually a set of complex numbers, satisfying certain whimsical property. This set looks curious when plotted onto the "complex plane" - the plane representing all possible complex numbers.

Definition. Given some complex value C we define whether it belongs to Mandelbrot Set or not by the following process.

Let's put it into formulas:

X0 = (0, 0)        we remember complex value consists of two parts, real and imaginary
X1 = X0^2 + C
X2 = X1^2 + C
...
check: abs(Xn) < R - if so (after many iterations), mark C as belonging to the Mandelbrot Set

You'll need to recollect basic operations on complex numbers (squaring, addition and taking absolute value). For R it is typically recommended to use the value 2.

Drawing and other technical details

We provide you with painting canvas (rectangle below, of size 400 by 300 points) and some means to paint the results from the code. As said above, the code should be in Lua language. Regard the following example:

require "graph"

for y = 0, scrH-1 do
    for x = 0, scrW-1 do
        pset(x, y, x * 255 / scrW, y * 255 / scrH, 0)
    end
end

In the first line we import some small custom module. It defines values scrW and scrH (width and height of the canvas) along with function pset(x, y, r, g, b). Use this function passing coordinates of the point as the first two parameters and red / green / blue components (0 .. 255) for the remaining 3.

Copy-paste this example into solution area and click Lua button below it. Few seconds later you should see the canvas painted with red-green gradient.

Now simply change this small program to draw the Mandelbrot Set. Scale the "field" of your research to the range -2 .. 1 by X and -1.5 .. 1.5 by Y. Try every of scrW * scrH points and for each of them put black pixel if belongs to Mandelbrot Set and something different otherwise.

Use the two input values as X0 (real and imaginary part). Simply read them in your program like this:

x0_re, x0_im = io.read('*n', '*n')

If you like beautiful image, use the followig coloring approach: count how many iterations are needed for given point to get out of the R "circle" by absolute value. If it never diverges, put black color. Otherwise use 255 * num_iterations / max_iterations for example for red and green components. This will paint points "slightly diverging" brightly, while "quickly diverging" will look more "dull", so you'll see the shape similar to one shown in the example above.

The checker will run your code and compare result dot-by-dot with whatever it expects. However it only cares of whether dot is black or not (belongs to the Set or not) - so particular coloring of the zone outside the Set is not important. If you don't succeed, you'll get some "difference" value showing how far are you (1.00 means every dot is wrong, while everything below 0.01 is accepted).

P.S. you can print something from your code for debugging purposes (it will go into answer box) but please remove these debugging lines before submitting (as checker uses output stream itself).

You need to login to get test data and submit solution.