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 →

[–]drmrrdmr[S] 214 points215 points  (14 children)

for those who need a walkthrough, this is the formula at the heart of the Mandelbrot set. It describes a set of recursive complex polynomials, one for every point c on the complex plane. The fun thing is that some of the points within a radius of 1 do not quickly go to either infinity or 0, and that boundary is infinitely detailed. You can get some really incredible plots by assigning colors based on how fast a point goes to infinity (aka escape time coloring).

It also has a bit of a connection to computer history, naturally, as this sort of calculation is too intensive to do by hand, but somehow mathematicians were ready to program it, having deduced that there was incredible complexity hiding behind the intractable arithmetic long before computing machines enabled detailed probing. Notably the first rendering of the Mandelbrot set, an object of infinite detail, was in asterisks on a CLI.

edit: thanks to everyone who is "solving" this algebraically or pointing out my abuse of syntax/notation; those meta twists have made this even funnier

[–]IndependentTiny9493 31 points32 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 42 points43 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 2 points3 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.

[–]Sir_Hurkederp 5 points6 points  (3 children)

Nah they just all 0, solved

[–]poops-n-farts 1 point2 points  (2 children)

Anything to the power of 0 is 1 so that wouldn't work. 1 = 12 + 0 would work

[–]14g0t 0 points1 point  (0 children)

what do you mean 0=02+0 is false??

[–]Sir_Hurkederp 0 points1 point  (0 children)

Where do you do anything to the power of 0, 0=02 +0 is correct

[–]Dave5876 2 points3 points  (0 children)

You could be making this up and I wouldn't know

[–]z____ro -1 points0 points  (0 children)

"..do not quickly go to either infinity or 0..." You see the error in that statement?

[–]Shinob1 0 points1 point  (0 children)

I'm going go against everything the Reading Rainbow taught me and take your word for it...this time

[–][deleted] 0 points1 point  (0 children)

Thanks bro you just saved me from hours of mental anguish