📜  使用图形在C / C ++中设置的Julia Fractal

📅  最后修改于: 2021-05-31 18:41:46             🧑  作者: Mango

JSr07885.gif

CC BY-SA 4.0,链接

Julia集与复平面上的那些点z = x + iy相关,对于这些点,级数zn + 1 = zn2 + c不会趋于无穷大。 c是一个复数常数,每个c取一个不同的Julia集。系列的初始值z0是图像平面中的每个点。

众所周知的Mandelbrot集形成了Julia集的一种索引。 Julia集已连接或已断开,从Mandelbrot集内部选择的c值已连接,而来自Mandelbrot集外部的c值已断开。断开连接的集合通常称为“灰尘”,无论以何种分辨率查看,它们都由单个点组成。

代码

#include 
#include 
#include 
#include 
  
#define Y 1079
#define X 1919
  
// To recursively find the end value
// of the passed point till the pixel
// goes out of the bounded region
// or the maximum depth is reached.
int julia_point(float x, float y,
                int r, int depth,
                int max,
                double _Complex c,
                double _Complex z)
{
    if (cabs(z) > r) {
        putpixel(x, y,
                 COLOR(255 - 255 * ((max - depth) * (max - depth)) % (max * max),
                       0, 0));
        depth = 0;
    }
    if (sqrt(pow((x - X / 2), 2)
             + pow((y - Y / 2), 2))
        > Y / 2) {
        putpixel(x, y, 0);
    }
    if (depth < max / 4) {
        return 0;
    }
    julia_point(x, y, r,
                depth - 1, max,
                c, cpow(z, 2) + c);
}
  
// To select the points in a Julia set.
void juliaset(int depth, double _Complex c, int r, int detail)
{
    for (float x = X / 2 - Y / 2; x < X / 2 + Y / 2; x += detail) {
        for (float y = 0; y < Y; y += detail) {
            julia_point(x, y, r,
                        depth, depth, c,
                        (2 * r * (x - X / 2) / Y)
                            + (2 * r * (y - Y / 2) / Y)
                                  * _Complex_I);
        }
    }
}
  
// Driver code
int main()
{
    initwindow(X, Y);
    int depth = 100, r = 2, detail = 1;
  
    // Initial value for Julia
    // set taken by my personal preference.
    double _Complex c = 0.282 - 0.58 * _Complex_I;
    while (1) {
  
        cleardevice();
  
        // To formulate the display text
        // for the 'c' coordinate
        // into string format.
        char str1[100], str2[100], strtemp[100];
        if (floor(creal(c)) == -1) {
            strcpy(str1, "-0.");
        }
        if (floor(creal(c)) == -0) {
            strcpy(str1, "0.");
        }
        if (floor(cimag(c)) == -1) {
            strcpy(str2, "-0.");
        }
        if (floor(cimag(c)) == -0) {
            strcpy(str2, "0.");
        }
        itoa(sqrt(pow(creal(c), 2)) * 1000, strtemp, 10);
        strcat(str1, strtemp);
        strcat(str1, ", ");
        itoa(sqrt(pow(cimag(c), 2)) * 1000, strtemp, 10);
        strcat(str2, strtemp);
        strcat(str1, str2);
        outtextxy(X * 0.8, Y * 0.8, str1);
  
        // To call the julia-set for the selected value of 'c'.
        juliaset(depth, c, r, detail);
        outtextxy(X / 3, Y * 0.9,
                  "Press '1' to Exit, Space to"
                  " select a point or any "
                  "other key to continue");
        char key = getch();
  
        if (key == '\n') {
            break;
        }
  
        // To select the value of 'c'
        // using the position of the mouse and then
        // normalizing it between a value of -1-1i and 1+1i.
        while (key == ' ') {
  
            c = 2 * (double)(mousex() - X / 2) / X + 2 * (mousey() - Y / 2) * _Complex_I / Y;
  
            if (floor(creal(c)) == -1) {
                strcpy(str1, "-0.");
            }
  
            if (floor(creal(c)) == -0) {
                strcpy(str1, "0.");
            }
  
            if (floor(cimag(c)) == -1) {
                strcpy(str2, "-0.");
            }
  
            if (floor(cimag(c)) == -0) {
                strcpy(str2, "0.");
            }
  
            itoa(sqrt(pow(creal(c), 2))
                     * 1000,
                 strtemp, 10);
            strcat(str1, strtemp);
            strcat(str1, ", ");
            itoa(sqrt(pow(cimag(c), 2))
                     * 1000,
                 strtemp, 10);
            strcat(str2, strtemp);
            strcat(str1, str2);
            outtextxy(X * 0.8, Y * 0.8, str1);
            if (kbhit()) {
                key = getch();
            }
        }
    }
    closegraph();
    return 0;
}

输出




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