📜  使用图形的C / C ++中的Mandlebrot集

📅  最后修改于: 2021-05-31 16:50:52             🧑  作者: Mango

分形

分形是永无止境的模式。分形是无限复杂的模式,它们在不同尺度上是自相似的。通过在不断进行的反馈循环中反复重复一个简单的过程来创建它们。数学上的分形可以解释如下。

  • 屏幕上的点的位置作为初始解被输入到一个方程式中,并且该方程式被迭代了很多次。
  • 如果该方程趋向于零(即,迭代结束时的值小于初始值),则该点被涂成黑色。
  • 如果等式趋于无穷大(即最终值大于初始值),则根据增加速率(即该值趋于无穷大的速率),用适当的颜色绘制像素。

    定义Mandlebrot

    Mandelbrot集是函数的复数c的函数 f_c(z) = z^2 + cz = 0迭代时不发散,即序列 f_c(0), f_c(f_c(0))等等,仍然以绝对值为界。简单来说,Mandelbrot集是特定的复数集,在绘制时其分形边界非常复杂。


    执行

    #include 
    #include 
    #include 
      
    // Defining the size of the screen.
    #define Y 1080
    #define X 1920
      
    // Recursive function to provide the iterative every 100th
    // f^n (0) for every pixel on the screen.
    int Mandle(double _Complex c,
               double _Complex t = 0,
               int counter = 0)
    {
      
        // To eliminate out of bound values.
        if (cabs(t) > 4) {
            putpixel(creal(c) * Y / 2 + X / 2,
                     cimag(c) * Y / 2 + Y / 2,
                     COLOR(128 - 128 * cabs(t) / cabs(c),
                           128 - 128 * cabs(t) / cabs(c),
                           128 - 128 * cabs(t) / cabs(c)));
            return 0;
        }
      
        // To put about the end of the fractal,
        // the higher the value of the counter,
        // The more accurate the fractal is generated,
        // however, higher values cause
        // more processing time.
        if (counter == 100) {
            putpixel(creal((c)) * Y / 2 + X / 2,
                     cimag((c)) * Y / 2 + Y / 2,
                     COLOR(255 * (cabs((t * t))
                                  / cabs((t - c) * c)),
                           0, 0));
            return 0;
        }
      
        // recursively calling Mandle with increased counter
        // and passing the value of the squares of t into it.
        Mandle(c, cpow(t, 2) + c, counter + 1);
      
        return 0;
    }
      
    int MandleSet()
    {
      
        // Calling Mandle function for every
        // point on the screen.
        for (double x = -2; x < 2; x += 0.0015) {
            for (double y = -1; y < 1; y += 0.0015) {
                double _Complex temp = x + y * _Complex_I;
                Mandle(temp);
            }
        }
        return 0;
    }
      
    int main()
    {
        initwindow(X, Y);
        MandleSet();
        getch();
        closegraph();
        return 0;
    }
    

    输出

    想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”