Mandelbrot集合周期2灯泡区域着色错误排查求助(与颜色映射无关)
Mandelbrot集合周期2灯泡区域着色错误排查求助(与颜色映射无关)
大家好,我最近写了一段代码用连续着色渲染曼德博集合,但碰到个棘手的问题——周期2灯泡上的部分区域着色明显不对:那些本来属于集合内部、不该逃逸的点,被当成了迭代100次(我的迭代上限)才逃逸的情况,颜色完全不符合预期。
先给大家描述一下异常效果:整体曼德博集合的轮廓是对的,但周期2灯泡区域有大片异常着色,本该是集合内部的点却呈现出和迭代满上限才逃逸的点一样的颜色。
我已经尝试过的解决办法
- 提高渲染分辨率和最大迭代次数,但问题依然存在
- 把数据类型从
np.float32改成np.float64来提升精度,可惜程序运行时间太长,根本跑不完
下面是我的完整代码,麻烦各位帮忙看看哪里出问题了:
import numpy as np import matplotlib.pyplot as plt def mandelbrot(cmax, width, height, maxiter): real = np.linspace(-2, 0.5, width, dtype=np.float32) imag = np.linspace(0, cmax.imag, height // 2 + 1, dtype=np.float32) c_real, c_imag = np.meshgrid(real, imag) output = np.zeros((height // 2 + 1, width), dtype=np.uint16) z_real = np.zeros_like(c_real) z_imag = np.zeros_like(c_imag) mask = np.ones_like(c_real, dtype=bool) p = np.sqrt((c_real - 0.25)**2 + c_imag**2) cardioid = c_real < p - 2 * p**2 + 0.25 period2_bulb = (c_real + 1)**2 + c_imag**2 < 0.0625 mask[cardioid | period2_bulb] = False output[~mask] = maxiter - 1 epsilon = 1e-10 for i in range(maxiter): zr2 = z_real[mask] ** 2 zi2 = z_imag[mask] ** 2 z_imag_new = 2 * z_real[mask] * z_imag[mask] + c_imag[mask] z_real[mask] = zr2 - zi2 + c_real[mask] z_imag[mask] = z_imag_new diverged = zr2 + zi2 >= 4.0 abs_z = zr2 + zi2 + epsilon output[mask] = i + 1 - np.log(np.log(abs_z)) / np.log(2) mask[mask] = ~diverged output[output == maxiter - 1] = 0 full_output = np.vstack([np.flipud(output), np.flipud(output[-2::-1, :])]) return full_output width, height, maxiter = 12000, 9000, 100 cmax = complex(0, 1) mandelbrot_set = mandelbrot(cmax, width, height, maxiter) plt.figure(figsize=(16, 12)) plt.imshow(mandelbrot_set, extent=[-2, 0.5, -1, 1], cmap="inferno", interpolation="bilinear") plt.colorbar(label="Iterations to Divergence") plt.title("Mandelbrot Set") plt.xlabel("Re(c)") plt.ylabel("Im(c)") plt.show()
备注:内容来源于stack exchange,提问作者Apostla
相关产品推荐
相关产品推荐

