Normalized Iteration Count (NIC) 技术解析
在逃逸时间算法中,Normalized Iteration Count (NIC) 是一种提升分形图像质量的技术,它能够产生更加平滑且细腻的颜色过渡效果。此方法通过调整迭代次数来实现,其核心数学公式如下:
μ=n+1− log(log(∣Z(n)∣))/ log(p)
写成代码形式:
mu = n + 1 - log (log (|Z(n)|) ) / log (p)
其中,
- Zn 表示迭代次数,
- ∣Z(n)∣ 表示最后一次迭代后复数Z的模长,
- p 通常取为2。
实现代码示例
下面是几种不同语言的代码实现示例,展示了如何应用 NIC 方法于分形计算中。
Ruby 实现
Ruby
NormalizedIterationCount = lambda do |fractal| z = fractal.args[:z]**2 + fractal.c; fractal.last_iteration += 1 z = z**2 + fractal.c; fractal.last_iteration += 1 modulus = Math.sqrt(fractal.args[:z].real**2 + fractal.args[:z].imag**2).abs mu = fractal.last_iteration + Math.log(2 * Math.log(fractal.bailout)) - Math.log(Math.log(modulus)) / Math.log(fractal.args[:p]) (mu / fractal.max_iterations * 765).to_i end
Maxima 实现
Lisp
GiveNormalizedIteration(z,c,E_R,i_Max):= /* */ block( [i:0,r], while abs(z)<E_R and i<i_Max do (z:z*z + c,i:i+1), r:i-log2(log2(cabs(z))), return(float(r)) )$
其中 log2(x) := log(x) / log(2);
C 语言伪代码实现
C
int iter_count = 0; float escape_radius = 20.0; complex Z, C; loop (forever) { Z = Z*Z +C; iter_count ++; float modulus = sqrt (ReZ*ReZ + ImZ*ImZ); if (modulus > escape_radius) goto stop; if (iter_count > maxiter) goto stop; } stop: Z = Z*Z +C; iter_count ++; // a couple of extra iterations helps Z = Z*Z +C; iter_count ++; // decrease the size of the error term. float modulus = sqrt (ReZ*ReZ + ImZ*ImZ); float mu = iter_count - (log (log (modulus)))/ log (2.0); color_value = colormap_lookup (mu); draw_pixel (C, color_value);