# Fractals/Iterations in the complex plane/demj

This algorithm has 2 versions:
* exterior
* interior
Compare it with version for parameter plane and Mandelbrot set : DEM/M

# External distance estimation

Distance Estimation Method for Julia set ( DEM/J ) estimates distance from point z ( in the exterior of Julia set ) to nearest point in Julia set.
* c = 0.255
* c= -0.75+0.11
* c=-0.1+0.651
* c=-0.74543+0.11301*i
* c=- 0.181502832839439 -0.582826014844503*I
For distance estimate it has been proved that the computed value differs from the true distance at most by a factor of 4:
Math formula :
$distance(z_{0},K_{c},n)=2*|z_{n}|*{\frac {\log |z_{n}|}{|z'_{n}|}}\,$
where :
$z'_{n}\,$ is first derivative with respect to z.
This derivative can be found by iteration starting with
    $z'_{0}=1\,$
and then :
$z'_{n}=2*z_{n-1}*z'_{n-1}\,$

## Pseudocode and the code

* The Beauty of Fractals
* The Science of Fractal Images, page 198,
* Distance Estimator by Robert P. Munafo
Pseudocode by Claude Heiland-Allen
First derivative wrt z :
| degree | Function f(z) | derivative wrt z |
| 2 | $z^{2}+c$ | $2*z$ |
| 3 | $z^{3}+c$ | $3*z^{2}$ |
| 4 | $z^{4}+c$ | $4*z^{3}$ |
| d | $z^{d}+c$ | $d*z^{d-1}$ |

## How to use distance
### distance max

Distance max is the old ( deprecated) method.
One can use distance for colouring :
* only Julia set ( boundary of filled Julia set)
* boundary and exterior of filled Julia set.
Here is first example :
Here is second example

## Zoom
### distance max

Distance max is the old ( deprecated) method.
DistanceMax is smaller than pixel size. During zooming pixel size is decreasing and DistanceMax should also be decreased to obtain good picture. It can be made by using formula :
$DistanceMax={\frac {PixelSize}{n}}\,$
where $n\leq 1\,$
One can start with n=1 and increase n if picture is not good. Check also iMax !!
DistanceMax may also be proportional to zoom factor $mag\,$:
$DistanceMax={\sqrt {\frac {thick}{1000*mag}}}$
where thick is image width ( in world units) and mag is a zoom factor.
One can use also tanh which gives more precise look:

## Examples of code

For cpp example see mndlbrot::dist from mndlbrot.cpp in src code of program mandel ver 5.3 by Wolf Jung.
C function using complex type :
C function using double type:
Delphi function :
Matlab code by Jonas Lundgren
Maxima function :

### shadertoy

* Julia - Distance 1 by iq. Analytical distance to a Julia set z^2 + c where vec2 c = vec2( -0.745, 0.186 )
* Julia - Distance 2 by iq = SDF for the Julia set of f(z) = z^3+C where const vec2 kC = vec2(0.105,0.7905);
* Julia - Distance 3 by iq. A generic Julia set renderer, using distance to the set (Douady_Hubbard potential). In this case I'm using a rational function of order 6: f(z) = (z-(1+i)/10)(z-i)(z-1)^4 / ((z+1)(z-(1+i)) + c
* Inigo Quilez :: articles :: distance to fractals - 2004

# Internal distance estimation
## Colouring the Julia set by Gert Buschmann

In order to get a nice picture, we must also colour the Julia set, since otherwise the Julia set is only visible through the colouring of the Fatou domains, and this colouring changes vigorously near the Julia set, giving a muddy look (it is possible to avoid this by choosing the colour scale and the density carefully). A point z belongs to the Julia set if the iteration does not stop, that is, if we have reached the chosen maximum number of iterations, M. But as the Julia set is infinitely thin, and as we only perform calculations for regularly situated points, in practice we cannot colour the Julia set in this way. But happily there exists a formula that (up to a constant factor) estimates the distance from the points z outside the Julia set to the Julia set. This formula is associated to a Fatou domain, and the number given by the formula is the more correct the closer we come to the Julia set, so that the deviation is without significance.
The distance function is the function $\delta (z)=\phi (z)/|\phi '(z)|$ (see the section Julia and Mandelbrot sets for non-complex functions), whose equipotential lines must lie approximately regularly. In the formula appears the derivative $z'_{k}$ of $z_{k}$ with respect to z. But as $z_{k}=f(f(...f(z)))$ (the k-fold composition), $z'_{k}$ is the product of the numbers $f'(z_{i})$ (i = 0, 1, ..., k-1), and this sequence can be calculated recursively by $z'_{k+1}=f'(z_{k})z'_{k}$ and $z'_{0}=1$ (before the calculation of the next iteration $z_{k+1}=f(z_{k})$). In the three cases we have:
    $\delta (z)=$lim$_{k→∞}$$|z_{kr}-z*|/|z'_{kr}|$ (non-super-attraction)
    $\delta (z)=$lim$_{k→∞}$$log|z_{kr}-z*||z_{kr}-z*|/|z'_{kr}|$ (super-attraction)
    $\delta (z)=$lim$_{k→∞}$$log|z_{k}||z_{k}|/|z'_{k}|$ (d ≥ 2 and z* = ∞)
If this number (calculated for the last iteration number kr - to be divided by r) is smaller that a given small number, we colour the point z dark-blue, for instance.
For more see Pictures_of_Julia_and_Mandelbrot_Sets

## code
# references

* ↑ Koebe quarter theorem in wikipedia
* ↑ Distance Estimator by Robert P. Munafo
* ↑ math.stackexchange question: how-to-draw-a-mandelbrot-set-with-the-connecting-filaments-visible
* ↑ Pictures of Julia and Mandelbrot sets by Gert Buschmann
* ↑ Pictures of Julia and Mandelbrot sets by Gert Buschmann
* ↑ Julia sets by Jonas Lundgren in Matlab
* Explicit estimates on distance estimator method for Julia sets of polynomials by Masayo Fujimura, Yasuhiro Gotoh, S. Yoshida Published 1 October 2013
* Distance Estimator algorithm by E Demidov
* math.stackexchange question: interior-distance-estimate-for-julia-sets-getting-rid-of-spots