Since I’m a big fan of fractals, this weeks challenge is another fractal. Next week we will have something more divers, but I just want to see how crazy you guys can make this. So for this weeks challenge I picked the Mandelbrot set. I had a lot of fun learning how to do this so I hope you will have the same experience.
The Mandelbrot set is the set of complex numbers c
for which the function f(z)=z²+c
does not diverge when iterated, i.e., for which the sequence f(0)
, f(f(0))
, etc., remains bounded.
Mandelbrot set images are made by sampling complex numbers and determining for each whether the result tends towards infinity when a particular mathematical operation is iterated on it. Treating the real and imaginary parts of each number as image coordinates, pixels are colored according to how rapidly the sequence diverges, if at all.
You can implement it as broad as you want, it can be in ascii like this:
Or make it with an animated zoom like so:
As a reference here is some pseudo code to help you along which was provided by wikipedia:
For each pixel (Px, Py) on the screen, do:
{
x0 = scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.5, 1))
y0 = scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1, 1))
x = 0.0
y = 0.0
iteration = 0
max_iteration = 1000
while ( x*x + y*y < 2*2 AND iteration < max_iteration ) {
xtemp = x*x - y*y + x0
y = 2*x*y + y0
x = xtemp
iteration = iteration + 1
}
color = palette[iteration]
plot(Px, Py, color)
}
And again I’ve created a Codepen for you to start with. Please submit your solution to this Google Form and happy coding.