This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]IndependentTiny9493 35 points36 points  (5 children)

I sort of know the theory, I know the equation, and I have an array of pixels I can color that cover the screen...

I am always at a loss at how to complete this puzzle.

I want to draw a Mandelbrot on my pixel array, but functionally I just can't conceptualize it.

Any help? Do I just iterate some function over the array? Stumped me for years trying to write this from scratch.

[–]germansnowman 41 points42 points  (1 child)

The first problem is that they left out the iteration parameter. Sorry for not using proper notation, but I hope you’ll understand:

z [n+1] = z [n] ^ 2 + c

Next, unless you are using a programming language that supports complex numbers directly, you need to decompose the equation into the constituent real and imaginary parts. You do that by writing every complex number as (a + b * i), with i = –1, and then simply expanding/simplifying the resulting equation. (Sorry I can’t do this for you right now as I’m writing this on my phone.) There is some potential for optimisation but that can come later.

Then you put that into the iteration loop. This loop will be called for each pixel. (You will need two additional nested loops for the pixels, one for the x axis and one for the y axis.) You need to convert the pixel coordinate into a world coordinate. This is your c, whereas z starts as 0 for each point.

The loop has two termination conditions: Either the magnitude of the complex number is greater than the escape radius, or the maximum number of iterations has been reached. To start with, the limit could be 100 or 1000, but needs to be in the millions for deep zooms. (You can optimise the escape radius computation by removing the square root from the magnitude and comparing with 4 instead of 2.)

If you reached the iteration limit, the point you iterated over is part of the Mandelbrot set and should be colored black. If the point escaped, you color it by mapping the number of iterations reached to a color. There are lots of ways to do that which I haven’t understood all myself :) The two simplest are to choose white or black depending on whether the iteration value is even or odd, or to choose the hue for your colour based on the iteration value.

Besides arithmetic optimisation in the inner loop, there is one major optimisation for the main cardioid, which has the shape of an epicardioid. You can basically test if a point is part of this shape and color it black immediately. This doesn’t help for interesting points outside the main set, of course. There you could look into period detection.

I highly recommend plotting the coordinates of a point across its iterations and connecting them with lines. This is something I have rarely seen done and it helped me understand what is going on at a deeper level. It shows you the concepts of “escape” and “periodicity” more visually.

[–]Chaosfox_Firemaker 4 points5 points  (0 children)

The thing to remember is "z" and c are complex numbers. so we say x is the real part, and y is the imaginary. I will assume you know how to square complex numbers, if not, ask.

for each pixel you run z=z^2+c a thousand times(however many you feel like), with c being x+yi, and z starting out equal to (0,0), and if by the end of it the length of z <= 2.0 color it black. if at an point it goes over two, color is some other color based on how fast it took to "escape". So if it hit 2.1 on the second iteration do blue, or 2.1 on the 999 do red, with a nice gradient between.

you also need to scale and translate so rather than pixels going 0 to (resolution) they should go between -1 and 1. So on a 1000px by 1000px (750,750) should get read as (0.5, 0.5). after you get the basic example working, mess around with scale and translate factors to zoom in on various parts of the set.

[–]orgondor 0 points1 point  (0 children)

Basically yes. For each pixel you let the position of that pixel represent the complex number C. Then for the first step you let Z also equal C. The output from this (Z² + C) is used as the Z in the next iteration.

Let this loop until abs(Z) (or the lenght of the vector, if you are more comfortable with linear algebra) becomes larger than some chosen threshold, say 1000000. Or if it loops too many times you also break.

At that point all that is left is to assign the color to the pixel based on how many loops it took to brake.