RosettaCodeData/Task/Mandelbrot-set/Python/mandelbrot-set-5.py

51 lines
1.5 KiB
Python
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import numpy as np
import matplotlib.pyplot as plt
d, h = 800, 500 # pixel density (= image width) and image height
n, r = 200, 500 # number of iterations and escape radius (r > 2)
x = np.linspace(0, 2, num=d+1)
y = np.linspace(0, 2 * h / d, num=h+1)
A, B = np.meshgrid(x - 1, y - h / d)
2023-07-22 21:42:12 -07:00
C = 2.0 * (A + B * 1j) - 0.5
2023-07-01 11:58:00 -04:00
2025-08-11 18:05:26 -07:00
def iteration(C):
S, T = np.zeros(C.shape), np.zeros(C.shape)
Z, dZ = np.zeros_like(C), np.zeros_like(C)
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
def iterate1(C, S, T, Z, dZ):
2025-08-11 18:05:26 -07:00
S, T = S + np.exp(- abs(Z)), T + 1
Z, dZ = Z * Z + C, 2 * Z * dZ + 1
return S, T, Z, dZ
2026-02-01 16:33:20 -08:00
for i in range(0, n, 1):
2025-08-11 18:05:26 -07:00
M = abs(Z) < r
2026-02-01 16:33:20 -08:00
S[M], T[M], Z[M], dZ[M] = iterate1(C[M], S[M], T[M], Z[M], dZ[M])
2025-08-11 18:05:26 -07:00
return S, T, Z, dZ
S, T, Z, dZ = iteration(C)
D = np.zeros(C.shape)
2023-07-22 21:42:12 -07:00
plt.imshow(S ** 0.1, cmap=plt.cm.twilight_shifted, origin="lower")
plt.savefig("Mandelbrot_set_1.png", dpi=200)
N = abs(Z) >= r # normalized iteration count
2024-10-16 18:07:41 -07:00
T[N] = T[N] - np.log2(np.log(abs(Z[N])) / np.log(r))
2023-07-22 21:42:12 -07:00
plt.imshow(T ** 0.1, cmap=plt.cm.twilight_shifted, origin="lower")
plt.savefig("Mandelbrot_set_2.png", dpi=200)
N = abs(Z) > 2 # exterior distance estimation
D[N] = np.log(abs(Z[N])) * abs(Z[N]) / abs(dZ[N])
plt.imshow(D ** 0.1, cmap=plt.cm.twilight_shifted, origin="lower")
plt.savefig("Mandelbrot_set_3.png", dpi=200)
2023-08-01 14:30:30 -07:00
2023-09-01 09:35:06 -07:00
N, thickness = D > 0, 0.01 # boundary detection
2023-08-01 14:30:30 -07:00
D[N] = np.maximum(1 - D[N] / thickness, 0)
plt.imshow(D ** 2.0, cmap=plt.cm.binary, origin="lower")
plt.savefig("Mandelbrot_set_4.png", dpi=200)