SOFTIST 编程方法筆記 目録

描画曼德布洛特集合(算法)

The Algorithm of Drawing Mandelbrot Set

本文纪录曼德布洛特集合的描画方法 

曼德布洛特集合(Mandelbrot set)是一个著名的集合。其定义是,当n → ∞的时候,使下面左侧的复数数列不发散的所有常数c=a+ib的集合。実数和虚数的計算公式如下面右侧的式子。描画曼德布洛特集合的乐趣在于观察某些区域发散程度的变化,这些变化规律貌似一样其实不同。

 

上图用下面的程序描画的,用颜色表现发散的激烈程度。

#define r_limit 255
#define v_limit 255
void Mandelbrot(double a, double b, double& x, double& y, int& nRepeat)
{
    x = 0.0;
    y = 0.0;
    for (nRepeat = 1; nRepeat <  r_limit; nRepeat ++)
    {
        double xn = x;
        x = x * x - y * y + a;
        y = 2 * xn * y + b;
        if ((x*x + y*y) >= v_limit*v_limit)
            break;
    }
}

void CMandelbrotSetView::DrawMandelbrotSet() 
{
        
    CClientDC dc(this);
    for (double a = -3.0; a <= 3.0; a += 0.005)
    for (double b = -2.0; b <= 2.0; b += 0.005)
    {
        double x, y;
        int nRepeat;
        Mandelbrot(a, b, x, y, nRepeat);
        COLORREF clrref = RGB(255-nRepeat, sqrt(x*x), sqrt(y*y));
        int xx = 600 + a * 200;
        int yy = 400 - b * 200;
        dc.SetPixel(xx, yy, clrref);
    }
}