Jump to content

Mandelbrot set: Difference between revisions

→‎{{header|Python}}: Duplicate code removed (see following subsection)
(→‎{{header|Python}}: Duplicate code removed (see following subsection))
Line 8,852:
extent=(X.min(), X.max(), Y.min(), Y.max()))
plt.show()</lang>
 
Actually the same, but without optimizations and therefore better suited for teaching. At first without NumPy, but already with complex numbers.
<lang python>import matplotlib.pyplot as plt
 
 
def linspace(start, stop, num):
return [start + (stop - start) / (num - 1) * k for k in range(num)]
 
 
def zeros(rows, cols):
return [[0 for j in range(cols)] for i in range(rows)]
 
 
d, n = 200, 50 # pixel density & number of iterations
r = 2.5 # escape radius (must be greater than 2)
 
x = linspace(-2.5, 1.5, 4 * d + 1)
y = linspace(-1.5, 1.5, 3 * d + 1)
 
T = zeros(len(y), len(x))
 
for i, b in enumerate(y):
for j, a in enumerate(x):
c, z = a + b * 1j, 0j
for k in range(n):
if abs(z) < r:
z = z ** 2 + c
T[i][j] = k + 1
 
plt.imshow(T, cmap=plt.cm.twilight_shifted)
plt.savefig("Mandelbrot.png", dpi=250)</lang>
At second with NumPy and complex matrices.
<lang python>import numpy as np
import matplotlib.pyplot as plt
 
d, n = 200, 50 # pixel density & number of iterations
r = 2.5 # escape radius (must be greater than 2)
 
x = np.linspace(-2.5, 1.5, 4 * d + 1)
y = np.linspace(-1.5, 1.5, 3 * d + 1)
 
A, B = np.meshgrid(x, y)
C = A + B * 1j
 
Z = np.zeros_like(C)
T = np.zeros(C.shape)
 
for k in range(n):
M = abs(Z) < r
Z[M] = Z[M] ** 2 + C[M]
T[M] = k + 1
 
plt.imshow(T, cmap=plt.cm.twilight_shifted)
plt.savefig("Mandelbrot.png", dpi=250)</lang>
 
===Normalized Counting, Distance Estimation, Mercator Maps and Deep Zoom===
305

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.