22 lines
826 B
Python
22 lines
826 B
Python
|
# Source: https://matplotlib.org/matplotblog/posts/animated-fractals/
|
||
|
def mandelbrot(x, y, threshold):
|
||
|
"""Calculates whether the number c = x + i*y belongs to the
|
||
|
Mandelbrot set. In order to belong, the sequence z[i + 1] = z[i]**2 + c
|
||
|
must not diverge after 'threshold' number of steps. The sequence diverges
|
||
|
if the absolute value of z[i+1] is greater than 4.
|
||
|
|
||
|
:param float x: the x component of the initial complex number
|
||
|
:param float y: the y component of the initial complex number
|
||
|
:param int threshold: the number of iterations to considered it converged
|
||
|
"""
|
||
|
# initial conditions
|
||
|
c = complex(x, y)
|
||
|
z = complex(0, 0)
|
||
|
|
||
|
for i in range(threshold):
|
||
|
z = z ** 2 + c
|
||
|
if abs(z) > 4.: # it diverged
|
||
|
return i
|
||
|
|
||
|
return threshold - 1 # it didn't diverge
|