# Mandelbrot set

## Defination of Mandelbort set

According to wiki, the Mandelbrot set is the set of complex numbers for which the function does not diverge when iterated from .

## What magic does it has

If we try to find the set of in a complex plane. Here is the magic:

where x axis is the real part of , y axis is the imaginary of . A point is colored black if it belongs to the set.

## How to find set

here is the pseducode from wiki:

```
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
//when the sum of the squares of the real and imaginary parts exceed 4, the point has reached escape.
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)
}
```

for complex numbers:

Hence: x is sum of real parts in z and c, y is sum of imaginary parts of z and c. and

## How about 3D

I am going to try my 3D later, but please see the detail from skytopia firstly, I cited the formula directly.

3D formula is defined by:

where z and c are defined by {x,y,z}

```
{x,y,z}^n = r^n { sin(theta*n) * cos(phi*n) , sin(theta*n) * sin(phi*n) , cos(theta*n) }
where
r = sqrt(x^2 + y^2 + z^2)
theta = atan2( sqrt(x^2+y^2), z )
phi = atan2(y,x)
// z^n + c is similar to standard complex addition
{x,y,z}+{a,b,c} = {x+a, y+b, z+c}
//The rest of the algorithm is similar to the 2D Mandelbrot!
//Here is some pseudo code of the above:
r = sqrt(x*x + y*y + z*z )
theta = atan2(sqrt(x*x + y*y) , z)
phi = atan2(y,x)
newx = r^n * sin(theta*n) * cos(phi*n)
newy = r^n * sin(theta*n) * sin(phi*n)
newz = r^n * cos(theta*n)
```